GraphQL - spread input args

Viewed 2321

I'm trying to specify a GraphQL schema with a type that has various fields, and also allows you to tack on an OR: [...] recursively with the same arguments. I've got it working like this

input BookFilters {
  _id: String
  title: String
  title_contains: String
  title_startsWith: String
  title_endsWith: String
  OR: [BookFilters]
}

type Query {
   allBooks(_id: String
     title: String
     title_contains: String
     title_startsWith: String
     title_endsWith: String
     OR: [BookFilters]
   ): [Book]
}

which works great, and this query

{
  allBooks(title_endsWith: "1", OR: [{ title: "2" }, {title:"3"}, {OR: [{title_contains: "Q"}, {OR:[{title:"Q"}]}]}]) {
    _id
    title
  }
}

goes through just fine.

My question is, is there some way to remove the duplication? Ideally I'd love it if

type Query {
   allBooks(...BookFilters): [Book]
}

was valid, but it doesn't appear to be.


Edit - A comment brought up fragments. They can only be declared off of a type, right? So I'd still have a make a new type that lists all my query arguments, and then make a fragment off of it, re-listing all the fields?

Even if that's not so, GraphQL still doesn't seem to like spreading on input arguments.

fragment BookFiltersFragment on BookFilters {
    _id
}

type Query {
   allBooks(...BookFiltersFragment): [Book]
}

which errors out on the ...

2 Answers

We build our schema from plain JS instead of the query language and are able to use the spread since our arguments are plain-old-javascript-objects. I believe the "GraphQLInputType" is a fully-fledged instantiation that won't allow spreading easily.

Related