0

I have this schema on my graphcool:

type User @model {
  id: ID! @isUnique
  name: String!
  email: String!
  password: String!
}

Using playground, I can execute this properly:

query {
  User(id: "1234") {
    id
    name
  }
}

But this query:

query {
  User(name: "Thomas") {
    id
    name
  }
}

throws error:

Unknown argument 'name' on field 'User' of type 'Query'. (line 2, column 8):
User(name: "Thomas").

Why? And how to fix this? From my pov, anything that's already on the model, can be queried immediately, right? Btw, I'm very newbie in graphQL, and there's almost no article talk about this error (every tutorial just like assume that this will immediately works), so please give some more elaborate answer if necessary.

1 Answer 1

1

GraphQL does not intrinsically allow arbitrary queries against objects.

Somewhere in your schema there will be an additional declaration like

type Query {
  User(id: ID!): User
}

The names in the Query type are the top-level queries you can run, and the arguments listed in that query are the only arguments they accept. (There is a corresponding Mutation type for top-level mutations, which can change the underlying state, and use the mutation keyword in a query.)

If you control the server implementation, you could add a parameter or an additional top-level query

userByName(name: String!): User

but you'd also have to provide an implementation of this query or handle the additional parameter, which is a code change.

Sign up to request clarification or add additional context in comments.

2 Comments

OOOOOH okay. Yea I remember I saw that type Query somewhere (not in the tutorials), but I don't know what was that for. Omg I don't know why that's not on the basic tutorials. About the server implementation, can you point me out to a tutorial that's talking about this server implementation? I do control the server implementation, but I don't know how I can implement the server implementation at all. Btw, do I need to add extend so it become extend type Query, or not? Some code I read has extend.
From a more thorough reading of tutorials, the one I'm asking above was actually advanced, a "custom query". The simple query API graphcool provided actually has already provided many of the basic queries, and I'm not supposed to use this kind if I'm not prepared to write my own custom queries. That's why I was quite surprised of how graphql is able to magically interpret that I was going to search for user named "Thomas", and not less than or more than or not contain "Thomas". Turns out that's really not the case. For my case, I can use filter to search for user with specific name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.