简体   繁体   中英

How to use the query for custom fields on GraphQL?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM