Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GraphQL - spread input args

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 ...

like image 435
Adam Rackis Avatar asked Nov 08 '22 16:11

Adam Rackis


1 Answers

For the benefit of anyone winding up here, it looks like this is impossible, at least according to an Apollo dev.

https://twitter.com/stubailo/status/914997054098759680

https://twitter.com/stubailo/status/914997127356366848

like image 179
Adam Rackis Avatar answered Nov 14 '22 23:11

Adam Rackis