Docs / GraphQL

GraphQL resources

How a GraphQL provider publishes its schema, how a consumer declares the queries and mutations it calls, how GraphQL types become schemas and compare, the publish errors, and what a GraphQL contract does not compare.

On this page

A graphql block declares GraphQL resources. Each field of the query or the mutation root is two resources: its arguments, which the consumer sends and the provider reads, and its returns, which the provider produces and the consumer reads. Each one is compared on its own. What holds for every kind of resource is in The contract file.

The two sides write it differently. The provider publishes the GraphQL schema its application already has, and the broker reads its resources from it. The consumer writes in YAML the fields it calls, with the arguments it sends and the fields it reads.

A report names a GraphQL resource by its operation and field, as query pet, and a place in it by arguments or returns, as in query pet returns at "$.name" (Breaks).

The provider publishes its schema#

petstore_api serves pets over GraphQL. Its schema is the file its server reads or prints:

graphql
type Query {
  pet(id: ID!): Pet
  pets(status: Status): [Pet!]!
}

type Mutation {
  adoptPet(petId: ID!): Pet!
}

scalar DateTime

enum Status {
  AVAILABLE
  SOLD
}

type Pet {
  id: ID!
  name: String!
  status: Status!
  bornAt: DateTime
  reviews(first: Int): [Review!]!
}

type Review {
  stars: Int!
}

It publishes the file, with no YAML:

terminal
$ bidirekt publish schema.graphqls --participant petstore_api --version 1.4.0
petstore_api contract publish successful
  • A schema file ends in .graphql, .graphqls or .gql. Every schema file of one publish is one schema: the types may live in one file and the Query in another, and a type may be extended in a file other than the one that defines it. Passed alone, a file that names a type another file defines fails with ./api/query.graphqls:2:17: Undefined type Pet..
  • Each field of Query and Mutation, or of the roots that schema { query: … mutation: … } names, is a resource the participant provides. Subscription is ignored.
  • Schema files publish together with YAML files in the same command: a participant that also consumes, or that provides REST, passes its YAML next to its schema. Under provides, the graphql block takes only graph, for a subgraph (Graphs); query and mutation there are unknown keys, since the schema already declares them.
  • The types of the schema are a namespace of their own: a YAML schema Pet and a GraphQL type Pet in the same contract never collide.
  • A type declared only by extend, as extend type Query { pet: Pet } with no type Query, is defined by its extensions, as a GraphQL server reads it. Two extensions of it merge.
  • Every type is stored, also one no field reaches. Changing it makes a new snapshot and breaks no consumer.

The broker reads the schema with the rules of GraphQL. A schema that GraphQL rejects is refused with the position and the text of the error, after cannot post contract to broker:. The usual ones:

  • A type, a field or a directive that no file defines: ./api/query.graphqls:2:17: Undefined type Pet..
  • A file of client operations or fragments in place of the schema: ./operations.graphql: holds client operations; publish expects the GraphQL schema. Cut off, it gives the place where it ends: ./broken.graphql:5:1: Expected Name, found <EOF> in client operations; publish expects the GraphQL schema.

The consumer declares what it calls#

petstore_web calls three fields of petstore_api. Under query and mutation, each key is a field of the root, with arguments, the schema of the arguments it sends, and returns, the schema of what it reads:

yaml
consumes:
  petstore_api:
    graphql:
      query:
        pet:
          arguments: PetArgs
          returns: MaybePet
        pets:
          returns: PetList
      mutation:
        adoptPet:
          arguments: AdoptArgs
          returns: Pet

schemas:
  PetArgs:
    type: object
    properties:
      id:
        type: string
  AdoptArgs:
    type: object
    properties:
      petId:
        type: string
  MaybePet:
    ref: Pet
    nullable: true
  PetList:
    type: array
    items:
      ref: Pet
  Pet:
    type: object
    properties:
      name:
        type: string
      status:
        type: enum
        members: [AVAILABLE]
      bornAt:
        type: scalar
        name: DateTime
        nullable: true
      reviews:
        type: array
        items:
          ref: Review
        arguments: ReviewsArgs
  Review:
    type: object
    properties:
      stars:
        type: integer
  ReviewsArgs:
    type: object
    properties:
      first:
        type: integer
  • The key is the field name alone, as the schema writes it. Arguments go in arguments, never in the key.
  • returns is required: every call reads something.
  • Without arguments, the call sends no argument, so a provider that starts requiring one breaks it.
  • arguments names an object schema that is not nullable, with one property per argument the call sends.
  • The nullability and the list at the top of returns live in the schema it names: MaybePet reads pet: Pet, which may be null, and PetList reads pets: [Pet!]!.
  • The consumer declares only the fields it selects, at every level.
  • The name under consumes is the provider's participant name, or the name of a graph (Graphs).
  • subscription under graphql, and graph under consumes.<provider>.graphql, are unknown keys.
  • A root field that starts with __, such as __schema or __type, is introspection, served by the GraphQL library and not by the provider's contract. It publishes and is never stored, compared or broken.
  • __typename is read as an enum of the type names the consumer reads, or as a string for any type name (Reading the type name).

Several schemas for one field#

Two parts of a consumer may call the same field with different arguments, or read different fields of it. returns and arguments then take a list of schemas, and ~ in arguments stands for a call that sends no argument:

yaml
consumes:
  petstore_api:
    graphql:
      query:
        pets:
          arguments: [~, PetsArgs]
          returns: [PetList, PetNameList]

A list publishes and compares exactly like the same field declared once per schema in separate files (In several files): each entry is checked on its own, so ~ breaks when the provider starts requiring an argument, and two entries that give the same property different types or arguments are rejected at publish. A single schema is still written as a plain name.

Fields with arguments#

A field the call passes arguments to, as reviews(first: 10), carries arguments beside its type, naming the object schema of those arguments, as reviews above. arguments goes only on a property.

The arguments of a field are compared whenever either side declares them. The side that declares none sends none, so a provider that adds reviews(first: Int!) breaks a consumer that reads reviews without arguments.

Scalars#

A custom scalar is type: scalar with its name, as bornAt above. It matches only the scalar of the same name: DateTime and Date are different types. The built-in scalars are primitives, written as in the table below, or as type: scalar with their name, ID, String, Int, Float or Boolean, to declare that exact type (Type names).

type: scalar, arguments and name belong to GraphQL. In a schema that a REST resource reaches, they are rejected at publish; in a schema that no resource reaches, they are accepted and never compared.

Type names#

A consumer whose code names a GraphQL type, as a query that declares the variables $id: ID! or $status: Status, declares that name with name. GraphQL rejects such a query when the provider's argument has another type, even one that accepts the same values:

yaml
schemas:
  PetArgs:
    type: object
    properties:
      id:
        type: scalar
        name: ID
  PetsArgs:
    type: object
    properties:
      status:
        type: enum
        name: Status
        members: [AVAILABLE]
  • name goes on type: scalar, type: object and type: enum, in what a field returns and in its arguments.
  • A node with name matches only a provider type with that name and of the same kind: a scalar a scalar, an object an object or an interface, an enum an enum. Otherwise it breaks, as Query.pet(id:) β€” petstore_web sends it as ID!, but petstore_api expects String! (Breaks).
  • An argument with name is compared with the provider's argument as it is: {type: scalar, name: ID} against [ID!]! breaks. Without name, a single value still fits a list.
  • A node without name never breaks when the provider renames a type and keeps its shape. type: string accepts both String and ID.
  • The schema that arguments names is not a GraphQL type, so it takes no name.
  • It is compared in the consumer's check and in the provider's, against a provider called directly or a graph.

How GraphQL types become schemas#

The broker turns the provider's schema into the schemas a consumer writes:

GraphQLSchema
Stringstring
IDstring
Intinteger
Floatfloat
Booleanboolean
scalar DateTimetype: scalar, name: DateTime
type, inputobject
enumenum
unionunion, each member keyed by its type name
interfaceunion of the types that implement it, with the interface's own fields as properties
an output field without !nullable, never optional
an argument or an input field without !optional and nullable
an argument or an input field with ! and a default, as first: Int! = 10optional, never nullable
[Review!]!an array that is not nullable, of items that are not nullable

A consumer reads a Float as float, or as number when its code accepts any number (Primitives).

Unions and interfaces#

A union is read as a union whose member keys are type names of the provider's schema. The consumer lists the members it reads, each with the schema it reads from that type; a member it does not list never breaks.

An interface is read in one of three ways:

  • as an object, with the fields of the interface: node { id } is type: object with id;
  • as a union of the types that implement it: node { ... on User { name } } is type: union with the member User;
  • both at once, as Relay reads it: node { id ... on User { name } } is a union with properties, the fields read on the interface, and members, the types read with ... on:
yaml
Node:
  type: union
  properties:
    id:
      type: string
  members:
    User:
      ref: UserName
    Pet:
      ref: PetSpecies

A field that leaves the interface breaks a consumer that reads it on the interface, and a type that stops implementing it breaks a consumer that reads it as a member. A union with properties exists only in GraphQL: in a schema that a REST resource reaches, it is rejected at publish.

Reading the type name#

A consumer that reads __typename declares it as an enum of the type names it reads, written in place:

yaml
schemas:
  Pet:
    type: object
    properties:
      __typename:
        type: enum
        members: [Pet]
      name:
        type: string

or as a ref to an enum schema, which compares the same:

yaml
schemas:
  Pet:
    type: object
    properties:
      __typename:
        ref: PetTypename
      name:
        type: string
  PetTypename:
    type: enum
    members: [Pet]

The provider's schema has no __typename to write: at each check, the broker takes it from the provider's type. An object gives its own name, and a union or an interface gives the names of its members or of the types that implement it, without the @inaccessible ones when the provider is a graph. The names then compare as the values of any enum (Enums and unions). With petstore_api 1.4.0 deployed after renaming Pet to Animal, with the same fields, petstore_web breaks:

can-i-deploy output
petstore_api (1.4.0, deployed):
  Pet.__typename β€” petstore_web reads "Pet", but petstore_api doesn't provide it
    β†’ petstore_web: stop reading "Pet"
    β†’ petstore_api: provide "Pet" there
      Β· query pet returns at "$.__typename" (petstore_api: Animal.__typename)

An object that reads only __typename reads a union too: search { __typename }, with __typename: {type: enum, members: [Pet, Toy]}, breaks when SearchResult no longer has Pet. With petstore_api 2 deployed after renaming Pet, a member of SearchResult, to Animal, a consumer whose search returns a list of SearchHit breaks:

can-i-deploy output
petstore_api (2, deployed):
  SearchHit.__typename β€” petstore_web reads "Pet", but petstore_api doesn't provide it
    β†’ petstore_web: stop reading "Pet"
    β†’ petstore_api: provide "Pet" there
      Β· query search returns at "$[].__typename" (petstore_api: SearchResult.__typename)

A consumer that does not declare __typename is not affected by a rename that keeps the fields.

A consumer that reads any type name declares __typename as type: string, in place or through a ref. It never breaks: not when the provider renames the type, and not when a union loses a member.

__typename with another type, such as type: object or type: integer, is rejected at publish with "__typename" must be an enum or a string schema at schemas Pet: it is object, list the type names it reads in an enum's "members", or read any type name as a string, and __typename in a schema that a REST resource reaches with "__typename" is only allowed in GraphQL contracts at schemas Pet.

How types compare#

The rules of Which side decides hold, with the rules of GraphQL on top:

  • A field read must exist. A field the consumer reads, even one it marks optional, breaks when the provider no longer has it: selecting a field the type does not have invalidates the whole query.
  • Input is closed. GraphQL rejects an argument or an input field it does not know, so one the consumer sends and the provider does not declare breaks, even when the consumer marks it optional.
  • Nullability. An output field that becomes nullable breaks a consumer that does not accept null. An argument that becomes non-null breaks a consumer that may send null. A new required argument breaks a consumer that does not send it.
  • Custom scalars match by name.
  • Enums and unions compare as in How they compare: every value and member the consumer declares must be declared by the provider.
  • Input coercion. In an argument or an input field, an integer or a number sent to a Float passes, and so does an integer sent to an ID. A single value sent to a list passes too: a string sent to [String!]. An integer sent to a String still breaks.
  • Type names are compared only where the consumer declares them: in name (Type names), in __typename (Reading the type name) and as the members of a union. Renaming a type that keeps its shape breaks no other consumer.

A provider that changes an output field from Float to Int breaks a consumer that reads it as float, but not one that reads it as number.

In several files#

The schema files of a provider are one schema, so a type defined in two of them is refused by GraphQL: b.graphqls:1:6: Cannot redeclare type Query.. A consumer may declare the same field in several YAML files, or with a list in returns and arguments (Several schemas for one field), and they merge as in Several files. Two files that give the same property different arguments are rejected at publish:

publish error
contract validation failed
  - petstore_web.yaml: conflicting arguments for property "$.reviews" of consumes petstore_api query pet: ReviewsArgs here, none in other.yaml

Publish errors#

Each line below is printed after the file's name, as in publish:

  • type "scalar" is only allowed in GraphQL contracts at schemas Pet properties bornAt: a scalar in a schema that a REST resource reaches.
  • "arguments" is only allowed in GraphQL contracts at schemas Pet properties reviews: arguments in a schema that a REST resource reaches.
  • "arguments" must name an object schema at consumes petstore_api graphql query pet: PetId is string, and "arguments" must name a non-nullable schema at consumes petstore_api graphql query pet: MaybeArgs is nullable: an arguments, of a field called or of a property, whose ref chain does not end in an object, or passes through a nullable one.
  • scalar schema without name at schemas Pet properties bornAt: type: scalar without name.
  • "name" is not allowed on a string schema at schemas PetArgs properties id, use it with "type: scalar", "type: object" or "type: enum" (GraphQL contracts only): name on a string, array, map or union, and on an object or enum in a schema that a REST resource reaches.
  • "arguments" must name a schema without "name" at consumes petstore_api graphql query pet: PetArgs names the GraphQL type PetInput, a field's arguments are not a GraphQL type: remove its "name": name on the schema that arguments names.
  • missing "returns" at consumes petstore_api graphql query pet: a field called without returns.
  • "properties" is not allowed on a union schema at schemas Pet properties node, use it with "type: object": a union with properties in a schema that a REST resource reaches.
  • invalid GraphQL field name "pets(first: 10)" at consumes petstore_api graphql query, write the field name only; arguments go in "arguments": a key under query or mutation that is not a GraphQL name, such as "pets(first: 10)": {returns: PetList}.

The errors of a subgraph's graph are in Graphs.

What is not compared#

A change in any of the items below never makes can-i-deploy fail. The items that hold for every kind of resource are in The contract file.

  • Descriptions in the schema.
  • Default values. A default only makes an argument or an input field optional; its value is not stored.
  • Directives that are behavior: custom directives, access directives such as @authenticated, @requiresScopes and @policy, @deprecated and @specifiedBy. They are accepted and ignored once the schema defines them or imports them through @link; one it does neither is refused by GraphQL: petstore_api.graphqls:3:22: Undefined directive authenticated.. Only the federation directives of a subgraph count (Graphs).
  • Arguments marked @fromContext. The router fills them, and a client never sends them, so they are left out of the contract.
  • Subscriptions. A Subscription in the schema is ignored, and subscription in the YAML is rejected.
  • @oneOf. An input marked @oneOf publishes and is read as a plain input, with every field optional and nullable: a consumer that sends two of its fields, or a null one, passes.
  • The client's operations. Bidirekt compares schema against schema, never a query against the schema, so @defer, @stream, fragments and variables have no place in a contract.
  • Introspection, __schema and __type.