From 2fee9cca690bcb66a799f1a8c9284b230409cfc4 Mon Sep 17 00:00:00 2001 From: Simon Gadient Date: Wed, 3 Feb 2021 11:08:49 +0100 Subject: [PATCH] TASK: documentation for interface, union, enum and scalar --- Readme.md | 132 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) diff --git a/Readme.md b/Readme.md index 8169ca1..d602a43 100644 --- a/Readme.md +++ b/Readme.md @@ -253,6 +253,138 @@ All resolver methods share the same signature: method($source, $args, $context, $info) ``` +#### Interface Types + +When working with interfaces, you need Resolvers for your interfaces as well. Given this schema: + +```graphql schema +interface Person { + firstName: String! + lastName: String! +} + +type Customer implements Person { + firstName: String! + lastName: String! + email: String! +} + +type Supplier implements Person { + firstName: String! + lastName: String! + phone: String +} +``` + +You need to configure a `Person` Resolver as well as Resolvers for the concrete implementations. + +While the concrete Type Resolvers do not need any special attention, the `Person` Resolver implements the +`__resolveType` function returning the type names: + +```php +isActive() ? 'ACTIVE' : 'INACTIVE'; +} +``` + +#### Scalar Types + +Types at the leaves of a Json tree are defined by Scalars. Own Scalars are implemented by a Resolver +implementing the functions `serialize()`, `parseLiteral()` and `parseValue()`. + +This example shows the implementation of a `DateTime` Scalar Type. For the given Schema definition: + +```graphql schema +scalar DateTime +``` + +The `DateTimeResolver` looks the following when working with Unix timestamps: + +```php +getTimestamp(); + } + + public function parseLiteral(Node $ast): ?DateTime + { + if ($ast instanceof IntValueNode) { + $dateTime = new DateTime(); + $dateTime->setTimestamp((int)$ast->value); + return $dateTime; + } + return null; + } + + public function parseValue(int $value): DateTime + { + $dateTime = new DateTime(); + $dateTime->setTimestamp($value); + return $dateTime; + } +} +``` + +You have to make the `DateTimeResolver` available again through one of the configuration options in the Settings.yaml. + ### Context The third argument in your Resolver method signature is the Context.