Estoy aprendiendo GraphQL y me encontré con un error al intentar iniciar un servidor.
const graphql = require('graphql'); const _ = require('lodash'); const { GraphQLObjectType, GraphQLString, GraphQLInt, GraphQLSchema } = graphql; const Users = [ { id: '23', firstName: 'Bill', age: 20 }, { id: '41', firstName: 'Jane', age: 18 } ]; const UserType = new GraphQLObjectType({ name: 'User', fields: { id: { type: GraphQLString }, firstName: { type: GraphQLString }, age: { type: GraphQLInt } } }); const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: { user: { type: { type: UserType, args: { id: { type: GraphQLString } }, resolve(parentValue, args) { return _.find(Users, { id: args.id }); } } } } }); module.exports = new GraphQLSchema({ query: RootQuery });y el código server.js está aquí:
const express = require('express'); const { graphqlHTTP } = require('express-graphql'); const schema = require('./schema/schema'); const app = express(); app.use('/graphql', graphqlHTTP({ schema, graphiql: true })); app.listen(4000, () => { console.log("Server running at port 4000."); });El error parece provenir de la sección "module.exports", ya que cuando lo comento, el servidor se inicia sin problemas.
Hay confusión de sintaxis. En RootQuery, la parte de tipo debajo del usuario cubre completamente toda la estructura del campo, el error está aquí.
Simplemente elimine el type: {} . Probar esto:
const RootQuery = new GraphQLObjectType({ name: 'RootQueryType', fields: { user: { type: UserType, args: { id: { type: GraphQLString } }, resolve(parentValue, args) { return _.find(Users, { id: args.id }); } } } });