close

Filter

loading table of contents...

Headless Server Developer Manual / Version 2110

Table Of Contents

4.4 The @inherit Directive

Inheritance relationships between interfaces or object types may be expressed with the @inherit directive. This obviates the need to repeat fields of supertypes or interfaces in subtypes or subinterfaces, respectively.

As an example, define an interface Shape with a field area, and a subinterface Circle which inherits the area field and adds another field radius to the interface type:

interface Shape  {
  area: Float!
}

interface Circle @inherit(from: "Shape") {
  radius: Float!
}

In effect, the Circle interface includes both fields. The @inherit directive works similarly for object types.

You might be surprised that the GraphQL SDL language itself does not support field inheritance in some way. So far, the GraphQL language designers rejected the introduction of such a language feature. They argue that this would violate a fundamental design goal of GraphQL, namely to favor readability over writability.

This is debatable, as with the absence of field inheritance you have to repeat each field of all supertypes in each subtype, and the fact that the same field occurs in multiple types in exactly the same way has to be inferred by the reader. The content schema makes heavy use of inheritance in order to mirror the inheritance relationships within the document type model. CoreMedia found that this improves the readability of the schema and is less error prone when modifying the schema.

However, if you do not like the @inherit directive, don't use it. You can achieve exactly the same effect by copying field definitions to each related type. This is semantically equivalent to what the implementation of the @inherit directive does when the schema definition file is parsed: it adds all fields of supertypes or superinterfaces to the subtype or subinterface, respectively, to the internal representation of the schema. When this schema is then queried by a client like GraphiQL (by an introspective query), this expansion has already taken place, and there are no more @inherit directives in the schema visible to clients.

In the Headless Server, a GraphQL schema file is parsed by an extended graphql.schema.idl.SchemaParser that adds support for this @inherit directive.

Search Results

Table Of Contents
warning

Your Internet Explorer is no longer supported.

Please use Mozilla Firefox, Google Chrome, or Microsoft Edge.