Search code examples
graphqlapolloapollo-federation

How to federate two apollo services that provide the same type


I am new to apollo and I have two apollo service that I want to federate by using apollo federation:

Productservice:

extend type Query {
  job(id: String!): Job
}

type Seo {
  title: String! 
  description: String! 
  keywords: String! 
}

type Product @key(fields: "id")  {
  id: ID!
  title: String!
  seo: Seo!
}

StaffService:

extend type Query {
  staffMember(id: String!): StaffMember
}

type Seo {
  title: String! 
  description: String! 
  keywords: String! 
}

type StaffMember @key(fields: "id")  {
  id: ID!
  title: String!
  seo: Seo!
}

How can I use the type Seo in response objects of both objects? Is the correct procedure to create an interface Seo and implement StaffMemberSeo and ProductSeo or is there an annotation that allows me to define the exactly same type within two services?


Solution

  • One service must own the type. In that service use the @key directive. In the referencing services use @extend, and include a stub of the type with fields used by that service.

    Think of this as like a foreign key in a SQL database.