Reputation: 136
I'm trying to set up a GraphQL Scheme through Express and when I load it with Graphiql it doesn't pick up the schema. When I run a query it says that I must provide a query string. My code is below. Can anyone spot anything I'm doing wrong? There are no errors in the console and the documentation explorer loads a spinning loading circle that never ends. Am I creating an infinite loop somehow?
here's the schema:
const graphql = require('graphql');
const _= require('lodash');
const { GraphQLObjectType, GraphQLString, GraphQLSchema } = graphql;
//dummy data
var books = [
{name: 'book1', genre: 'genre1', id: '1'},
{name: 'book2', genre: 'genre2', id: '2'},
{name: 'book3', genre: 'genre3', 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) {
//code to get data from db / other source
return _.find(books, {id: args.id});
}
}
}
});
module.exports = new GraphQLSchema({
query: RootQuery
})
here's the server:
const express = require("express");
const bodyParser = require("body-parser");
const app = express();
require("dotenv").config();
const axios = require("axios");
const {graphqlHTTP} = require('express-graphql')
const schema = require('./schema/schema')
// set up our middleware
app.use(bodyParser.urlencoded({ extended: true }));
app.use(express.static("server/public"));
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true,
}));
//loading on port 5000
const PORT = process.env.PORT || 5000;
app.listen(PORT, () => {
console.log("server running on: ", PORT);
});
Upvotes: 1
Views: 1631
Reputation: 136
bodyparser is incompatible with graphQL. In order to get this to work I had to remove the bodyparser middleware. This resolved it for anyone who might be following this thread.
Upvotes: 0