# Graphs

> How subgraphs declare a federated graph, how the broker composes it in each environment, how a consumer of the graph and a subgraph are checked, what composition checks, the graph page, and the known gaps.

A federated graph is the API that a router serves by composing several subgraphs, each a participant that publishes its own GraphQL schema. In Bidirekt the graph is only a name, declared by its subgraphs; nobody creates it and it is not a participant. A consumer calls the graph by that name, and the broker checks it against the graph composed from the subgraphs deployed in the environment.

## Declaring the graph

A subgraph publishes its schema with the `@link` of Apollo Federation 2, and a YAML file that names its graph:

```yaml
provides:
  graphql:
    graph: petstore_graph
```

```graphql
extend schema @link(url: "https://specs.apollo.dev/federation/v2.3", import: ["@key", "@shareable"])

type Pet @key(fields: "id") {
  id: ID!
  reviews: [String!]!
}
```

```
$ bidirekt publish graph.yaml schema.graphqls --participant petstore_reviews --version 2.0.0
petstore_reviews contract publish successful
```

- Any `v2.x` of the federation spec is read, with the definitions of the newest one. Another version is refused with its position: `schema.graphqls:1:15: federation v3.0 is not supported; supported: v2.x`.
- A Federation 1 schema, without `@link`, is read as a subgraph schema when it declares `graph` or uses `@key`, `@external`, `@requires`, `@provides` or `@extends`, with these directives and `@tag` by their own names.
- A subgraph may extend a type that only another subgraph defines.
- A subgraph is still a GraphQL provider of its own: a consumer may call it directly by its participant name.
- A subgraph schema published without `graph` joins no graph. It is an ordinary GraphQL provider, called by its participant name, with the same resources it would have in a graph: the definitions a subgraph library adds, such as `_service` and `_entities`, are left out ([Composition](#composition)).
- A `@key` whose `fields` doesn't parse, is empty or names a field the type doesn't have publishes, and composition ignores it ([Composition](#composition)).
- A subgraph may change its graph between versions. The graph of the version deployed in an environment is the one that counts there.

Its errors at publish, each printed after the file's name as in [publish](https://bidirekt.com/docs/ship/publish.md):

- `graph "petstore_graph" without a GraphQL schema at provides graphql, pass its .graphql, .graphqls or .gql in the same command`: a `graph` without a schema file in the same publish. With the [TypeScript SDK](https://bidirekt.com/docs/contracts/typescript-sdk.md#graphql), the [PHP SDK](https://bidirekt.com/docs/contracts/php-sdk.md#graphql) or the [Go SDK](https://bidirekt.com/docs/contracts/go-sdk.md#graphql), the generated contract of a subgraph is published together with the schema, as `bidirekt publish contracts/ src/schema.graphql`.
- `invalid graph name "Petstore-Graph" at provides graphql, must be snake_case`: a graph name follows the rules of a participant name.
- `graph name "petstore_graph" is already a participant name at provides graphql, pick another graph name`.
- `graph "other_graph" conflicts with graph "petstore_graph" declared in contracts/api.yaml at provides graphql, a contract belongs to one graph: declare the same name in every file`: two YAML files of one publish that name different graphs. The same name in several files is one graph.

Graph names and participant names share one space. Once a subgraph has published a graph name, [`create-participant`](https://bidirekt.com/docs/reference/cli.md#create-participant) refuses it:

```error title="CLI error"
cannot post participant to broker: "petstore_graph" is a graph name; pick another participant name
```

## Consuming a graph

A consumer names the graph under `consumes`, as it names a provider, and writes the fields it calls as for any [GraphQL provider](https://bidirekt.com/docs/graphql/resources.md#the-consumer-declares-what-it-calls):

```yaml
consumes:
  petstore_graph:
    graphql:
      query:
        pet:
          arguments: PetArgs
          returns: MaybePet
```

The graph of an environment is the composition of the subgraphs deployed there, made at each check, so it may differ from one environment to another. The consumer is compared with it, and the report names the graph with the versions of its subgraphs in place of a version:

```error title="can-i-deploy output"
petstore_web 2.3.0 cannot be deployed to production

petstore_graph (petstore_api 1.4.0 + petstore_reviews 2.0.0, deployed):
  Pet.age — petstore_web reads it as String!, but petstore_graph provides Int!
    → petstore_web: read it as Int!
    → petstore_graph: provide it as String! there
      · query pet returns at "$.age"
    deployed graph: /graphs/petstore_graph
```

Each break of a property of the graph ends with the link to the [graph page](#the-graph-page). A root field the composed graph does not resolve is `calls query pet, but petstore_graph doesn't provide it`. A graph with no subgraph deployed in the environment is `calls query pet, but petstore_graph is not deployed in production`, under `petstore_graph:`, and a graph whose deployed subgraphs do not compose is `calls query pet, but petstore_graph can't be built in production`, followed by every composition error and `deployed graph: /graphs/petstore_graph` ([Breaks](https://bidirekt.com/docs/graphql/breaks.md#every-break)).

A subgraph may consume its own graph. In its check, the graph is composed with the version under check, marked `candidate`: `petstore_graph (petstore_api 1.5.0 candidate + petstore_reviews 2.0.0, deployed):`. [`validate`](https://bidirekt.com/docs/ship/validate.md) has no version, so it prints `petstore_api (local candidate)`. The participant's own deployed version is never one of its counterparts.

## Checking a subgraph

The check of a subgraph compares, as for any provider, the consumers that call it directly. It also composes the graph with the new version, and compares each consumer of the graph deployed in the environment with it. A break that comes through the graph names the subgraph with ` (via petstore_graph)` ([In the report](https://bidirekt.com/docs/graphql/breaks.md#in-the-report)).

A subgraph answers only for the breaks it causes. The broker also composes the graph as it is without the change, with the subgraph's deployed version, or without the subgraph when none of its versions is deployed there or that version is in another graph. Only the breaks that the new version has and that graph does not are reported. So:

- a break that a field of another subgraph already causes does not block this one, and shows only in the consumer's own check;
- a composition error between other subgraphs is not this subgraph's;
- `makes petstore_graph fail to compose` shows only when the graph composes without the change and does not compose with it.

When the graph does not compose without the change but composes with it, the subgraph is compared with the graph composed without it: a version that fixes the composition can be deployed, even while a consumer is already broken by another subgraph. In that case the fields the new version drops, compared with its own deployed version, are not reported. When neither composes, the consumer is not compared.

A subgraph that leaves the graph, with a version without `graph`, is checked against the graph composed without it, and a subgraph that moves to another graph is checked in both.

## Composition

Bidirekt checks that the subgraphs deployed in the environment, composed, serve what each consumer of the graph declares. The graph may be partial: when only some subgraphs of a graph are deployed in an environment, a consumer that reads a field of a missing subgraph breaks on that field, and deploying the missing subgraphs fixes it. How a subgraph is written, such as its directives `@shareable` and `@external` or its default values, is checked by Apollo's rover in the provider's pipeline ([rover supergraph](https://www.apollographql.com/docs/rover/commands/supergraphs)).

The broker refuses to compose only when an element has no single shape across the subgraphs. A field the graph can't serve leaves the graph only for the root fields that can't reach it ([Fields a root field can't reach](#fields-a-root-field-cant-reach)). Only `@key`, `@inaccessible`, `@external`, `@override`, `@provides`, `@requires`, `@interfaceObject`, `@context`, `@fromContext` and `@extends` count; every other directive is accepted and ignored. An argument marked `@fromContext` is left out of the contract, since the router fills it and a client never sends it. When another subgraph declares that argument optional, it leaves the graph; when another subgraph requires it, the graph is refused. The definitions that a subgraph library prints next to the `@link`, such as `_service`, `_entities`, `_Any`, `_Entity`, `_Service`, `FieldSet` and `link__*`, are not part of the contract nor of the graph.

What composes:

- A field resolved by more than one subgraph has its types joined, with or without `@shareable`.
- Output fields join, and are nullable when any subgraph has them nullable. Two compatible output types join as the wider one: `Holder.item: Node` in one subgraph and `Thing`, which implements `Node`, in the other compose as `Node`. A shared field can also return a union or an interface in one subgraph and one of its members in another: `search: SearchResult`, with `union SearchResult = Pet | Review`, and `search: Pet`.
- Arguments and input fields intersect, and non-null wins.
- An enum used only as output joins, one used only as input intersects, and one used as both must be the same in every subgraph.
- An `@inaccessible` element leaves the graph, and stays when the subgraph is called directly.
- A field, an optional argument or an optional input field whose type is `@inaccessible` leaves the graph with it. An object, input or enum left with no field or value, or a union or interface left with no member, leaves the graph too, with every field, optional argument and optional input field of its type, and with its place in the unions and interfaces it belongs to. This repeats until nothing else is left empty: `type Audit { entries: Int @inaccessible }` takes `Pet.audit: Audit` out of the graph. A consumer that reads a field that left breaks on that field, and one that doesn't read it passes. A root field that leaves is one the graph doesn't provide.
- A field that a subgraph marks `@external` is resolved by the other subgraphs. When no subgraph resolves it, it is not in the graph, and a consumer that reads it breaks on that field.
- A field with `@override(from: "<subgraph>")` is resolved only by the subgraph that overrides it, and has its type there. The `from` names a subgraph by its participant name: `@override(from: "petstore_api")`.
- A field with `@override(from: "petstore_api", label: "percent(50)")`, a progressive override, is resolved by both subgraphs while the migration runs. So its type is the join of both sides: `String` in one and `String!` in the other compose as `String`, and `String` in one and `Int` in the other do not compose. A root field from which a query can't reach each subgraph that serves the field loses the field.
- An `@interfaceObject` composes as the interface, and every type that implements it gets its fields.
- The graph has only the roots that some subgraph declares.

The errors name the subgraphs, their versions and the element, one text per rule:

| Rule | Error |
|---|---|
| output types that do not join | `petstore_api 1.4.0 resolves Pet.age as Int, but petstore_reviews 2.0.0 as String` |
| argument or input field types that do not intersect | `petstore_api 1.4.0 and petstore_search 2.0.0 disagree on Query.pets(limit:): Int in petstore_api, String in petstore_search` |
| a required argument or input field that another subgraph does not declare | `petstore_api 1.4.0 requires Query.pets(limit:), which petstore_reviews 2.0.0 doesn't declare` |
| an argument that one subgraph fills from a context and another requires | `petstore_greetings 2.0.0 requires Pet.greeting(name:), which petstore_api 1.4.0 fills from a context with @fromContext` |
| a required argument or input field that is `@inaccessible` | `petstore_api 1.4.0 requires Query.pet(debug:), which is @inaccessible` |
| a required argument or input field whose type is `@inaccessible` | `petstore_api 1.4.0 requires Query.pets(filter:), whose type PetFilter is @inaccessible` |
| a required argument or input field whose type has nothing left in the graph | `petstore_api 1.4.0 requires Query.pets(filter:), whose type PetFilter has nothing left in the graph` |
| an enum used as input and output that differs | `petstore_api 1.4.0 and petstore_reviews 2.0.0 declare different values for Status, used as input and output` |
| a root with another name beside a type with the default name | `petstore_api 1.4.0 has a type named Query, but its query root is RootQuery` |
| any other conflict, such as a type of another kind | `petstore_api 1.4.0 and petstore_reviews 2.0.0 disagree on Pet: object in petstore_api, interface in petstore_reviews` |

## Fields a root field can't reach

A query starts at a root field and goes from one subgraph to another through the jumps that the `@key`s allow and the fields that a `@provides` on the path selects. A field is served under a root field when a query from that root field can reach a subgraph that resolves it:

- a `@key` counts only when its `fields` parses and the subgraph it leads to declares every field it selects; a `@key` with `resolvable: false` lets no query in;
- a field with `@requires(fields: "…")` is served only where what it selects can be reached from the subgraph that resolves the field, and never when `fields` doesn't parse;
- an argument with `@fromContext` is served only where a type above its field in the query sets the context with `@context`;
- a field with a progressive `@override` is served only where each subgraph that serves it can be reached.

A field that no place of its type under a root field can serve leaves the graph for that root field only. The other root fields keep it. A consumer that reads it through that root field breaks on that field, `reads it, but petstore_graph doesn't provide it`, and a root field that can't be served at all is one the graph doesn't provide. The graph still composes.

`petstore_api` serves `Pet.name`, but its `@key` doesn't let another subgraph jump to it. `petstore_reviews` returns `Review.pet` with only the `id` (the `@link` of both is left out here):

```graphql
# petstore_api
type Query {
  pet(id: ID!): Pet
}

type Pet @key(fields: "id", resolvable: false) {
  id: ID!
  name: String!
}
```

```graphql
# petstore_reviews
type Query {
  reviews: [Review!]!
}

type Review {
  stars: Int!
  pet: Pet!
}

type Pet @key(fields: "id") {
  id: ID!
}
```

A consumer that reads `pet(id) { name }` or `reviews { pet { id } }` passes. One that reads `reviews { pet { name } }` breaks, because no query from `reviews` reaches `petstore_api`:

```error title="can-i-deploy output"
petstore_graph (petstore_api 1.4.0 + petstore_reviews 2.0.0, deployed):
  Pet.name — petstore_web reads it, but petstore_graph doesn't provide it
    → petstore_web: stop reading it
    → petstore_graph: provide it
      · query reviews returns at "$[].pet.name"
    deployed graph: /graphs/petstore_graph
```

In a subgraph's check, the subgraph answers only for the fields its new version makes unreachable, as `Pet.name — petstore_reviews (via petstore_graph) doesn't provide it, but petstore_web reads it`.

## The graph page

`/graphs/<name>` in the [Web UI](https://bidirekt.com/docs/start/run-the-broker.md#web-ui) shows, for each environment, the subgraphs deployed there with their versions, and whether they compose, with every composition error. Under `Left out of the graph`, it lists each field a root field can't reach, with that root field and the cause: `Pet.name`, `through Query.reviews`, `query reviews.pet.name can't be resolved: no resolvable @key leads from petstore_reviews 2.0.0 to petstore_api 1.4.0 on Pet`. A graph with fields left out still composes. An environment without a subgraph of the graph shows `no subgraph deployed`. A subgraph's page links to the graph of its version deployed in each environment, and a check of a consumer links the graph from its name.

## Known gaps

- The graph page composes only the deployed subgraphs. The `deployed graph: /graphs/<graph>` of a subgraph's check opens a page that can say the graph composes, while the error comes from the version under check, which only the check composes.
- A subgraph's name in the graph is its participant name. An `@override(from:)` that uses another name, such as the subgraph name of an Apollo router configuration, names no subgraph and is ignored.
