Syntax/pre-compilation issue with graphql and express

70 Views Asked by At

Hi I am following this video to make a booking system enter link description here

And my code is identical to the creator but it isn't running (error message shown below)

    [nodemon] restarting due to changes...
[nodemon] starting `node app.js`
C:\Users\Abdulrahman\Desktop\bookings1\app.js:12
    graphqlHttp({
    ^

TypeError: graphqlHttp is not a function
    at Object.<anonymous> (C:\Users\Abdulrahman\Desktop\bookings1\app.js:12:5)
    at Module._compile (node:internal/modules/cjs/loader:1101:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1153:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:81:12)
    at node:internal/main/run_main_module:17:47
[nodemon] app crashed - waiting for file changes before starting...

And code in the app.js file is

const express = require('express');
const bodyParser = require('body-parser');
const graphqlHttp = require('express-graphql');
const { buildSchema } = require('graphql');

const app = express();

app.use(bodyParser.json());

app.use(
    '/graphql',
    graphqlHttp({
        schema: buildSchema(`
        type RootQuery {
            events: [String!]!
        }
        type RootMutation {
            createEvent(name: String): String
        }
        schema {
            query: RootQuery
            mutation: RootMutation
        }
    `),
        rootValue: {
            events: () => {
                return ['Romantic Cooking', 'Sailing', 'All-Night Coding'];
            },
            createEvent: (args) => {
                const eventName = args.name;
                return eventName;
            }
        },
        graphiql: true
    })
);

app.listen(3000);

Any ideas how to fix this error? To get to this debugger? Thanks alot as I've been stuck on this for a while! enter image description here

1

There are 1 best solutions below

0
On BEST ANSWER

To fix the error TypeError: graphqlHttp is not a function, replace

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

with

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

(Notice the different case for HTTP, versus Http)

Of course, in the other lines, graphqlHttp must be replaced by graphqlHTTP.

https://graphql.org/graphql-js/express-graphql/