This page describes how interface and union types can be used with neo4j-graphql.js.
GraphQL supports two kinds of abstract types: interfaces and unions. Interfaces are abstract types that include a set of fields that all implementing types must include. A union type indicates that a field can return one of several object types, but doesn't specify any fields that must be included in the implementing types of the union. See the GraphQL documentation to learn more about interface and union types.
Interface types are supported in neo4j-graphql.js through the use of multiple labels in Neo4j. For example, consider the following GraphQL type definitions:
The above GraphQL type definitions would define the following property graph model using neo4j-graphql.js:
Note that the label
Person (which represents the interface type) is added to each node of a type implementing the
Person interface (
When an interface type is included in the GraphQL type definitions, the generated create mutations will add the additional label for the interface type to any nodes of an implementing type when creating data. For example consider the following mutations.
This creates the following graph in Neo4j (note the use of multiple labels):
A query field is addd to the generated
Query type for each interface. For example, querying using our
__typename introspection field
__typename introspection field can be added to the selection set to determine the concrete type of the object.
Inline fragments can be used to access fields of the concrete types in the selection set.
Filtering With Interfaces
The generated filter arguments can be used for interface types. Note however that only fields in the interface definition are included in the generated filter arguments as those apply to all concrete types.
Interface Relationship Fields
We can also use interfaces when defining relationship fields. For example:
Note that using union types for relationship types is not yet supported by neo4j-graphql.js. Unions can however be used on relationship fields.
Union types are abstract types that do not specify any fields that must be included in the implementing types of the union, therefore it cannot be assumed that the concrete types of a union include any overlapping fields. Similar to interface types, in neo4j-graphql.js an additional label is added to nodes to represent the union type.
For example, consider the following GraphQL type definitions:
Using the generated mutations to create the following data:
The above mutations create the following data in Neo4j. Note the use of multiple node labels.
A query field is added to the Query type for each union type defined in the schema.
Inline fragments are used in the selection set to access fields of the concrete type.
Using With @cypher Directive Query Fields
We can also use unions with
@cypher directive fields. Unions are often useful in the context of search results, where the result object might be one of several types. In order to support this usecase full text indexes can be used to search across multiple node labels and properties.
First, let's create a full text index in Neo4j. This index will include the
Now we can add a
search field to the Query type that searches the full text index.
Now we can query the
search field, leveraging the full text index.
- Using Neo4j’s Full-Text Search With GraphQL -- Defining Custom Query Fields Using The Cypher GraphQL Schema Directive