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/ab5ccf5767ad0b05d8fcefcc59ae95a9 to your computer and use it in GitHub Desktop.
Save ivanbtrujillo/ab5ccf5767ad0b05d8fcefcc59ae95a9 to your computer and use it in GitHub Desktop.
GraphQL. 03 — Schemas y GraphiQL - 04
// Importamos la librería GraphQl
const graphql = require('graphql'),
lodash = require('lodash');
/*
Usamos el deestructuring de Javascript Es6 para extraer el objeto GraphQLObjectType del objeto graphql
(el cual es la librería) para poder utilizarlo fuera.
*/
const {
GraphQLObjectType,
GraphQLString
} = 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({
// El nombre de la entidad (imagina que es una tabla SQL)
name: 'User',
// Los distintos campos que va a tener
fields: {
id: { type: GraphQLString },
firstName: { type: GraphQLString },
lastName: { type: GraphQLString },
country: { type: GraphQLString }
}
})
const RootQuery = new GraphQLObjectType({
// Definimos el nombre
name: 'RootQueryType',
// Los campos que va a tener
fields: {
// Buscaremos usuarios
user: {
// De tipo UserType
type: UserType,
// Y le pasaremos a la query un Id de tipo GraphQlString
args: { id: { type: GraphQLString }},
// Funcion que va a la BBDD a buscar los datos solicitados
// parentValue: no lo utilizamos en esta ocasion
// args: los argumentos que recibimos en la query
resolve(parentValue, args){
// Es6
return users.find((user) => user.id === args.id);
// Lodash
// return _.find(users, { id: args.id });
}
}
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment