Skip to content

Instantly share code, notes, and snippets.

@Gethyl
Created December 28, 2016 05:12
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 Gethyl/e0a1d17d873c2c94e0044c61f93896c8 to your computer and use it in GitHub Desktop.
Save Gethyl/e0a1d17d873c2c94e0044c61f93896c8 to your computer and use it in GitHub Desktop.
graphql schema
import {
GraphQLObjectType,
GraphQLNonNull,
GraphQLSchema,
GraphQLString,
GraphQLList,
GraphQLInt,
GraphQLBoolean
} from 'graphql/type';
import ToDoMongo from '../../mongoose/todo'
/**
* generate projection object for mongoose
* @param {Object} fieldASTs
* @return {Project}
*/
export function getProjection (fieldASTs) {
return fieldASTs.fieldNodes[0].selectionSet.selections.reduce((projections, selection) => {
projections[selection.name.value] = true;
return projections;
}, {});
}
var todoType = new GraphQLObjectType({
name: 'todo',
description: 'todo item',
fields: () => ({
itemId: {
type: (GraphQLInt),
description: 'The id of the todo.',
},
item: {
type: GraphQLString,
description: 'The name of the todo.',
},
completed: {
type: GraphQLBoolean,
description: 'Completed todo? '
}
})
});
var schema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'RootQueryType',
fields: {
todo: {
type: new GraphQLList(todoType),
args: {
itemId: {
name: 'itemId',
type: new GraphQLNonNull(GraphQLInt)
}
},
resolve: (root, {itemId}, source, fieldASTs) => {
var projections = getProjection(fieldASTs);
var foundItems = new Promise((resolve, reject) => {
ToDoMongo.find({itemId}, projections,(err, todos) => {
err ? reject(err) : resolve(todos)
})
})
return foundItems
}
}
}
})
});
export default schema;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment