Skip to content

Instantly share code, notes, and snippets.

@ivanbtrujillo
Last active June 18, 2017 16:32
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 ivanbtrujillo/a8199fa2ef0a187b51782c4cacd7137d to your computer and use it in GitHub Desktop.
Save ivanbtrujillo/a8199fa2ef0a187b51782c4cacd7137d to your computer and use it in GitHub Desktop.
GraphQL. 03 — Schemas y GraphiQL - 05
const graphql = require('graphql');
const {
GraphQLObjectType,
GraphQLString,
// Importamos GraphQL Schema de GraphQL mediante destructuring
GraphQLSchema
} = graphql;
const users = [
{ id: '1', firstName: 'Ivan', lastName: 'Bacallado', country: 'España' },
{ id: '2', firstName: 'Ana', lastName: 'Lopez', country: 'Mexico' },
{ id: '3', firstName: 'Bill', lastName: 'Thomson', country: 'EEUU' },
{ id: '4', firstName: 'Maximilian', lastName: 'Lahm', country: 'Alemania' },
];
const UserType = new GraphQLObjectType({
name: 'User',
fields: {
id: { type: GraphQLString },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
country: { type: GraphQLString }
}
})
const RootQuery = new GraphQLObjectType({
name: 'RootQueryType',
fields: {
user: {
type: UserType,
args: { id: { type: GraphQLString }},
resolve(parentValue, args){
return users.find((user) => user.id === args.id);
}
}
}
})
// Exportamos nuestro schema
module.exports = new GraphQLSchema({
query: RootQuery
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment