Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

175
Visualizações
GraphQL server not starting due to module.exports failure

I am learning GraphQL and I faced an error due while trying to start a server.

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
});

and the server.js code is here:

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.");
});

The error seems to be coming from the "module.exports" section as when I comment it out the server starts without a problem.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

There is syntax confusion. In RootQuery the type part under user completely covers the whole field structure, error is here.

Just remove top level type: {}. Try this out:

const RootQuery = new GraphQLObjectType({
    name: 'RootQueryType',
    fields: {
        user: {
            type: UserType,
            args: { id: { type: GraphQLString } },
            resolve(parentValue, args) {
                return _.find(Users, { id: args.id });
            }
        }
    }
});
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda