4

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

SegFaultDev
  • 455
  • 1
  • 7
  • 24
  • just play with `/graphiQL` ? simply place one query below the other – xadm Sep 28 '18 at 21:36
  • @xadm thanks for the comment. I'm looking to group queries with a label. Please see edit above if you have a second. – SegFaultDev Sep 30 '18 at 01:44
  • If you need to do this for different entities, you'll need to define a `Type` for each of them. Once you have that, it's just a matter of grouping the records you query from the database into the respective label that you want. – Andy Nov 14 '18 at 22:19

1 Answers1

2

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.

David Maze
  • 130,717
  • 29
  • 175
  • 215
  • 1
    Thanks for the response @DavidMaze. I've updated the question to further explain the format I'm looking for. Based on your answer tho, I don't think its possible to do what I'm looking for. Would I be able to make a type that resolves into the format i'm looking for, or would that break graphql conventions? – SegFaultDev Sep 30 '18 at 01:43
  • You could create a type structure that produced that exact response format, by adding query `Employees: EmployeeStatuses` and then making a type with the `new`, `updated`, and `deleted` fields. It is a little less...GraphQLy though, and it feels like rewriting your API to match one specific client's expectations. – David Maze Sep 30 '18 at 10:53