6
6
GraphQLList ,
7
7
GraphQLInt ,
8
8
GraphQLID ,
9
+ GraphQLError ,
9
10
} from "graphql"
10
11
11
12
const PROJECT_NAME = "GraphQL"
@@ -27,18 +28,21 @@ const projects: Project[] = [
27
28
{
28
29
name : PROJECT_NAME ,
29
30
tagline : PROJECT_TAGLINE ,
31
+ contributors : [ ] ,
30
32
} ,
31
33
{
32
34
name : "GraphiQL" ,
33
35
tagline : "Ecosystem for building browser & IDE tools." ,
36
+ contributors : [ ] ,
34
37
} ,
35
38
{
36
39
name : "graphql-js" ,
37
40
tagline : "A reference implementation of GraphQL for JavaScript" ,
41
+ contributors : [ ] ,
38
42
} ,
39
43
]
40
44
41
- interface User {
45
+ interface Contributor {
42
46
id : string
43
47
website ?: string | null
44
48
contributions : number
@@ -47,14 +51,15 @@ interface User {
47
51
interface Project {
48
52
name : string
49
53
tagline : string
54
+ contributors : Contributor [ ]
50
55
}
51
56
52
57
interface PaginationArgs {
53
58
first ?: number | null
54
59
after ?: string | null
55
60
}
56
61
57
- const UserType = new GraphQLObjectType < User > ( {
62
+ const UserType = new GraphQLObjectType < Contributor > ( {
58
63
name : "User" ,
59
64
fields : {
60
65
id : {
@@ -96,11 +101,26 @@ const ProjectType = new GraphQLObjectType<Project>({
96
101
description : "Cursor (User.id) after which to start" ,
97
102
} ,
98
103
} ,
99
- resolve : ( project , args ) => {
100
- return getContributorsForProject ( project , {
101
- first : args ?. first ,
102
- after : args ?. after ?? null ,
103
- } )
104
+ resolve : async ( project , args : PaginationArgs ) => {
105
+ try {
106
+ const params = new URLSearchParams ( )
107
+
108
+ if ( args . first ) params . set ( "first" , args . first . toString ( ) )
109
+ if ( args . after ) params . set ( "after" , args . after )
110
+ params . set ( "project" , project . name )
111
+
112
+ const response = await fetch ( `/api/contributors?${ params . toString ( ) } ` )
113
+
114
+ if ( ! response . ok ) {
115
+ console . error ( `Failed to fetch contributors: ${ response . status } ` )
116
+ return [ ]
117
+ }
118
+
119
+ return response . json ( )
120
+ } catch ( error ) {
121
+ console . error ( "Error fetching contributors:" , error )
122
+ return [ ]
123
+ }
104
124
} ,
105
125
} ,
106
126
} ,
@@ -118,9 +138,17 @@ const QueryType = new GraphQLObjectType({
118
138
} ,
119
139
} ,
120
140
resolve : ( _ , args ) => {
121
- return projects . find (
141
+ const project = projects . find (
122
142
project => project . name . toLowerCase ( ) === args . name . toLowerCase ( ) ,
123
143
)
144
+
145
+ if ( ! project ) {
146
+ throw new GraphQLError (
147
+ "To learn about more GraphQL projects, visit graphql.org/code/ or github.com/topics/graphql. In this playground, try 'GraphiQL', 'graphql-js' or 'graphiql'." ,
148
+ )
149
+ }
150
+
151
+ return project
124
152
} ,
125
153
} ,
126
154
projects : {
@@ -131,40 +159,6 @@ const QueryType = new GraphQLObjectType({
131
159
} ,
132
160
} )
133
161
134
- async function getContributorsForProject (
135
- project : Project ,
136
- args : PaginationArgs ,
137
- ) : Promise < User [ ] > {
138
- try {
139
- const params = new URLSearchParams ( )
140
-
141
- if ( args . first ) {
142
- params . set ( "first" , args . first . toString ( ) )
143
- }
144
-
145
- if ( args . after ) {
146
- params . set ( "after" , args . after )
147
- }
148
-
149
- params . set ( "repository" , project . name )
150
-
151
- const response = await fetch ( `/api/contributors?${ params . toString ( ) } ` )
152
-
153
- if ( ! response . ok ) {
154
- console . error ( `Failed to fetch contributors: ${ response . status } ` )
155
- return [ ]
156
- }
157
-
158
- const contributors : User [ ] = await response . json ( )
159
-
160
- // Map contributors to User format (they have the same structure now)
161
- return contributors
162
- } catch ( error ) {
163
- console . error ( "Error fetching contributors:" , error )
164
- return [ ]
165
- }
166
- }
167
-
168
162
export const projectsSchema = new GraphQLSchema ( {
169
163
query : QueryType ,
170
164
} )
0 commit comments