How to implement Relay node query with GraphQL federation

Viewed 453

We are trying to implement Relay node query with Apollo federation. Since Apollo is not aware of Relay, we have to implement the node query in some service (Node Resolution Service)

interface Node {
  id: ID!
}

type Query {
  node(id: ID!): Node!
}

The trouble is that the Node resolution service is not aware of any of the implementation types as they are defined in other service subgraphs.

The Apollo Gateway sends the following request to the node resolution service

{node(id:"dHlwZUZyb21BU2VydmljZTox"){__typename ...on TypeFromAnotherService{id __typename}}}

The query validation fails as the service does not know anything about TypeFromAnotherService. We are able to implement the node query as we have the type encoded in the ID, but we do not know how to fix the validation.

  1. We can generate the schema dynamically based on the federated schema. This seems to be used here but feels cumbersome
  2. Switch-off the validation and trust Apollo GW validation. We do not like it and it seems that it is not possible in Netflix DGS which we use on backend.

Any ideas how to make Relay node query work with the federation?

2 Answers

We solved it by implementing a standalone Node Resolver. It does the following:

  1. Inspects the query and generates the schema on-the-fly in case Apollo uses a type in a fragment. Node resolver basically adds TypeFromAnotherService to the schema.
  2. Node resolver extracts Type from the ID and generates the response.

We are thinking about open-sourcing the service, would anybody be interested?

There are similar open issues on Apollo's GitHub repo for federation: https://github.com/apollographql/federation/issues/377 https://github.com/apollographql/federation/issues/1067

These discussions give some of the reasoning behind why it's an open issue and examples of how it could be worked around. In addition, this example can be used for mapping ids from nodes to entities but isn't a supported feature of Apollo: https://stackblitz.com/edit/nodemon-zbdwdj?file=README.md

If these don't work for your use case, I'd add feedback to the most relevant GitHub issue.

Related