Get list of requested fields inside resolver #14

Open
opened 2024-09-02 00:13:03 +02:00 by prescientmoon · 2 comments
Contributor

Hi!

I'm not super familiar with the GraphQL terminology, so this question might sound a bit nonsensical. When a GQL query is received, the request might only ask for a subset of the available fields. Is there any way for a Resolve monad to ask for the list of available fields?

Hi! I'm not super familiar with the GraphQL terminology, so this question might sound a bit nonsensical. When a GQL query is received, the request might only ask for a subset of the available fields. Is there any way for a `Resolve` monad to ask for the list of available fields?
prescientmoon changed title from Get requested values inside resolver to Get list of requested fields inside resolver 2024-09-02 00:13:46 +02:00
Owner

No, currently the resolver context contains only the field arguments and the value returned by the parent resolver. The fields requested can be seen only in the parsed query.

I find such a feature useful, I just don't use it in my application, so I haven't looked into implementing it. In my resolvers I always rely on the fact that if the field was requested, its resolver will be called, and I load the data in that resolver if needed. Not sure if it is useful for your use case, but I'll try to give an example.

type Worker {
  id: ID!,
  firstName: String!
}

type Project {
  number: ID!
  manager: Worker
}

type Query {
  projects: [Project!]!
}

Now if the user queries the projects, my root resolver returns the following:

{
  "projects": [
    "number": "123"
    "manager": {
      "id": 1
    }
  ]
}

So only the "id" of the Worker object is returned. Now the manager resolver is only called if the user requests it. So in this resolver I have access to the ID from the parent resolver and can query the remaining fields of the manager. The query will be executed only if the user requests some of the manager fields.

Sorry for the long explanation if it's not useful for you use case.

No, currently the resolver context contains only the field arguments and the value returned by the parent resolver. The fields requested can be seen only in the parsed query. I find such a feature useful, I just don't use it in my application, so I haven't looked into implementing it. In my resolvers I always rely on the fact that if the field was requested, its resolver will be called, and I load the data in that resolver if needed. Not sure if it is useful for your use case, but I'll try to give an example. ```graphql type Worker { id: ID!, firstName: String! } type Project { number: ID! manager: Worker } type Query { projects: [Project!]! } ``` Now if the user queries the projects, my root resolver returns the following: ```json { "projects": [ "number": "123" "manager": { "id": 1 } ] } ``` So only the "id" of the `Worker` object is returned. Now the `manager` resolver is only called if the user requests it. So in this resolver I have access to the ID from the parent resolver and can query the remaining fields of the `manager`. The query will be executed only if the user requests some of the `manager` fields. Sorry for the long explanation if it's not useful for you use case.
belka added the
Kind
enhancement
label 2024-09-03 14:57:58 +02:00
Author
Contributor

@belka My usecase is that sometimes certain fields return errors (in my case, int overflow errors), but the query should still go through when those fields are not asked for.

@belka My usecase is that sometimes certain fields return errors (in my case, int overflow errors), but the query should still go through when those fields are not asked for.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: OSS/graphql#14
No description provided.