You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: README.md
+86-2
Original file line number
Diff line number
Diff line change
@@ -38,8 +38,8 @@ It could be hard to believe, but give it a try and you'll be rewarded with much
38
38
*[Lists](#lists)
39
39
*[Input Objects](#input-objects)
40
40
*[Non-Null](#non-null)
41
-
*[Mutation structure](#mutation-structure)
42
-
*[Schema validation](#schema-validation)
41
+
*[Building your schema](#building-your-schema)
42
+
*[Mutation helper class](#mutation-helper-class)
43
43
*[Useful information](#useful-information)
44
44
*[GraphiQL tool](#graphiql-tool)
45
45
@@ -996,7 +996,91 @@ So far we've been working mostly on the request that does not require you to sen
996
996
In order to properly handle and validate that data GraphQL type system provides you an `InputType`. By default all the `Scalar` types are input but if you want to have a single more complicated input type you need to extend an `InputObjectType`.
997
997
998
998
Let's go ahead and create a `PostInputType` that could be used to create a new Post in our system.
999
+
```php
1000
+
<?php
1001
+
/**
1002
+
* PostInputType.php
1003
+
*/
1004
+
1005
+
namespace Examples\Blog\Schema;
1006
+
1007
+
1008
+
use Youshido\GraphQL\Type\Config\InputTypeConfigInterface;
1009
+
use Youshido\GraphQL\Type\NonNullType;
1010
+
use Youshido\GraphQL\Type\Object\AbstractInputObjectType;
1011
+
use Youshido\GraphQL\Type\Scalar\StringType;
1012
+
1013
+
class PostInputType extends AbstractInputObjectType
1014
+
{
1015
+
1016
+
public function build(InputTypeConfigInterface $config)
1017
+
{
1018
+
$config
1019
+
->addField('title', new NonNullType(new StringType()))
1020
+
->addField('summary', new StringType());
1021
+
}
1022
+
1023
+
1024
+
}
1025
+
```
1026
+
1027
+
This `InputType` could be used to create a new mutation (we can do it in the `BlogSchema::build` for testing):
1028
+
```php
1029
+
<?php
1030
+
$config->getMutation()->addFields([
1031
+
'likePost' => new LikePost(),
1032
+
'createPost' => [
1033
+
'type' => new PostType(),
1034
+
'args' => [
1035
+
'post' => new PostInputType(),
1036
+
'author' => new StringType()
1037
+
],
1038
+
'resolve' => function($value, $args, $type) {
1039
+
return DataProvider::getPost(10);
1040
+
}
1041
+
]
1042
+
]);
1043
+
```
999
1044
1045
+
Try to execute the following mutation so you can see the result:
> The best way to see the result of your queries/mutations and to inspect the Schema is to use a [GraphiQL tool](#graphiql-tool)
1054
+
1055
+
### Non Null
1056
+
1057
+
`NonNullType` is really simple to use – consider it as a wrapper that can insure that your field / argument is required and being passed to the resolve function.
1058
+
We have used this type many times already so we'll just show you two methods that might be useful in your resolve functions:
1059
+
-`getNullableType()`
1060
+
-`getNamedType()`
1061
+
1062
+
These two can return you a type that was wrapped up in the `NonNullType` so you can get it's fields, arguments or name.
1063
+
1064
+
## Building your schema
1065
+
1066
+
It's always a good idea to give your heads up about any possible errors as soon as possible, better on the development stage.
1067
+
For this purpose specifically we made a lot of Abstract classes that will force you to implement the right methods to reduce amount of errors or, if you're lucky enough – to have none of them.
1068
+
If you want to implement a new type consider extending the following classes:
1069
+
* AbstractType
1070
+
* AbstractScalarType
1071
+
* AbstractObjectType
1072
+
* AbstractMutationObjectType
1073
+
* AbstractInputObjectType
1074
+
* AbstractInterfaceType
1075
+
* AbstractEnumType
1076
+
* AbstractListType
1077
+
* AbstractUnionType
1078
+
* AbstractSchemaType
1079
+
1080
+
### Mutation helper class
1081
+
Usually you can create a mutation buy extending `AbstractObjectType` or by creating a new field of `ObjectType` inside your `Schema::build` method.
1082
+
It is crucial for the class to have a `getType` method returning the actual OutputType of your mutation.
1083
+
There's a class called `AbstractMutationObjectType` that will help you to not forget about OutputType by forcing you to implement a method `getOutputType` that will eventually be used by internal `getType` method.
0 commit comments