66 GraphQLList ,
77 GraphQLInt ,
88 GraphQLID ,
9+ GraphQLError ,
910} from "graphql"
1011
1112const PROJECT_NAME = "GraphQL"
@@ -27,18 +28,21 @@ const projects: Project[] = [
2728 {
2829 name : PROJECT_NAME ,
2930 tagline : PROJECT_TAGLINE ,
31+ contributors : [ ] ,
3032 } ,
3133 {
3234 name : "GraphiQL" ,
3335 tagline : "Ecosystem for building browser & IDE tools." ,
36+ contributors : [ ] ,
3437 } ,
3538 {
3639 name : "graphql-js" ,
3740 tagline : "A reference implementation of GraphQL for JavaScript" ,
41+ contributors : [ ] ,
3842 } ,
3943]
4044
41- interface User {
45+ interface Contributor {
4246 id : string
4347 website ?: string | null
4448 contributions : number
@@ -47,14 +51,15 @@ interface User {
4751interface Project {
4852 name : string
4953 tagline : string
54+ contributors : Contributor [ ]
5055}
5156
5257interface PaginationArgs {
5358 first ?: number | null
5459 after ?: string | null
5560}
5661
57- const UserType = new GraphQLObjectType < User > ( {
62+ const UserType = new GraphQLObjectType < Contributor > ( {
5863 name : "User" ,
5964 fields : {
6065 id : {
@@ -96,11 +101,26 @@ const ProjectType = new GraphQLObjectType<Project>({
96101 description : "Cursor (User.id) after which to start" ,
97102 } ,
98103 } ,
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+ }
104124 } ,
105125 } ,
106126 } ,
@@ -118,9 +138,17 @@ const QueryType = new GraphQLObjectType({
118138 } ,
119139 } ,
120140 resolve : ( _ , args ) => {
121- return projects . find (
141+ const project = projects . find (
122142 project => project . name . toLowerCase ( ) === args . name . toLowerCase ( ) ,
123143 )
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
124152 } ,
125153 } ,
126154 projects : {
@@ -131,40 +159,6 @@ const QueryType = new GraphQLObjectType({
131159 } ,
132160} )
133161
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-
168162export const projectsSchema = new GraphQLSchema ( {
169163 query : QueryType ,
170164} )
0 commit comments