Resolver (Resolve)
Cuando definimos un esquema, a este esquema debemos proveer unos métodos resolve
, estos metodos mapea un consulta (Query) o mutacion (Mutation) para ejecutar algun codigo cuando se haga una llamada a ella. Los metodos resolver aceptan 4 parametros, estos parametros son los siguientes:
parent
Este argumento contiene los valores padres, es decir, contiene información del propio tipo.
import express from 'express'
import graphqlHTTP from 'express-graphql'
import {
GraphQLSchema,
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLInt,
GraphQLBoolean,
GraphQLNonNull,
GraphQLList } from 'graphql'
const Authors = [{
id: '8dlx7ak38fd39dv79ad',
name: 'Isiah Hamilton',
twitterHandle: '@isiah2000'
},
{
id: 'jd3kd03d0w9a0l35rh74',
name: 'Giovanni Harvey',
twitterHandle: '@gio_har'
},
{
id: '0hy894hf0dlkfh9oinv',
name: 'Lawrence Rhodes',
twitterHandle: '@rhodes'
}]
const Videos = [{
id: '1',
title: 'Mattis ullamcorper velit sed ullamcorper',
duration: 180,
watched: false,
author_id :'0hy894hf0dlkfh9oinv'
},
{
id: '2',
title: 'Habitasse platea dictumst vestibulum',
duration: 240,
watched: true,
author_id :'jd3kd03d0w9a0l35rh74'
},
{
id: '3',
title: 'Urna et pharetra pharetra massa massa',
duration: 160,
watched: false,
author_id :'8dlx7ak38fd39dv79ad'
},
{
id: '4',
title: 'Eu facilisis sed odio morbi',
duration: 260,
watched: true,
author_id :'0hy894hf0dlkfh9oinv'
}]
const getVideoById = (id) => new Promise((resolve) => {
const [video] = Videos.filter((video) => {
return video.id === id
})
resolve(video)
})
const getVideos = () => new Promise((resolve) => resolve(Videos))
const app = express()
const AuthorType = new GraphQLObjectType({
name: 'Author',
description: 'Represent a Author.',
fields: {
id: {
type: new GraphQLNonNull(GraphQLString),
description: 'Id of the author.'
},
name: {
type: new GraphQLNonNull(GraphQLString),
description: 'Name of the author.'
},
twitterHandle: {
type: GraphQLString,
description: 'Twitter name account.'
}
}
})
const VideoType = new GraphQLObjectType({
name: 'Video',
description: 'Represent a Video.',
fields: {
id: {
type: GraphQLID,
description: 'Id of the video.'
},
title: {
type: GraphQLString,
description: 'Title of the video.'
},
duration: {
type: GraphQLInt,
description: 'Duration of the video.'
},
watched: {
type: GraphQLBoolean,
description: 'Whether or not the viewer has watch the video.'
},
author: {
type: AuthorType,
description: 'Author information.',
resolve: function(parent) {
console.log(parent.author_id)
//-> "1"
//-> "2"
//-> "3"
//-> "4"
return Authors.find(author => author.id == parent.author_id)
}
}
}
})
const query = new GraphQLObjectType({
name: 'Query',
description: 'Application Schema Query Root.',
fields: () => ({
authors: {
type: new GraphQLList(AuthorType),
description: 'List of all Authors.',
resolve: function() {
return Authors
}
},
videos: {
type: new GraphQLList(VideoType),
description: 'List of all Videos.',
resolve: getVideos
}
})
})
const schema = new GraphQLSchema({
query
})
app.use('/graphql', graphqlHTTP({
schema,
rootValue: root,
graphiql: true
}))
app.listen(3000, () => {
console.log(`Server listening on port ${PORT}`)
})
Query:
query{
videos{
author {
name
}
}
}
args
Este son argumentos pasados en la Query o Mutation.
const VideoType = new GraphQLObjectType({
name: 'Video',
description: 'Represent a Video.',
fields: {
id: {
type: GraphQLID,
description: 'Id of the video.'
},
title: {
type: GraphQLString,
description: 'Title of the video.'
},
duration: {
type: GraphQLInt,
description: 'Duration of the video.'
},
watched: {
type: GraphQLBoolean,
description: 'Whether or not the viewer has watch the video.'
},
author: {
type: AuthorType,
description: 'Author information.',
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: 'Author id'
}
},
resolve: function(parent, args) {
console.log(args)
//-> { id: 'idNumber' }
return Authors.find(author => author.id == parent.author_id)
}
}
}
})
Query:
query{
videos{
author(id: "idNumber") {
name
}
}
}
context
Es un objeto especial en donde se puede integrar información utilitaria como el usuario logueado, token, etc.
const VideoType = new GraphQLObjectType({
name: 'Video',
description: 'Represent a Video.',
fields: {
id: {
type: GraphQLID,
description: 'Id of the video.'
},
title: {
type: GraphQLString,
description: 'Title of the video.'
},
duration: {
type: GraphQLInt,
description: 'Duration of the video.'
},
watched: {
type: GraphQLBoolean,
description: 'Whether or not the viewer has watch the video.'
},
author: {
type: AuthorType,
description: 'Author information.',
args: {
id: {
type: new GraphQLNonNull(GraphQLID),
description: 'Author id'
}
},
resolve: function(parent, args, context) {
console.log(context.headers)
//-> Headers Object
return Authors.find(author => author.id == parent.author_id)
}
}
}
})
execution (info)
Es otro objeto especial en donde nos devuelve información sobre la consulta.