简体   繁体   中英

GraphQL nested query arguments?

When building a GraphQL API, is it possible to allow arguments on nested query properties? ie I've implemented an orderBy arg for a top-level tasks query like so:

query {
  tasks(orderBy: { order: asc }) {
    title
  }
}

and this works fine, but I would like to be able to query a collection of task s and add the query arguments to the nested tasks property like this:

query {
  collection {
    id
    name
    tasks(orderBy: { order: asc }) {
      title
    }
  }
}

It doesn't recognize the arguments by default, so I assume if it is possible, then there is some further set up required. I get this error when I try that query: "Unknown argument \\"orderBy\\" on field \\"tasks\\" of type \\"Collection\\"."

PS I'm using graphql-yoga with prisma on the backend.

Do you use nexus/schema and nexus-plugin-prisma ? If you do, you have to activate ordering in your collection task model like this t.model.tasks({ ordering: true })

Aha! I worked it out in the end. I just needed to pass the args to the tasks invocation in the Collection.tasks resolver:

Collection: {
  tasks: (parent, args, context) => {
    const { id } = parent

    const collection = context.prisma.collection.findOne({
      where: { id },
    })

    return collection.tasks(args) // ← pass in `args` here
  },
},

I also needed to add the orderBy option in my schema for the Collection.tasks field (I previously only had it on Query.tasks )

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