Skip to content

Instantly share code, notes, and snippets.

@jschwarty
Last active October 17, 2017 10:48
Show Gist options
  • Save jschwarty/6b9ad1ffdb5c86457bd3740d515d16b2 to your computer and use it in GitHub Desktop.
Save jschwarty/6b9ad1ffdb5c86457bd3740d515d16b2 to your computer and use it in GitHub Desktop.
GraphQL Express server with desire to mock data for schema
const graphqlHTTP = require('express-graphql');
const express = require('express');
import { schema } from './schema/schema';
// Can I use something from graphql-tools with my schema...
// import { <something-here> } from 'graphql-tools';
// ...and then feed it to my express() server below?
const port = 3000;
express()
.use('/graphql', graphqlHTTP({schema: schema, pretty: true, graphiql: true}))
.listen(port);
console.log(`GraphQL server running on http://localhost:${port}/graphql`);
@GlennMatthys
Copy link

GlennMatthys commented Oct 17, 2017

Try this:

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

mockServer = gqlTools.mockServer;


var fs = require("fs");

fs.readFile('schema.graphqls',
    function (err, data)
    {
        if (err) throw err;

        const typeDefs = data.toString();

        const schema = gqlTools.makeExecutableSchema({ typeDefs });

        gqlTools.addMockFunctionsToSchema({ schema });

        app.options('/graphql', cors());

        app.use(cors());

        app.use('/graphql', graphqlHTTP({
            schema: schema,
            graphiql: true,
        }))

        app.listen(4000);
    }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment