Apollo Federation Support
Introduction
Hasura GraphQL Engine supports the Apollo Federation v1 spec, so you can add Hasura as a subgraph in your Apollo federated gateway. You can also use Hasura generated table types in your other subgraphs by enabling tables for Apollo Federation explicitly.
Apollo Federation is available from Hasura version v2.10.0
and above.
Apollo Federation can be enabled using
the server configuration
(--enable-apollo-federation
flag or setting HASURA_GRAPHQL_ENABLE_APOLLO_FEDERATION
to true
).
For backwards compatibility, the feature can also be enabled by adding apollo_federation
to the
HASURA_GRAPHQL_EXPERIMENTAL_FEATURES
environment variable array or
with the server flag --experimental-feature
.
Using Hasura tables in other subgraphs
Currently, only the types generated for the database source tables can be extended in other subgraphs. The primary key
will be used in the @key
directive's fields
argument. For example, enabling the table user
for Apollo Federation
will generate the type user
as follows:
type user @key(fields: "id") {
id: Int!
name: String
...
}
The Apollo Federation support in Hasura only allows extending other subgraphs with Hasura types. The other way i.e. extending Hasura types with other subgraphs is not possible currently. We recommend using remote relationships for extending types from other subgraphs in Hasura.
Other types such as action types, Remote Schema types, etc. cannot be extended to other subgraphs.
- Console
- CLI
- API
To enable Apollo Federation using the Hasura Console, you'll first need to add apollo_federation
to the list of
experimental features using the
HASURA_GRAPHQL_EXPERIMENTAL_FEATURES
environment variable
array or with the
server flag --experimental-feature
. You can
learn more about setting these here.
Head to the Data -> [table-name] -> Modify
tab in the Console and toggle the switch in the Enable Apollo Federation
section:
To enable Apollo Federation using the Hasura CLI head to the particular
/metadata/databases/<schema_name>/tables/<schema_name>_<table_name>.yaml
file and add the database configuration as
below:
table:
name: <table_name>
schema: <schema_name>
apollo_federation_config:
enable: v1
Apply the Metadata using the Hasura CLI by running:
hasura metadata apply
To extend the types using the Hasura Metadata API, you can to enable it
with the particular *_set_apollo_federation_config
call:
POST /v1/metadata HTTP/1.1
Content-Type: application/json
X-Hasura-Role: admin
{
"type": "pg_set_apollo_federation_config",
"args": {
"source": "<source_name>",
"table": "<table_name>",
"apollo_federation_config": {
"enable": "v1"
}
}
}