-
-
Save latentflip/f1c9520ac0d83b5237fc to your computer and use it in GitHub Desktop.
graphql-js broken example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
graphql, | |
GraphQLSchema, | |
GraphQLObjectType, | |
GraphQLString, | |
GraphQLNonNull, | |
GraphQLList, | |
} from 'graphql'; | |
var projectVersionType = new GraphQLObjectType({ | |
name: 'ProjectVersion', | |
fields: () => ({ | |
id: { type: new GraphQLNonNull(GraphQLString) }, | |
}) | |
}); | |
var projectReturnType = new GraphQLObjectType({ | |
name: 'ProjectReturn', | |
fields: () => ({ | |
id: { type: new GraphQLNonNull(GraphQLString) }, | |
versions: { | |
type: new GraphQLList(projectVersionType), | |
resolve: (project) => { | |
return [ | |
{ id: '1' }, | |
{ id: '2' }, | |
]; | |
} | |
} | |
}) | |
}); | |
var projectPromiseType = new GraphQLObjectType({ | |
name: 'ProjectPromise', | |
description: 'A project', | |
fields: () => ({ | |
id: { | |
type: new GraphQLNonNull(GraphQLString), | |
}, | |
versions: { | |
type: new GraphQLList(projectVersionType), | |
resolve: (project) => { | |
return Promise.resolve([ | |
{ id: '1' }, | |
{ id: '2' }, | |
]); | |
} | |
} | |
}) | |
}); | |
var orgType = new GraphQLObjectType({ | |
name: 'Org', | |
description: 'An organisation', | |
fields: () => ({ | |
id: { | |
type: new GraphQLNonNull(GraphQLString), | |
}, | |
projectReturns: { | |
type: new GraphQLList(projectReturnType), | |
resolve: (org) => { | |
return [ | |
{ id: '1', name: 'My project 1' }, | |
]; | |
} | |
}, | |
projectPromises: { | |
type: new GraphQLList(projectPromiseType), | |
resolve: (org) => { | |
return [ | |
{ id: '2', name: 'My project 2' }, | |
]; | |
} | |
} | |
}) | |
}); | |
var schema = new GraphQLSchema({ | |
query: new GraphQLObjectType({ | |
name: 'RootQueryType', | |
fields: { | |
org: { | |
type: orgType, | |
resolve: () => { | |
return { id: 1 }; | |
} | |
} | |
} | |
}), | |
}); | |
var query = ` | |
query Foo { | |
org { | |
id, | |
projectReturns { | |
id, | |
versions { | |
id | |
} | |
} | |
projectPromises { | |
id, | |
versions { | |
id | |
} | |
} | |
}, | |
} | |
` | |
graphql(schema, query).then(result => { | |
console.log('RESULT', JSON.stringify(result, null, 2)); | |
}).catch(console.log); | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
RESULT { | |
"data": { | |
"org": { | |
"id": "1", | |
"projectReturns": [ | |
{ | |
"id": "1", | |
"versions": [ | |
{ | |
"id": "1" | |
}, | |
{ | |
"id": "2" | |
} | |
] | |
} | |
], | |
"projectPromises": [ | |
{} | |
] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment