0

We are using graphql-spqr to generate the graphql schema from our Java backend api and model.

Today to get products for example we annotate a methods that looks like that:

@GraphQLQuery
List<Product> allProducts() {...}

We want to avoid creating a new endpoint to retrieve extra fields in Product.

Is there a way to define a resolver for fields that are not part of the model? (annotation or not)

As an example, let's assume our java model for a Product + two end points:

class Product {
   String id;
   String name;
}

class Frame {
  String id;
}

// and some kind of relationship between the two
class ProductToFrame {
  String productId;
  String frameId
}

List<Product> allProducts();
List<Frame> getFramesByProductId(String productId);

And we want our graphql schema to look like that:

type Product {
  id: String
  name: String

  frames: [Frame] 
}

type Frame {
  id: String
}
kaqqao
  • 12,984
  • 10
  • 64
  • 118
Denis G.
  • 86
  • 6

1 Answers1

0

All you need is something like this (e.g. next to your existing allProducts() method:

@GraphQLQuery
public List<Frame> frames(@GraphQLContext Product product) {
    return getFramesByProductId(product.getId());
}

The frames field will then be attached to its contextual Product type. Or in other words: the Product type will get an extra field called frames, as desired.

Make sure you look through the tests in SPQR. They act as a showcase for most features.

kaqqao
  • 12,984
  • 10
  • 64
  • 118