How do I set up GraphQL query so one or another argument is required, but not both

前端 未结 2 513
感动是毒
感动是毒 2021-01-14 01:34

I\'m just getting to grips with GraphQL,

I have set up the following query: ​

type: UserType,
args: {
    id:    { name: \'id\',    type: new GraphQL         


        
相关标签:
2条回答
  • 2021-01-14 02:10

    Define an interface credentials and have that implemented as id or email.

    0 讨论(0)
  • 2021-01-14 02:26

    There's no built-in way to do that in GraphQL. You need to make your arguments nullable (by removing the GraphQLNonNull wrapper type from both of them) and then, inside your resolver, you can just do a check like:

    resolve: (root, { id, email }, { db: { User } }, fieldASTs) => {
      if (!id && !email) return Promise.reject(new Error('Must pass in either an id or email'))
      if (id && email) return Promise.reject(new Error('Must pass in either an id or email, but not both.'))
      // the rest of your resolver
    }
    
    0 讨论(0)
提交回复
热议问题