mercurius-js / mercurius-gateway

Mercurius federation support plugin
MIT License
17 stars 11 forks source link

Support Apollo Managed federation #28

Open StarpTech opened 3 years ago

StarpTech commented 3 years ago

The difference between federation is that the gateway is no longer responsible to specify the service configurations. The entire composed graph is fetched by the gateway. This allows to dynamically adding and removing services and schema updates without restarting the gateway.

https://www.apollographql.com/docs/rover/supergraphs

Specs:

Example:

https://github.com/StarpTech/graphql-registry/tree/main/examples/apollo-managed-federation

mcollina commented 3 years ago

I think this is extremely simple to add, we should just expand refresh() to accept a new list of services. Or maybe does this include a little bit more work?

StarpTech commented 3 years ago

That would be a limited workaround. Apollo produces a supergraph-sdl that includes service endpoints and the federated schema. The gateway is only responsible to read the schema and make the federated schema working.

For example, based on the federation example:

schema
  @core(feature: "https://specs.apollo.dev/core/v0.1"),
  @core(feature: "https://specs.apollo.dev/join/v0.1")
{
  query: Query
}

directive @core(feature: String!) repeatable on SCHEMA

directive @join__field(graph: join__Graph, requires: join__FieldSet, provides: join__FieldSet) on FIELD_DEFINITION

directive @join__type(graph: join__Graph!, key: join__FieldSet) repeatable on OBJECT | INTERFACE

directive @join__owner(graph: join__Graph!) on OBJECT | INTERFACE

directive @join__graph(name: String!, url: String!) on ENUM_VALUE

scalar join__FieldSet

enum join__Graph {
  ACCOUNTS @join__graph(name: "accounts" url: "http://localhost:4001/graphql")
  INVENTORY @join__graph(name: "inventory" url: "http://localhost:4004/graphql")
  PRODUCTS @join__graph(name: "products" url: "http://localhost:4003/graphql")
  REVIEWS @join__graph(name: "reviews" url: "http://localhost:4002/graphql")
}

type Product
  @join__owner(graph: PRODUCTS)
  @join__type(graph: PRODUCTS, key: "upc")
  @join__type(graph: REVIEWS, key: "upc")
  @join__type(graph: INVENTORY, key: "upc")
{
  upc: String! @join__field(graph: PRODUCTS)
  name: String @join__field(graph: PRODUCTS)
  price: Int @join__field(graph: PRODUCTS)
  weight: Int @join__field(graph: PRODUCTS)
  reviews: [Review] @join__field(graph: REVIEWS)
  inStock: Boolean @join__field(graph: INVENTORY)
  shippingEstimate: Int @join__field(graph: INVENTORY, requires: "price weight")
}

type Query {
  topProducts(first: Int = 5): [Product] @join__field(graph: PRODUCTS)
  me: User @join__field(graph: ACCOUNTS)
}

type Review
  @join__owner(graph: REVIEWS)
  @join__type(graph: REVIEWS, key: "id")
{
  id: ID! @join__field(graph: REVIEWS)
  body: String @join__field(graph: REVIEWS)
  author: User @join__field(graph: REVIEWS, provides: "username")
  product: Product @join__field(graph: REVIEWS)
}

type User
  @join__owner(graph: ACCOUNTS)
  @join__type(graph: ACCOUNTS, key: "id")
  @join__type(graph: REVIEWS, key: "id")
{
  id: ID! @join__field(graph: ACCOUNTS)
  name: String @join__field(graph: ACCOUNTS)
  username: String @join__field(graph: ACCOUNTS)
  reviews: [Review] @join__field(graph: REVIEWS)
}