How can I propagate graphQL query argument to child dataFetchers using graphQL java

194 Views Asked by At

I am using those GraphQL Java dependencies:

|  +- com.graphql-java:graphql-java:jar:17.3:compile
|  |  +- com.graphql-java:java-dataloader:jar:3.1.0:compile

With this schema:

schema {
    query: Query
}

type Query {
    users: UsersNamespace
}


type UsersNamespace {

    userByPhone(phone: String!): User!
}

type User {
    id: String!
    photoId: String
}

Using this query

{
  user1: userByPhone(phone: "375290201111") {
      id
      photoId
  }
  user2: userByPhone(phone: "375290202222") {
      id
      photoId
  }
}

Assume I have a separate dataFetcehr for the User itself and the photoId which I get from a different source.

I want to pass the phone argument (or even the alias of the parent user) down to the photoId data fetcher.

I can not save the information on the GraphQlContext since the resolution is not performed "Depth-first", meaning the data fetchers will be called in this order:

  1. Users data fetcher for user1
  2. Users data fetcher for user2
  3. photoId data fetcher for user1
  4. photoId data fetcher for user2

So anything I will save for user1 will be overridden by user data-fetcher.

How can I pass down arguments / information to child resolvers under the same alias?

*Note: photoId is always resolved only within a User

1

There are 1 best solutions below

0
On BEST ANSWER

The solution is to use ExecutionStepInfo like so:

environment.getExecutionStepInfo()
      .getParent()
      .getArgument("phone").toString()