is there a way to group queries in graphQL?

11.9k views Asked by At

I'm trying to group graphQL queries to have a more organized response.

I want to make a query for allEmployees and get back something in the following format

GraphQL Query

{
    Employees:allEmployees{
        id
        firstName
        lastName    
    }
}

Response

{
    "data": {
        "Employees": [
            "new":[
                {
                    "id": "1",
                    "firstName": "James",
                    "lastName": "Test"
                },
                {
                    "id": "3",
                    "firstName": "Charles",
                    "lastName": "Tes"
                }
            ],
            "updated":[
                {
                    "id": "4",
                    "lastName": "Test"
                },
            ],
            "deleted":[
                {
                    "id": "1",
                },
            ],
        }
    }
}

I've looked into a few options to get named sub-request( like new, updated and deleted) via aliases on fragments but that doesn't seem to be a thing. I've looked at unions, but that doesn't seem to be what I'm looking for.

Ideally I would love to query graphql like...

{
    Employees:{
        new: allEmployees(status:"new"){
            id
            firstName
            lastName    
        }

        updated: allEmployees(status:"updated"){
            id
            firstName
            lastName    
        }

        deleted: allEmployees(status:"deleted"){
            id
        }
}

but I don't think it is possible to pass a nested query like this.

Is there anyway to do something like this? I'm using graphql with ruby via the graphql-ruby gem.

please let me know if anyone needs more information?

Thanks


Edit

To clarify. We have multiple entities that will follow the new, updated, deleted pattern. Looking to try and get a response where the results are nested inside a parent name/alias (Employees, Users)

{
    "data": {
        "Employees": [
            "new":[...],
            "updated":[...],
            "deleted":[...],
        ],
        "Users": [
            "new":[...],
            "updated":[...],
            "deleted":[...],
        ],
         ...                   
}

That is why we would want to nest

1

There are 1 answers

2
David Maze On

GraphQL definitely supports nested queries and multiple top-level queries, and graphql-ruby supports these just fine.

If your GraphQL schema looks like:

type Employee {
  id: ID!
  firstName: String
  lastName: String
}
enum Status { NEW, UPDATED, DELETED }
type Query {
  allEmployees(status: Status): [Employee!]!
}

then you could write a query

fragment EmployeeData on Employee { id firstName lastName }
query Everyone {
  new: allEmployees(status: NEW) { ... EmployeeData }
  updated: allEmployees(status: UPDATED) { ... EmployeeData }
  deleted: allEmployees(status: DELETED) { ... EmployeeData }
}

That wouldn't have quite the specific form you're looking for – there aren't good ways to add or remove arbitrary levels in your query, like adding an "Employees" label or removing layers from React-style connection records – but it can retrieve the data you're looking for.