GraphQL Federation with Apollo - felipe-gdr/wiki GitHub Wiki

Apollo Federation article on Medium

Concepts

  • Entities: types that are referenced across different services. They are annotated with @key
type User @key(fields: "id") {
  id: ID!
  username: String
}
  • Type extensions: a service can extend a type defined in a different service
extend type User @key(fields: "id") {
  id: ID! @external
  reviews: [Review]
}

Features

  • multiple primary keys
  • compound primary keys
  • shortcuts for faster data fetching (with @provides)

Architecture

Apollo federation architecture

Pieces

  • Federated services
  • Gateway

Gateway code

const { ApolloGateway } = require("@apollo/gateway");

const gateway = new ApolloGateway({
  serviceList: [
    { name: "accounts", url: "https://pw678w138q.sse.codesandbox.io/graphql" },
    { name: "reviews", url: "https://0yo165yq9v.sse.codesandbox.io/graphql" },
    { name: "products", url: "https://x7jn4y20pp.sse.codesandbox.io/graphql" },
    { name: "inventory", url: "https://o5oxqmn7j9.sse.codesandbox.io/graphql" }
  ]
});

Questions

  1. is the @key directive part of the GraphQL spec?
  2. can each individual service be used in isolation from the "stitched" result schema?
  3. what is the difference between "federation" and "schema stitching"?

Links