To overcome the limitations in GraphQL, federation architecture is introduced. With this architecture, the GraphQL schema can be broken down into smaller microservices. To merge these microservices, a GraphQL federation gateway is required. The gateway will direct GraphQL requests to the relevant subgraph implementation, and it will support multiple programming languages to accommodate a broad range of GraphQL subgraphs.
Goals
Implement a GraphQL federation gateway
Non-Goals
Provide a way to implement subgraphs using ballerina-graphql
Generating the supergraph schema from subgraph schema
Provide support for dynamically updating gateway endpoints corresponding to changes in subgraphs
Motivation
It is a common practice in GraphQL to create a collection of subgraphs and then merge them into a single supergraph when performing large deployments. This practice offers means of splitting monolithic GraphQL servers into independent microservices (subgraphs) which can be independently scaled and managed by responsible teams. This is commonly done following Apollo Federation specifications. The gateway can resolve a client query by connecting to these subgraphs and executing a query plan. It would be ideal if the ballerina has a gateway module to interact with the subgraph implementations.
Description
GraphQL Federation helps to split monolithic GraphQL servers into independent microservices. The federation consists of two main components.
Independent micro services
A gateway
Each independent microservice holds a part of the GraphQL schema and the gateway merges the schema into a single composed schema. In other words, these independent microservices create subgraphs which are combined by the gateway to create a supergraph. This supergraph can be consumed by the client application by connecting to the gateway.
A ballerina service needs to act as a gateway and route the request to correct graphql end-points to fetch data. This service will be generated as an executable (a .jar file) via the Ballerina GraphQL CLI tool.
Configuration and Initialization
The Ballerina federation gateway executable can be generated by the following command.
bal graphql -m federation-gateway -i <supergraph-schema-file>
The supergraph schema file needs to be provided as a .graphql as an input. It can be composed and obtained by a third party tool such as rover cli. (Composing a supergraph schema can be a part of the Ballerina GraphQL CLI tool in the future).
Validation
The supergraph schema provided will be parsed and validated by a schema parser.
Building query plan
The plan to query the subgraphs is composed to a table by parsing the supergraph schema.
For example to the below schema,
The common types ( ex: QueryPlanEntry ) and function used for gateway can be published as a repository for ballerina central and imported upon gateway generation.
The query plan is straightforward from the supergraph SDL. The graphql request types and the fields that can be resolved from the gateway can be decided from the type definitions in the supergraph SDL. For example from the above scenario,
Above segment defines the graphql query requests that can handle only astronaut, astronauts, mission, missions arguments.
For resolving the query requests the gateway may generate and inject the resolver functions to the graphql while generating the executable.
For example to resolve
query{
astronaut(id: <id>){
# data need to be fetched
}
}
requests the generated function will be as,
service on new graphql:Listener(5000){
resource function get astronaut (int id) returns Astronaut {
// using graphql clients data will be fetched from subgraphs
// make use of the @join__field(graph: <GRAPH-ENUM-VALUE>) to identify the corresponding subgraph url
}
}
Response generation
The resolver of the gateway core module will compose and return the response in the requested type. The response sent to the client is handled by the already existing response generator and formatter of the ballerina graphql module.
The location field in error messages will be removed from an interceptor before the response is delivered to the client. Since it is irrelevant for the response from the gateway.
Testing
The gateway could be tested with Ballerina graphql subgraph microservices.
Load testing also needs to be carried out.
Future plans
Provide GraphQL CLI tool support to compose a supergraph schema using subgraphs
Build the executable file by accepting the federated subgraph urls instead of the supergraph schema.
Option to periodically update the gateway according to the changes in subgraph.
Summary
To overcome the limitations in GraphQL, federation architecture is introduced. With this architecture, the GraphQL schema can be broken down into smaller microservices. To merge these microservices, a GraphQL federation gateway is required. The gateway will direct GraphQL requests to the relevant subgraph implementation, and it will support multiple programming languages to accommodate a broad range of GraphQL subgraphs.
Goals
Non-Goals
Motivation
It is a common practice in GraphQL to create a collection of subgraphs and then merge them into a single supergraph when performing large deployments. This practice offers means of splitting monolithic GraphQL servers into independent microservices (subgraphs) which can be independently scaled and managed by responsible teams. This is commonly done following Apollo Federation specifications. The gateway can resolve a client query by connecting to these subgraphs and executing a query plan. It would be ideal if the ballerina has a gateway module to interact with the subgraph implementations.
Description
GraphQL Federation helps to split monolithic GraphQL servers into independent microservices. The federation consists of two main components.
Each independent microservice holds a part of the GraphQL schema and the gateway merges the schema into a single composed schema. In other words, these independent microservices create subgraphs which are combined by the gateway to create a supergraph. This supergraph can be consumed by the client application by connecting to the gateway. A ballerina service needs to act as a gateway and route the request to correct graphql end-points to fetch data. This service will be generated as an executable (a
.jar
file) via the Ballerina GraphQL CLI tool.Configuration and Initialization
The Ballerina federation gateway executable can be generated by the following command.
The supergraph schema file needs to be provided as a .graphql as an input. It can be composed and obtained by a third party tool such as rover cli. (Composing a supergraph schema can be a part of the Ballerina GraphQL CLI tool in the future).
Validation
The supergraph schema provided will be parsed and validated by a schema parser.
Building query plan
The plan to query the subgraphs is composed to a table by parsing the supergraph schema. For example to the below schema,
Following kind of table will be generated and used for dynamically resolving the incoming query requests.
The common types ( ex:
QueryPlanEntry
) and function used for gateway can be published as a repository for ballerina central and imported upon gateway generation. The query plan is straightforward from the supergraph SDL. The graphql request types and the fields that can be resolved from the gateway can be decided from the type definitions in the supergraph SDL. For example from the above scenario,Above segment defines the graphql
query
requests that can handle onlyastronaut
,astronauts
,mission
,missions
arguments.For resolving the query requests the gateway may generate and inject the resolver functions to the graphql while generating the executable.
For example to resolve
requests the generated function will be as,
Response generation
The resolver of the gateway core module will compose and return the response in the requested type. The response sent to the client is handled by the already existing response generator and formatter of the ballerina graphql module.
The
location
field in error messages will be removed from an interceptor before the response is delivered to the client. Since it is irrelevant for the response from the gateway.Testing
Future plans