* DOCS: Add new graphql 4 docs * Reorganise docs * Docs done * Basic graphql index page * TOC for getting started * show folders on graphql index page * Add middleware note * Docs update * Update docs to reflect flushless schema * Docs updates * Docs for getByLink * Query caching docs * Docs on nested operations * update docs for new graphql dev admin * Docs for configurable operations * Replace readSiteTrees with readPages * Schema defaults docs * Docs for inherited plugins * Docs for customising * * Docs for field whitelisting * Change whitelist word * New docs on modelConfig * Document dev/build extension * Document default/global plugins * Document new input type fields config * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * Note about when procedural schema gets built * Fix link * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * DOCS Note about plugins in custom queries * DOCS Note about filter and custom resolvers * DOCS Note about canview paging * DOCS Updated guidance on _extend See https://github.com/silverstripe/silverstripe-graphql/issues/296 * Apply suggestions from code review Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> * DOCS Pre-release warning Co-authored-by: Ingo Schommer <ingo@silverstripe.com> Co-authored-by: Andre Kiste <bergice@users.noreply.github.com> Co-authored-by: Ingo Schommer <me@chillu.com>
3.0 KiB
title | summary |
---|---|
Enums, unions, and interfaces | Add some non-object types to your schema |
Working with generic types
[CHILDREN asList]
[alert] You are viewing docs for a pre-release version of silverstripe/graphql (4.x). Help us improve it by joining #graphql on the Community Slack, and report any issues at github.com/silverstripe/silverstripe-graphql. Docs for the current stable version (3.x) can be found here [/alert]
Enums, unions, and interfaces
In more complex schemas, you may want to define types that aren't simply a list of fields, or "object types." These include enums, unions and interfaces.
Enum types
Enum types are simply a list of string values that are possible for a given field. They are
often used in arguments to queries, such as {sort: DESC }
.
It's very easy to add enum types to your schema. Just use the enums
section of the config.
app/_graphql/schema.yml
enums:
SortDirection:
DESC: Descending order
ASC: Ascending order
Interfaces
An interface is a specification of fields that must be included on a type that implements it.
For example, an interface Person
could include firstName: String
, surname: String
, and
age: Int
. The types Actor
and Chef
would implement the Person
interface. Actors and
chefs must have names and ages.
To define an interface, use the interfaces
section of the config.
app/_graphql/schema.yml
interfaces:
Person:
fields:
firstName: String!
surname: String!
age: Int!
resolveType: [ 'MyProject\MyResolver', 'resolvePersonType' ]
Interfaces must define a resolveType
resolver method to inform the interface
which type it is applied to given a specific result. This method is non-discoverable and
must be applied explicitly.
public static function resolvePersonType($object): string
{
if ($object instanceof Actor) {
return 'Actor';
}
if ($object instanceof Chef) {
return 'Chef';
}
}
Union types
A union type is used when a field can resolve to multiple types. For example, a query for "Articles" could return a list containing both "Blog" and "NewsStory" types.
To add a union type, use the unions
section of the configuration.
app/_graphql/schema.yml
unions:
Article:
types: [ 'Blog', 'NewsStory' ]
typeResolver: [ 'MyProject\MyResolver', 'resolveArticleUnion' ]
Like interfaces, unions need to know how to resolve their types. These methods are also non-discoverable and must be applied explicitly.
public static function resolveArticleUnion(Article $object): string
{
if ($object->category === 'blogs')
return 'Blog';
}
if ($object->category === 'news') {
return 'NewsStory';
}
}
Further reading
[CHILDREN]