What's the best way to unit test __resolveReference?

I’m playing around with federation and converting an existing monolith service to be a federated service.

This means I’m adding a __resolveReference function to some of the core types.

What’s the best way to test this new entry point? Most of our existing “unit”(ish) tests are basically just send a query to an in memory server and assert the response - using the normal server.executeOperation helper.

The problem is that you can’t send a regular query to trigger __resolveReference in isolation to the in-memory server - it can only be called via the gateway, so you’d need to spin up the gateway + a fake service to call __resolveReference + the actual service under test - all in memory.

Looks like this is what federation-testing-tool does - and it makes sense to me. As someone who’s entirely new to federation, this feels the most natural migration path to me.

I can’t find any “official” recommendations on the the apollo docs about what we’re supposed to do here - perhaps just import the resolver map and call the resolver directly? which feels slightly gross.

Would be great to have some sort of paved path here or official guidance on this.

Thanks!

Mark

You can mimic what the gateway does with an operation like this:

query SubgraphEntityQuery($refs: [_Any!]!) {
  _entities(representations: $refs) {
    __typename
    ... on Product {
      id
      someProductField
    }
    ... on User {
      id
      someUserField
    }
  }
}

So your test should like something like:

const response = await server.executeOperation({ 
  query: subgraphEntityQuery, 
  variables: {
    refs: [
      { __typename: "Product", id: "1" },
      { __typename: "User", fields: "listed", in: "key directives" }
    ]
  }
});

Hope this helps!

2 Likes