|
| 1 | +# PHP GraphQL Utils for graphql-php |
| 2 | + |
| 3 | +[](https://travis-ci.org/simPod/GraphQL-Utils) |
| 4 | +[](https://packagist.org/packages/simpod/graphql-utils) |
| 5 | +[](https://packagist.org/packages/simpod/graphql-utils) |
| 6 | +[](https://packagist.org/packages/simpod/graphql-utils) |
| 7 | +[](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils) |
| 8 | +[](https://scrutinizer-ci.com/g/simPod/GraphQL-Utils) |
| 9 | +[](https://github.com/simPod/GraphQL-Utils/issues) |
| 10 | + |
| 11 | + |
| 12 | +## Contents |
| 13 | +- [Installation](#installation) |
| 14 | +- [Features](#features) |
| 15 | + - [Schema Builders](#schema-builders) |
| 16 | + - [Types](#types) |
| 17 | + - [Error Handling](#error-handling) |
| 18 | + |
| 19 | +## Installation |
| 20 | + |
| 21 | +Add as [Composer](https://getcomposer.org/) dependency: |
| 22 | + |
| 23 | +```sh |
| 24 | +composer require simpod/graphql-utils |
| 25 | +``` |
| 26 | + |
| 27 | +## Features |
| 28 | + |
| 29 | +### Schema Builders |
| 30 | + |
| 31 | +Instead of defining your schema as an array, use can use more objective-oriented approach. |
| 32 | +This library provides set of strictly typed builders that help you build your schema. |
| 33 | + |
| 34 | +#### ObjectBuilder and FieldBuilder |
| 35 | + |
| 36 | +✔️ Standard way with `webonyx/graphql-php` |
| 37 | + |
| 38 | +```php |
| 39 | +$userType = new ObjectType([ |
| 40 | + 'name' => 'User', |
| 41 | + 'description' => 'Our blog visitor', |
| 42 | + 'fields' => [ |
| 43 | + 'firstName' => [ |
| 44 | + 'type' => Type::string(), |
| 45 | + 'description' => 'User first name' |
| 46 | + ], |
| 47 | + 'email' => Type::string() |
| 48 | + ] |
| 49 | +]); |
| 50 | +``` |
| 51 | + |
| 52 | +✨ The same can be produced in objective way |
| 53 | + |
| 54 | +```php |
| 55 | +use SimPod\GraphQLUtils\Builder\ObjectBuilder; |
| 56 | + |
| 57 | +... |
| 58 | + |
| 59 | +$userType = ObjectBuilder::create('User') |
| 60 | + ->setDescription('Our blog visitor') |
| 61 | + ->setFields([ |
| 62 | + FieldBuilder::create('firstName', Type::string()) |
| 63 | + ->setDescription('User first name') |
| 64 | + ->build(), |
| 65 | + FieldBuilder::create('email', Type::string()) |
| 66 | + ->build(), |
| 67 | + ]) |
| 68 | + ->build(); |
| 69 | + |
| 70 | +``` |
| 71 | + |
| 72 | +#### EnumBuilder |
| 73 | + |
| 74 | +✔️ Standard way with `webonyx/graphql-php` |
| 75 | + |
| 76 | +```php |
| 77 | +use SimPod\GraphQLUtils\Builder\EnumBuilder; |
| 78 | + |
| 79 | +... |
| 80 | + |
| 81 | +$episodeEnum = new EnumType([ |
| 82 | + 'name' => 'Episode', |
| 83 | + 'description' => 'One of the films in the Star Wars Trilogy', |
| 84 | + 'values' => [ |
| 85 | + 'NEWHOPE' => [ |
| 86 | + 'value' => 4, |
| 87 | + 'description' => 'Released in 1977.' |
| 88 | + ], |
| 89 | + 'EMPIRE' => [ |
| 90 | + 'value' => 5, |
| 91 | + 'description' => 'Released in 1980.' |
| 92 | + ], |
| 93 | + 'JEDI' => [ |
| 94 | + 'value' => 6, |
| 95 | + 'description' => 'Released in 1983.' |
| 96 | + ], |
| 97 | + ] |
| 98 | +]); |
| 99 | +``` |
| 100 | + |
| 101 | +✨ The same can be produced in objective way |
| 102 | + |
| 103 | +```php |
| 104 | +$episodeEnum = EnumBuilder::create('Episode') |
| 105 | + ->setDescription('One of the films in the Star Wars Trilogy') |
| 106 | + ->addValue(4, 'NEWHOPE', 'Released in 1977.') |
| 107 | + ->addValue(5, 'EMPIRE', 'Released in 1980.') |
| 108 | + ->addValue(6, 'JEDI', 'Released in 1983.') |
| 109 | + ->build(); |
| 110 | +``` |
| 111 | + |
| 112 | +### Types |
| 113 | + |
| 114 | +#### 🕰️ DateTime |
| 115 | + |
| 116 | +scalar type that produces `scalar DateTime` in your schema. |
| 117 | + |
| 118 | +[`SimPod\GraphQLUtils\Type\DateTimeType`](https://github.com/simPod/GraphQL-Utils/blob/master/src/Type/DateTimeType.php) |
| 119 | + |
| 120 | +### Error Handling |
| 121 | + |
| 122 | +Extending your exception with `SimPod\GraphQLUtils\Error\Error` forces you to implement `getType()` method. |
| 123 | + |
| 124 | +Example Error class |
| 125 | + |
| 126 | +```php |
| 127 | +use SimPod\GraphQLUtils\Error\Error; |
| 128 | +use function sprintf; |
| 129 | + |
| 130 | +final class InvalidCustomerIdProvided extends Error |
| 131 | +{ |
| 132 | + public const TYPE = 'INVALID_CUSTOMER_ID_PROVIDED'; |
| 133 | + |
| 134 | + public static function noneGiven() : self |
| 135 | + { |
| 136 | + return new self('No CustomerId provided'); |
| 137 | + } |
| 138 | + |
| 139 | + public function getType() : string |
| 140 | + { |
| 141 | + return self::TYPE; |
| 142 | + } |
| 143 | + |
| 144 | + public function isClientSafe() : bool |
| 145 | + { |
| 146 | + return true; |
| 147 | + } |
| 148 | +} |
| 149 | +``` |
| 150 | + |
| 151 | +Create your formatter |
| 152 | + |
| 153 | +```php |
| 154 | +use SimPod\GraphQLUtils\Error\FormattedError; |
| 155 | + |
| 156 | +$formatError = function formatError(Error $error) : array |
| 157 | +{ |
| 158 | + if (!$error->isClientSafe()) { |
| 159 | + // eg. log error |
| 160 | + } |
| 161 | + |
| 162 | + return FormattedError::createFromException($error); |
| 163 | +}; |
| 164 | + |
| 165 | +$errorFormatterCallback = static function (Error $error) use ($formatError) : array { |
| 166 | + return $formatError($error); |
| 167 | +}; |
| 168 | + |
| 169 | +$config = GraphQL::executeQuery(/* $args */) |
| 170 | + ... |
| 171 | + ->setErrorFormatter($errorFormatterCallback) |
| 172 | + ->setErrorsHandler( |
| 173 | + static function (array $errors, callable $formatter) : array { |
| 174 | + return array_map($formatter, $errors); |
| 175 | + } |
| 176 | + ) |
| 177 | +``` |
| 178 | + |
| 179 | +Error types will then be provided in your response so client can easier identify the error type |
| 180 | + |
| 181 | +```json |
| 182 | +{ |
| 183 | + "errors": [ |
| 184 | + { |
| 185 | + "type": "INVALID_CUSTOMER_ID_PROVIDED", |
| 186 | + "message": "No CustomerId provided", |
| 187 | + "category": "graphql", |
| 188 | + ... |
| 189 | +``` |
0 commit comments