Run
npm i
from the root folder and within the each of the services folder.
To run the project in development mode:
npm run dev-federated
or
npm run dev-stitched
To run the project in production mode:
npm run start-federated
or
npm run start-stitched
classDiagram
Actor <--> Cast
Cast <--> Movie
class Actor{
id: ID!
name: String!
}
class Movie{
id: ID!
title: String!
year: String!
}
class Cast{
id: ID!
movieId: ID!
actorId: ID!
}
flowchart TD
A[GraphQL Gateway] --> |HTTP| B[Movie Server]
A[GraphQL Gateway] --> |HTTP| C[Cast Server]
A[GraphQL Gateway] --> |HTTP| D[Actor Server]
subgraph Movie Server
B[Movie Service] --> E[(Movie Data)]
end
subgraph Cast Server
C[Cast Service] --> F[(Cast Data)]
end
subgraph Actor Server
D[Actor Service] --> G[(Actor Data)]
end
|
flowchart TD
A[GraphQL Gateway] --> B[Movie Service]
A[GraphQL Gateway] --> C[Cast Service]
A[GraphQL Gateway] --> D[Actor Service]
B[Movie Service] --> E[(Movie Data)]
C[Cast Service] --> F[(Cast Data)]
style C stroke-dasharray: 5 5
style F stroke-dasharray: 5 5
D[Actor Service] --> G[(Actor Data)]
|
Entrypoint: ./01.federated.gateway.ts
Example Query: query {
actors {
name
movies {
title
year
}
}
} Execution:
|
Entrypoint: ./02.stitched.gateway.ts
Example Query: query {
actors {
name
movies {
title
year
}
}
} Execution:
|
GraphQL Federation and Schema Stitching are two ways to build a GraphQL schema from multiple GraphQL services. This document describes the differences between the two techniques.
Federation is a specification that describes how to distribute a GraphQL API into multiple distributed subservices. The specification is provided by Apollo.
- Federation is an official specification
- Federation's Gateway is much easier to get up and running
- Federation allows to develop and deploy separate services completely independently by multiple teams
- Federation mandates for each service to be hosted as a separate server process
- Federation introduces new directives that have to be used across services' schema definitions
- Federation doesn't support subscriptions out of the box but there are some hacky solutions
- I wasn't able to find a viable quick way to setup a quick graphQL wrapper over REST API other than setting up a completely new service
Schema Stitching is a technique that describes how to merge multiple GraphQL schemas into one. The technique is provided by Apollo.
- Schema Stitching does not require exploring new directives outside of official GraphQL language (even though it's optionally available - e.g.
@merge
directive) - Schema Stitching does not require a separate GraphQL server per service to be running
- Schema Stitching allows separate services to share a single server process
- Schema Stitching enables easy to setup GraphQL wrapper over the REST API
- Schema Stitching supports subscriptions
- Schema Stitching comes with a much larger intial boilerplate
- Schema Stitching has a single shared gateway file which needs to be maintained when new connections are introduced which complicates an independent service development
- Schema Stitching is not an official specification
Federation was much easier to set up and make sense of. It also comes up with quite extensive documentation in oppose to schema stitching. It is a formal specification maintained by Apollo team. Schema stitching on the other hand provides more flexibility in terms of infrastructure set up and addresses a broader variety of problems.
Neither of the above. If my next project was a startup then I'd consider the above methods to be a premature optimization and unnecessary over-engineering. If on the other hand my next project would be a long running project that has finances to invest into performance then I would consider much more scalable option of microservices structure like Apache Kafka. The only usecase I can imagine me using the Schema Stitching would be a gradual rewrite of an existing application. I would receive requests on a gateway and forward them either to the old codebase or if the part of the system was already rewritten into a new system then I would forward it to the new codebase. We've also used Schema Stitching on Coinflip where the core of the system was a GraphQL server written in C# and our new modules and functionalities were built in NodeJS as a separate GraphQL API. A gateway stitched both GraphQL schema built in C# with our GraphQL schema built in NodeJS
Gateway is the first point of contact from frontend environments. It's exposed to the general public. It's responsible for putting together an execution plan based on the services registered and then distribute the subschema requests to them. After that the responses are combined and returned to the client.
The service is located in ./services/rating. I've also injected it into ./02.stitched.gateway.ts. I didn't find any information on how to incorporate it into federated gateway other than defining a completely new service.