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
Use GraphOS Router v2.11.0 or later. When running
rover devlocally, set this environment variable:Bash1APOLLO_ROVER_DEV_ROUTER_VERSION=2.11.0Configure the router to enable the preview version of Connectors (in whatever file you pass for
--router-config, typicallyrouter.yaml):YAML1connectors: 2 preview_connect_v0_4: trueUse the appropriate composition version with
roverand Apollo Federation. When runningroverlocally, configure this version in a YAML file specified with--supergraph-config:YAML1federation_version: =2.13.0Update your schemas to use the preview version of Connectors:
GraphQL1extend 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
interfacetypes.Define fields that return
uniontypes.Use
->matchwith string literal__typenamevalues to determine the concrete type at runtime.
Interface example
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
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
__typenamefield 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.
__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.