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.8.0-preview.0 or greater. When running
rover devlocally, set this environment variable:Bash1APOLLO_ROVER_DEV_ROUTER_VERSION=2.8.0-preview.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_3: trueUse composition v2.12.0-preview.8 or greater with
roverand the Federation Next build pipeline in Studio. When runningroverlocally, this version can be configured in a YAML file specified with--supergraph-config:YAML1federation_version: =2.12.0-preview.8Update your schemas to use the preview version of Connectors:
GraphQL1extend schema 2 @link( 3 url: "https://specs.apollo.dev/connect/v0.3", 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.
The $env variable
The $env variable is a new feature for accessing environment variables in your Connectors. This is useful for API keys or other configuration values that you don't want to hard-code into your schema.
New methods and operators
Array methods
| Method | Description |
|---|---|
find | Returns the first item in the array that matches the criteria outlined in the first argument of find. |
filter | Returns a new array containing all array items that match the criteria outlined in the first argument of filter. |
get | For a string, returns the character at the specified index. For an array, returns the item at the specified index. For an object, returns the property with the specified name. |
Logical operators
| Method | Description |
|---|---|
ne | Returns true if the value is not equal to the argument. |
in | Returns true if the list of arguments contains the value. |
contains | Returns true if the array contains the argument value. |
lte | Returns true if the value is less than or equal to the argument value. |
gt | Returns true if the value is greater than the argument value. |
lt | Returns true if the value is less than the argument value. |
gte | Returns true if the value is greater than or equal to the argument value. |
or | Returns true if the value or any of the arguments are true. |
not | Inverts the boolean of the value. true becomes false, and false becomes true. |
eq | Returns true if the value is equal to the argument. |
and | Given two or more values to compare, returns true if all of the values are true. |
Math methods
| Method | Description |
|---|---|
mod | Divides the value by the argument and returns the remainder. |
sub | Subtracts the argument from the value. |
add | Adds the value and the argument. |
div | Divides the value by the argument. |
mul | Multiplies the value by the argument. |
Type coercion
| Method | Description |
|---|---|
parseInt | Converts a value to an Int. |
toString | Converts an input to a String. |
The ?? and ?! coalescing operators
The new ?? and ?! nullish- and None-coalescing operators enable providing
reliable default expressions when an expression to the left of the ?? or ?!
evaluates to null or None (as the ?? operator does) or only None (as the
?! operator does):
1people: $(people ?? [])
2bOrDefault: $(b ?? "default")
3default: $(null ?? "default")
4alsoDefault: $(missing ?? "default")
5multiple: $(this ?? that ?? null)
6true: $(true ?? "default")
7safeSlice: array->slice($args.sliceStart ?? 0, $args.sliceEnd ?? 10)
8nullAllowed: $(null ?! "default")
9stillDefault: $(missing ?! "default")See the section on defaulting values for more information on these operators.
isSuccess for handling non-20x responses
By default, Apollo Connectors map successful 20X HTTP responses to the data property of the GraphQL response, and map other status codes to the errors property. The isSuccess argument for both @source and @connect directives enables you to customize this behavior.
The isSuccess expression uses the Connectors mapping language and must evaluate to a boolean. If the result is true, the router applies the selection mapping to the response and merges the result to the data property. If it's false, the router applies the errors.message and errors.extensions mappings and appends the result to the errors property.
You can use expressions like isSuccess: "$status->match([200, true], [400, true], [@, false])" to map the response to data for specific status codes.
Here is an example that will never result in a GraphQL error by setting isSuccess to true:
type Mutation {
createProduct: CreateProductResponse!
@connect(
http: { GET: "https://api.dev/endpoint-that-can-fail" }
isSuccess: "$(true)"
selection: """
product { id }
errors { message }
"""
)
}
type CreateProductResponse {
product: Product
errors: [Error]
}Learn more about error handling with Connectors.