Extensions to GraphQL

The API Gateway extends the GraphQL specification with the following additional schema directives.

@cost

The @cost directive associates weight values with types or fields, to be used when calculating the type cost and field cost, respectively, of a request to the GraphQL API.

Syntax

@cost(weight: value)

@listSize

The @listSize directive applies slicing arguments to fields.

Syntax

@listSize(slicingArguments: [String], requireOneSlicingArgument: Boolean, assumedSize: value, sizedFields: [String])

Nested slicing arguments

A GraphQL schema can also specify slicing arguments that target nested fields within complex input object types.

Example

Consider the following Filter input object type.
input Filter {
  max: Int
  onlyActiveEntries: Boolean
}

Assuming that the max field of the Filter object references a slicing argument that is a scalar value, nested slicing arguments can be defined, as in the following example.

type Query {
  users(filter: Filter): [User] @listSize(slicingArguments: ["filter.max"])
}

The following query specifies the nested slicing argument. The cost analysis assumes that the field Query.users returns a list of 10 users.

querynestedSlicing {
  users(filter: {max: 10}) {
    name
  }
}

Specifying default values for slicing arguments

Default values can be defined for both scalar and complex input types. If no other value is explicitly defined in a query, the cost analysis uses default values.

Note that if a slicing argument has a default value defined and the requireOneSlicingArgument is set to true, queries are allowed to omit that slicing argument.

Example

Consider the following Filter input object type.
input Filter {
  max: Int
  onlyActiveEntries: Boolean
}

The argument Query.users.max and the input field filter.max both specify default values of 10.

type Query {
  users(max: Int = 10): [User]
  filteredUsers(filter: Filter = {max: 10}): [User]
}

@remove

The @remove directive specifies conditions for removing types and fields from validation or introspection for each transaction based on values in the API context.

Syntax

@remove(if: [String]) on SCALAR | OBJECT | FIELD_DEFINITION | ARGUMENT_DEFINITION | INTERFACE | UNION | ENUM | ENUM_VALUE | INPUT_OBJECT | INPUT_FIELD_DEFINITION

Limitations and propagation rules

For limitations and propagation rules, see Limitations and propagation rules for the GraphQL @remove directive.

Example 1

Remove the creditCard type if the current API plan is bronze.
type creditCard @remove(if: ["plan.name in [\"bronze\"]"])

Example 2

Remove the creditCard type if the current API plan is not gold, and remove the field card1 if the API plan is bronze.
type creditCard @remove(if: ["$not(plan.name = \"gold\")"]){
   field1: String
 }
 type User {
   card1: creditCard @remove(if: ["plan.name = 'bronze'", "$not(plan.name = \gold\")"])
 }