Home > database >  cant fetch graphql server
cant fetch graphql server

Time:12-31

hi I followed a tutorial for GraphQL, that is the code I wrote until now :

    const express = require('express');
const {graphqlHTTP} = require('express-graphql');
const graphql = require('graphql')
const {
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLString,
    GraphQLList,
} = graphql;
const users = require('./users.json')
const app = express();

const userType = new GraphQLObjectType({
    name:'User',
    fields: () => ({
        email: { type: GraphQLString},
        username: { type: GraphQLString},
        name: { type: GraphQLString},
        company: { type: GraphQLString},
        address: { type: GraphQLString},
    })
})

const RootQuery = new GraphQLObjectType({
    name:'RootQueryType',
    fields: () => ({
        getAllUsers: {
            type: new GraphQLList(userType),
            resolve(parent,args){
                return users
            }
        }
    })
})

const Mutation = new GraphQLObjectType({
    name:"mutation",
    fields: () => ({
        createUser: {
            type: userType,
            args:{
                email: { type: GraphQLString},
                username: { type: GraphQLString},
                name: { type: GraphQLString},
                company: { type: GraphQLString},
                address: { type: GraphQLString},
            },
            resolve(parent,args){
                users.push({ email: args.email,
                             username: args.usename,
                             name: args.name,
                             company: args.company,
                             adress: args,company,
                })
                return args
            }
        }
    })
})

const schema = new GraphQLSchema({query: RootQuery, mutation: Mutation})

app.use('/graphql', graphqlHTTP({
    schema:schema,
    graphql:true
}))
app.listen(5000, ()=>{
    console.log('server started')
})

but when I got to localhost:5000/graphql in the browser I get : {"errors":[{"message":"Must provide query string."}]} i tried to check graphqlstring is imported correctly and everything is fine I searched a lot, I hope the answer is here.

CodePudding user response:

Can you replace graphql: true, by graphiql: true, ? as per here Also in general try to have the port as 4000

  • Related