1

I'm trying to learn graphql. I didn't find any good course witch will help me to learn it. So i started building it with little examples. In this moment i'm getting error when i'm trying to open http://127.0.0.1:3000/graphql. it's telling me "message": "Must provide query string."

I thinks i did something wrong with my users query? This is my complete code of it. Can someone please help...

// user type
const UserType = new GraphQLObjectType({
  name: "User",
  description: "User Type",
  fields: () => ({
    id: { type: GraphQLInt },
    firstname: { type: GraphQLString },
    lastname: { type: GraphQLString },
    email: { type: GraphQLString },
    password: { type: GraphQLString },
  }),
});

// register (mutation)
const register = {
  type: UserType,
  args: {
    firstname: { type: GraphQLString },
    lastname: { type: GraphQLString },
    email: { type: GraphQLString },
    password: { type: GraphQLString },
  },
  //@ts-ignore
  async resolve(parent, args) {
    const { firstname, lastname, email, password } = args;

    const user = new User();
    user.firstname = firstname;
    user.lastname = lastname;
    user.email = email;
    user.password = password;
    const result = await user.save();
    console.log(result);
    return result;
  },
};

// users (query)
const users = {
  // type: new GraphQLList(UserType),
  type: UserType,
  args: { id: { type: GraphQLInt } },
  //@ts-ignore
  async resolve(parent, args) {
    const users = await User.find();
    return users;
  },
};

const MutationType = new GraphQLObjectType({
  name: "MutationType",
  description: "Mutations",
  fields: { register },
});

const QueryType = new GraphQLObjectType({
  name: "QueryType",
  description: "Queries",
  fields: { users },
});

const schema = new GraphQLSchema({ query: QueryType, mutation: MutationType });

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

I have 2 problems.

  1. when i type in browser: http://127.0.0.1:3000/graphql it's don't loading. it's telling me Loading... and stuck on it.
  2. when i try it in insomnia it's telling me
{
  "errors": [
    {
      "message": "Must provide query string."
    }
  ]
}

how can i fixed it ?

Bergi
  • 630,263
  • 148
  • 957
  • 1,375
BasDriver
  • 171
  • 3
  • 9
  • Where is it telling you "*Must provide query string.*"? – Bergi Jun 25 '22 at 06:14
  • when i write in browser this address http://127.0.0.1:3000/graphql i get json error like this: { "errors": [ { "message": "Must provide query string." } ] } – BasDriver Jun 25 '22 at 06:16
  • Ah, yes, well you didn't pass a graphql query. (And also you didn't make a POST request). What did you expect? – Bergi Jun 25 '22 at 06:19
  • Btw, the [option passed to `graphqlHTTP`](https://github.com/graphql/express-graphql#options) is called [`graphiql`](https://github.com/graphql/graphiql/tree/main/packages/graphiql#readme), not `graphql` – Bergi Jun 25 '22 at 06:21
  • I'm doing it first time, I just want to learn how it works, and if i make this code work, i will understand how it's works. now i'm doing it blind, because of it i didn't pass graphql query. )) i just don't know really how to do it. i searched about it, but didn't find examples how to pass graphql query, If u know how can i make this code run, can you please help me with this? : ) – BasDriver Jun 25 '22 at 06:23
  • 1
    You need to use a GraphQL client: https://graphql.org/graphql-js/graphql-clients/ – robertklep Jun 25 '22 at 06:24
  • changed it to graphiql now it's stuck on loading... )) thanks for response. ) – BasDriver Jun 25 '22 at 06:24
  • I'm using insomnia i changed app.post( "/graphql", graphqlHTTP({ schema, graphiql: true, }) ); how can i ppass graphql query to see all users? – BasDriver Jun 25 '22 at 06:35
  • Please edit your question to explain your current issue(s). – robertklep Jun 25 '22 at 06:36
  • "*when i try it in insomnia*" - please show us what exactly you are doing. It seems you're still not sending a proper request. robertklep already pointed you to the documentation, you don't have to do it blind. – Bergi Jun 25 '22 at 06:47
  • i saw docs, and i'm trying to send data in insomnia like this { "query": "{QueryType}" } am getting this error { "message": "Cannot query field \"QueryType\" on type \"QueryType\".", – BasDriver Jun 25 '22 at 06:53

0 Answers0