Skip to content

Instantly share code, notes, and snippets.

@latentflip
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save latentflip/f1c9520ac0d83b5237fc to your computer and use it in GitHub Desktop.
Save latentflip/f1c9520ac0d83b5237fc to your computer and use it in GitHub Desktop.
graphql-js broken example
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);
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