Search code examples
ecmascript-6graphqlecmascript-5apollo

Export graphql schema using es5


I'm writing a script that runs an Express server with graphql. I m using ES5.

Here is my server.js code (to run the Express server) :

const express = require('express');
const cors = require('cors');

const bodyParser = require('body-parser');
const {graphqlExpress, graphiqlExpress} = require('apollo-server-express');

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

const app = express();
app.listen(4000, () => { console.log("Listening on 4000")});

app.use('/graphql', bodyParser.json(), graphqlExpress({schemaTest})); 
app.use('/graphiql', graphiqlExpress({endpointURL: '/graphql'}));

and here is the code of my schema.js

const {makeExecutableSchema, addMockFunctionsToSchema} = require('graphql-tools');

const typeDefs = `type Query {
  greeting: String
}
`;

const schema = makeExecutableSchema({typeDefs});
addMockFunctionsToSchema({ schema });

module.exports = schema;

however i'm getting this isse :

Error: Expected undefined to be a GraphQL schema.

and I m not able to find where is my error.

For your informtion, if I copy paste my schema.js code into the server.js file it works correctly, it is like I m not importing (or exporting) the schema file correctly.

Where is my error


Solution

  • graphqlExpress is expecting a configuration object to be passed to it, with one of the properties on that object being schema. So your code should look something like this:

    app.use('/graphql', bodyParser.json(), graphqlExpress({
      schema: schemaTest,
    }));
    

    What you are currently doing is passing in an object with a schemaTest property, but no schema property.