Apollo Connectors Preview Features

Try out the latest features of Apollo Connectors


Want to try out the latest and greatest features of Apollo Connectors? You're in the right place!

Before you jump in, there are a couple important pieces of information:

  • All of these features are subject to change. Use care when updating composition or GraphOS Router.

  • Tell us what you think before these features are stable.

Enable Preview Features

  1. Use GraphOS Router v2.11.0 or later. When running rover dev locally, set this environment variable:

    Bash
    1APOLLO_ROVER_DEV_ROUTER_VERSION=2.11.0
  2. Configure the router to enable the preview version of Connectors (in whatever file you pass for --router-config, typically router.yaml):

    YAML
    1connectors:
    2  preview_connect_v0_4: true
  3. Use the appropriate composition version with rover and Apollo Federation. When running rover locally, configure this version in a YAML file specified with --supergraph-config:

    YAML
    1federation_version: =2.13.0
  4. Update your schemas to use the preview version of Connectors:

    GraphQL
    1extend schema
    2 @link(
    3   url: "https://specs.apollo.dev/connect/v0.4",
    4   import: ["@source", "@connect"]
    5 )

The order of these steps is important. Older versions of the router don't accept the configuration setting, and routers without the configuration reject schemas composed with the latest composition version.

Abstract type support

Connectors v0.4 introduces support for abstract types (interface and union types in GraphQL), enabling type polymorphism in your connector schemas.

What's new

Previously, Connectors required concrete object types. With v0.4, you can:

  • Define fields that return interface types.

  • Define fields that return union types.

  • Use ->match with string literal __typename values to determine the concrete type at runtime.

Interface example

GraphQL
1extend schema
2  @link(
3    url: "https://specs.apollo.dev/connect/v0.4",
4    import: ["@source", "@connect"]
5  )
6
7@source(name: "api", http: { baseURL: "https://api.example.com" })
8
9interface Product {
10  id: ID!
11  title: String!
12  price: Float!
13}
14
15type Book implements Product {
16  id: ID!
17  title: String!
18  price: Float!
19  author: String!
20}
21
22type Movie implements Product {
23  id: ID!
24  title: String!
25  price: Float!
26  director: String!
27}
28
29type Query {
30  products: [Product!]!
31    @connect(
32      source: "api"
33      http: { GET: "/products" }
34      selection: """
35      $.results {
36        ... type->match(
37          ["book", { __typename: "Book", id, title, price, author }],
38          ["movie", { __typename: "Movie", id, title, price, director }]
39        )
40      }
41      """
42    )
43}

Union example

GraphQL
1extend schema
2  @link(
3    url: "https://specs.apollo.dev/connect/v0.4",
4    import: ["@source", "@connect"]
5  )
6
7@source(name: "api", http: { baseURL: "https://api.example.com" })
8
9union SearchResult = Book | Author
10
11type Book {
12  id: ID!
13  title: String!
14}
15
16type Author {
17  id: ID!
18  name: String!
19}
20
21type Query {
22  search(query: String!): [SearchResult!]!
23    @connect(
24      source: "api"
25      http: { GET: "/search?q={$args.query}" }
26      selection: """
27      $.results {
28        ... resultType->match(
29          ["book", { __typename: "Book", id, title }],
30          ["author", { __typename: "Author", id, name }]
31        )
32      }
33      """
34    )
35}

Abstract type resolution

The key to abstract type support is the ->match method combined with the spread operator (...). The ->match method lets you conditionally return different objects based on a discriminator field in the API response (like type or resultType in the preceding examples). Each match case includes:

  • A __typename field set to a string literal (like "Book" or "Author") that identifies the concrete GraphQL type.

  • The fields specific to that type.

The spread operator (...) then merges the matched object's properties into the selection result.

note
The __typename value must be a string literal that exactly matches one of the concrete types implementing the interface or belonging to the union. Using a field reference (like __typename: type) doesn't work because the connector needs specific string literals for static type analysis.
Feedback

Edit in VSCode

Ask Community