Created
December 28, 2016 05:12
-
-
Save Gethyl/e0a1d17d873c2c94e0044c61f93896c8 to your computer and use it in GitHub Desktop.
graphql schema
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 { | |
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