Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

166
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!