简体   繁体   中英

Query argument spread in GraphQL

I have the following schema:

input ResumeFilter {
  id: ID
  accountId: ID
  lang: Lang
}

extend type Query {
  resume(filter: ResumeFilter): Resume!
  resumes(filter: ResumeFilter, offset: Int, limit: Int): [Resume!]!
}

Now I'd like to remove the filter keyword from the resume query as it won't have anything besides that:

extend type Query {
  resume(id: ID, accountId: ID, lang: Lang): Resume! # !!! repeats ResumeFilter !!!
  resumes(filter: ResumeFilter, offset: Int, limit: Int): [Resume!]!
}

To solve DRY violation I'm trying something like this:

extend type Query {
  resume(...ResumeFilter): Resume!
  resumes(filter: ResumeFilter, offset: Int, limit: Int): [Resume!]!
}

Which is syntactically incorrect. Is there a syntax/way to solve this case gracefully? Maybe with Fragments?

Fragments can only be used client-side with executable documents. There is no syntax for spreading an input object. If your server-side language supports string interpolation, you can use that. For example, in JavaScript:

const resumeFilterFields = `
  id: ID
  accountId: ID
  lang: Lang
`

const schema = `
  input ResumeFilter {
    ${resumeFilterFields}
  }

  extend type Query {
    resume(${resumeFilterFields}): Resume!
    resumes(filter: ResumeFilter, offset: Int, limit: Int): [Resume!]!
  }
`

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