How to pass additional information to child resolver in GraphQL?

Viewed 261

I am using GraphQL Java implementation and my simplified schema looks like:

topSection: TopSection {
  anotherSection: AnotherSection {
    name: String
    ## someMetadata: String (not part of schema)
    someAttribute: Attribute
  }
}

I have to implement resolvers for topSection and someAttribute field. For other fields, I will rely on GraphQL's implicit property fetching.

Now, for resolver of someAttribute to work, I need to set and pass someMetadata to it.

One option is to make it part of schema and someAttribute resolver can retrieve it from parent argument but I would prefer to not make it part of schema. I have verified that this works.

Then, I tried to return a subclass of AnotherSection with someMetadata field from topSection resolver, but I am getting internal failure with NPE.

class TopSectionResolver implements DataFetcher<TopSection> {
    @Override
    public TopSection get(final DataFetchingEnvironment dataFetchingEnvironment) {
       AnotherSection anotherSection = AnotherSectionSubClass.builder().name().someMetadata().someAttribute().build();
       return TopSection.builder().anotherSection(anotherSection).build();
    }
}

class SomeAttributeResolver implements DataFetcher<Attribute> {
    @Override
    public Attribute get(final DataFetchingEnvironment dataFetchingEnvironment) {
        String someMetadata = ((AnotherSectionSubClass) dataFetchingEnvironment.getSource()).getSomeMetadata();
        // other processing
    }
}

AnotherSectionSubClass extends AnotherSection {
    String name;
    String someMetadata;
    Attribute someAttribute
}

Error:

{\"errors\":[{\"message\":\"The execution failed due to an internal error: java.util.concurrent.ExecutionException: java.lang.NullPointerException\",\"extensions\":{\"classification\":\"DataFetchingException\"}}]}

I couldn't figure the cause of failure from GraphQL logs.

  1. Can you someone explain why this approach of passing subclass is failing?
  2. What is other alternative to achieve what I want?
0 Answers
Related