Tim Dean
Tim Dean

Reputation: 8292

Changing default data fetcher behavior for graphql-java

I am doing a proof-of-concept project using graphql-java to implement the backend of a GraphQL interface. In my project, the persistence layer I am retrieving data from is a proprietary API that has map-like operations, and I would like to write custom data fetching logic against that proprietary API.

The runtime wiring for my root-level query looks something like the following:

RuntimeWiring wiring = newRuntimeWiring()
    .type("query", builder -> builder
        .dataFetcher("myQuery", new MyQueryFetcher()))
    .build();

The get() method currently returns an instance of a class called PersistenceStore.Node, and that object has map-like access methods that I can call in subsequent data fetchers. But I've been unable to get a data fetcher wired up right to handle those subsequent fetches.

With the above example, I'd like to have a query such as the following:

query {
  myQuery {
    id,
    name,
    description
  }
}

I would like the MyQueryFetcher class shown in the above runtime wiring return a PersistenceStore.Node instance. Then I'd like the references to id, name, and description fields to be resolved by another custom data fetcher implementation that I create.

I've tried adding a default data fetcher to my wiring:

RuntimeWiring wiring = newRuntimeWiring()
    .type("query", builder -> builder
        .defaultDataFetcher(new PersistenceStoreNodeFetcher())
        .dataFetcher("myQuery", new MyQueryFetcher()))
    .build();

but that doesn't appear to work - the PersistenceSAtoreNodeFetcher class's get method is never called.

I've been digging through the graphql-java documentation and source code but have not found anything yet.

Any idea how I can replace or customize the PropertyDataFetcher behavior with a fetcher of my own design?

Upvotes: 4

Views: 2441

Answers (1)

Doctor Eval
Doctor Eval

Reputation: 3961

I suspect the defaultDataFetcher is being applied to the query object, not to the object type returned by myQuery. What you probably need to do is to apply the defaultDataFetcher to the myQuery object type.

RuntimeWiring wiring = newRuntimeWiring()
    .type("query", builder -> builder
      .dataFetcher("myQuery", new MyQueryFetcher()))
    .type("myQueryResult")
      .defaultDataFetcher(new PersistenceStoreNodeFetcher())
    .build();

Upvotes: 1

Related Questions