mercurius-js / mercurius-integration-testing

Mercurius integration testing utility library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Feature proposal: add helper to test federated entities

paolochiodi opened this issue · comments

When working with federated schemas it's common to have a service extend a type defined in another service to add some data.

As an example you may have service A managing users with a schema like:

type User @key(fields: "id") {
  id: ID!
  name: String!
}

extend type Query {
  me: User!
}

And service B managing posts with a schema like

type Post  @key(fields: "id") {
  id: ID! @xternal
  description: String!
}

type User @key(fields: "id") @extends {
  id: ID! @xternal
  posts: [Post!]!
}

How do we test User.posts in service B, where we do not have any query returning a user?

There are a number of possible solutions, like setting up a fake federation during tests, but I think the cleanest solution would be to test the same query that the graphql federation gateway would run to get posts for a user.

In the example above, it would be something like

{
  _entities(representations: [{ __typename: "User", id: "user1"}]) {
    __typename
    ... on User {
      posts
    }
  }
}

Using mercurius-integration-testing I can run that query. However it seems a bit obscure and require some boilerplate.

A getEntity method or similar that simplifies running this type of queries could be a nice addition to the tool.

Something like

const client = createMercuriusTestClient(server)

const response = await client.getEntity('User',`
  {
    posts
  }
`)