/
Launch Apollo Studio

Value types

Define the exact same type in multiple services


It's common to reuse a GraphQL type across multiple implementing services. For example, you might define a generic Error interface and an ErrorCode enum:

interface Error {
  code: ErrorCode!
  message: String!
}

enum ErrorCode {
  UNKNOWN_ERROR
  NETWORK_ERROR
  AUTHENTICATION_ERROR
  SERVER_ERROR
}

These value types don't "originate" in a particular service. Instead, all of the services that define a value type share ownership.

Most importantly, a value type must be defined identically in every service that defines it. Otherwise, your federated schema will fail composition. Not every service needs to define a particular value type.

Any of the following can be a value type:

  • Scalars
  • Objects
  • Interfaces
  • Inputs
  • Enums
  • Unions

Considerations for each of these are listed below.

Scalars

If you define a custom scalar as a value type, make sure all of your implementing services use the exact same serialization and parsing logic for it. The schema composition process does not verify this.

Objects, interfaces, and inputs

For types with field definitions, all fields and their types must be identical (including nullability).

If an object type is an entity, it cannot be a value type.

Enums and unions

For enums and unions, all possible values must match across all defining services. Even if a particular service doesn't use a particular value, that value still must be defined in the schema. Otherwise, your federated schema will fail composition.

Edit on GitHub