Introduction
Tyk, with release v4.0 offers GraphQL federation that allows you to divide GraphQL implementation across multiple back-end services, while still exposing them all as a single graph for the consumers. Tyk Operator supports GraphQL Federation subgraph and supergraph with following Custom Resources.Custom Resources
GraphQL Federation uses concepts of Subgraph and Supergraph. Subgraph is a representation of a back-end service and defines a distinct GraphQL schema. It can be queried directly as a separate service or it can be federated into a larger schema of a supergraph. Supergraph is a composition of several subgraphs that allows the execution of a query across multiple services in the backend. Tyk Operator uses Custom Resources called SubGraph and SuperGraph, that allows users to model the relationship between Subgraphs and Supergraphs.SubGraph
schema
and sdl
values for your subgraph.
To create a Subgraph API in Tyk, you can reference the subgraph’s metadata name through graphql.graph_ref
field, as follows:
- ApiDefinition and SubGraph must be in the same namespace,
graphql.execution_mode
must be set tosubgraph
,graphql.graph_ref
must be set to the metdata name of the SubGraph resource that you would like to refer.
SuperGraph
subgraph_refs
and schema
values for your supergraph. subgraph_refs
is an array of SubGraph Custom Resource(CR) references which expects the name and namespace of the referenced subgraph. If namespace
is not specified, Operator will check SubGraphs in the current namespace.
Tyk Operator will update your SuperGraph ApiDefinition when one of the subgraphs that you reference in subgraph_refs
changes.
To create a SuperGraph API in Tyk, you can reference the supergraph’s metadata name through graphql.graph_ref field
, as follows:
- ApiDefinition and SuperGraph must be in the same namespace,
graphql.execution_mode
must be set tosupergraph
,graphql.graph_ref
must be set to the metdata name of the SuperGraph resource that you would like to refer.
Propagating Subgraph Changes to Supergraph
Tyk Operator will automatically propagate changes in SubGraph CRD to the corresponding Subgraph ApiDefinition. Also, if the SubGraph is referenced by a SuperGraph, the corresponding SuperGraph CR and corresponding supergraph ApiDefinition will be updated too. Therefore, once you make an update on SubGraph CR, you do not need to update your supergraph. It will be updated by Tyk Operator. With this approach, multiple teams can work on SubGraph CRDs and Tyk Operator will update the relevant SuperGraph ApiDefinition.Example
Let’s assume that a developer responsible for the Users SubGraph would like to deleteusername
field from the Users SubGraph.
Also, the Supergraph called Social Media already uses the Users Subgraph.
To achieve this, the developer should update the Users SubGraph CRD. Once the SubGraph CRD is updated, Tyk Operator will:
- Update Users SubGraph CRD,
- Update Social Media Supergraph ApiDefinition since it is referencing the Users SubGraph CRD.
Deleting SubGraph
-
SubGraph without any reference
If the subgraph is not referenced in any ApiDefinition CRD or SuperGraph CRD, it is easy to delete SubGraph CRDs as follows:
-
SubGraph referenced in ApiDefinition
If you have a subgraph which is referenced in any ApiDefinition, Tyk Operator will not delete the SubGraph.
In order to delete this subgraph, the corresponding ApiDefinition CR must be updated, such that it has no reference to the
subgraph in
graph_ref
field. -
SubGraph referenced in SuperGraph
Although the subgraph is not referenced in any ApiDefinition, if it is referenced in the SuperGraph, Tyk Operator will
not delete the subgraph again.
In order to delete this subgraph, SuperGraph CR should not have reference to corresponding subgraph in the
subgraph_ref
.
Deleting SuperGraph
-
SuperGraph without any reference
If the supergraph is not referenced in any ApiDefinition CRD, it can be deleted as follows:
- SuperGraph referenced in ApiDefinition If a supergraph is referenced in any ApiDefinition, the Tyk Operator will not delete the SuperGraph CRD. In order to delete this supergraph, the ApiDefinition that has a reference to the supergraph must de-reference the supergraph or be deleted.