4

I'm trying to learn Graphql by making a simple Node application with some dummy data to test with.

When I try to run Node I get the following error

TypeError: Cannot set property 'name' of undefined
at GraphQLObjectType (/Users/BorisGrunwald/Desktop/programmering/Javascript/Node/graphQLServer/server/node_modules/graphql/type/definition.js:480:15)
at Object.<anonymous> (/Users/BorisGrunwald/Desktop/programmering/Javascript/Node/graphQLServer/server/schema/schema.js:22:23)
at Module._compile (internal/modules/cjs/loader.js:721:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:732:10)
at Module.load (internal/modules/cjs/loader.js:620:32)
at tryModuleLoad (internal/modules/cjs/loader.js:560:12)
at Function.Module._load (internal/modules/cjs/loader.js:552:3)
at Module.require (internal/modules/cjs/loader.js:657:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/Users/BorisGrunwald/Desktop/programmering/Javascript/Node/graphQLServer/server/app.js:6:20)

These are the only two files in my project:

app.js

const express = require('express');
const graphqlHTTP = require('express-graphql');

const app = express();

const schema = require('./schema/schema');

app.use('/graphql',graphqlHTTP({
    schema
}));

app.listen(4000,() => {
    console.log('listening on port 4000');
});

schema.js

const graphql = require('graphql');   

const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;


const dummyData = [
    {name: 'Name of the Wind', genre: 'Fantasy', id:'1'},
    {name: 'The Final Empire', genre: 'Fantasy', id:'2'},
    {name: 'The Long Earth', genre: 'Sci-Fi', id:'3'}
];

const BookType = new GraphQLObjectType({
    name:'Book',
    fields: () => ({
        id: {type : GraphQLString},
        name: {type: GraphQLString},
        genre: {type: GraphQLString}
    })
});

const RootQuery = GraphQLObjectType({
    name:'RootQueryType',
    fields: {
        book:{
            type:'BookType',
            args: {id:{type:GraphQLString}},
            resolve(parent,args) {
                return dummyData.find(book => book.id === args.id)
            }
        }
    }
});


module.exports = new GraphQLSchema({
    query:RootQuery
});

Can any of you spot the error? Thanks in advance.

1 Answer 1

17

while creating RootQuery object you just missed the "new" keyword for GraphQLObjectType class. I have fixed that please have a look


const graphql = require('graphql');   

const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;


const dummyData = [
    {name: 'Name of the Wind', genre: 'Fantasy', id:'1'},
    {name: 'The Final Empire', genre: 'Fantasy', id:'2'},
    {name: 'The Long Earth', genre: 'Sci-Fi', id:'3'}
];

const BookType = new GraphQLObjectType({
    name:'Book',
    fields: () => ({
        id: {type : GraphQLString},
        name: {type: GraphQLString},
        genre: {type: GraphQLString}
    })
});

const RootQuery = new GraphQLObjectType({
    name:'RootQueryType',
    fields: {
        book:{
            type:'BookType',
            args: {id:{type:GraphQLString}},
            resolve(parent,args) {
                return dummyData.find(book => book.id === args.id)
            }
        }
    }
});


module.exports = new GraphQLSchema({
    query:RootQuery
});
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you very much, that was it.
type:'BookType' should be type: BookType

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.