Back to Graphql Engine

Apollo Federation Support

docs/docs/data-federation/apollo-federation.mdx

2.49.43.8 KB
Original Source

import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; import Thumbnail from '@site/src/components/Thumbnail';

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.

:::tip Supported from

Apollo Federation is available from Hasura version v2.10.0 and above.

:::

:::caution Enabling apollo-federation

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:

graphql
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.

:::info Supported types

Other types such as action types, Remote Schema types, etc. cannot be extended to other subgraphs.

:::

<Tabs groupId="user-preference" className="api-tabs"> <TabItem value="console" label="Console">

:::info Prerequisite before enabling Apollo Federation on the Hasura Console

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:

<Thumbnail src="/img/graphql/core/data-federation/apollo-federation-enable.png" alt="Set table as enum" /> </TabItem> <TabItem value="cli" label="CLI">

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:

yaml
table:
  name: <table_name>
  schema: <schema_name>
apollo_federation_config:
  enable: v1

Apply the Metadata using the Hasura CLI by running:

bash
hasura metadata apply
</TabItem> <TabItem value="api" label="API">

To extend the types using the Hasura Metadata API, you can to enable it with the particular *_set_apollo_federation_config call:

http
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"
    }
  }
}
</TabItem> </Tabs>