Skip to content

Commit

Permalink
finished beta version of documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
viniychuk committed May 6, 2016
1 parent dba5eb0 commit 461e07f
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 23 deletions.
88 changes: 86 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ It could be hard to believe, but give it a try and you'll be rewarded with much
* [Lists](#lists)
* [Input Objects](#input-objects)
* [Non-Null](#non-null)
* [Mutation structure](#mutation-structure)
* [Schema validation](#schema-validation)
* [Building your schema](#building-your-schema)
* [Mutation helper class](#mutation-helper-class)
* [Useful information](#useful-information)
* [GraphiQL tool](#graphiql-tool)

Expand Down Expand Up @@ -996,7 +996,91 @@ So far we've been working mostly on the request that does not require you to sen
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`.
Let's go ahead and create a `PostInputType` that could be used to create a new Post in our system.
```php
<?php
/**
* PostInputType.php
*/

namespace Examples\Blog\Schema;


use Youshido\GraphQL\Type\Config\InputTypeConfigInterface;
use Youshido\GraphQL\Type\NonNullType;
use Youshido\GraphQL\Type\Object\AbstractInputObjectType;
use Youshido\GraphQL\Type\Scalar\StringType;

class PostInputType extends AbstractInputObjectType
{

public function build(InputTypeConfigInterface $config)
{
$config
->addField('title', new NonNullType(new StringType()))
->addField('summary', new StringType());
}


}
```
This `InputType` could be used to create a new mutation (we can do it in the `BlogSchema::build` for testing):
```php
<?php
$config->getMutation()->addFields([
'likePost' => new LikePost(),
'createPost' => [
'type' => new PostType(),
'args' => [
'post' => new PostInputType(),
'author' => new StringType()
],
'resolve' => function($value, $args, $type) {
return DataProvider::getPost(10);
}
]
]);
```
Try to execute the following mutation so you can see the result:
```
mutation {
createPost(author: "Alex", post: {title: "helpp", summary: "help2" }) {
title
}
}
```
> The best way to see the result of your queries/mutations and to inspect the Schema is to use a [GraphiQL tool](#graphiql-tool)
### Non Null
`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.
We have used this type many times already so we'll just show you two methods that might be useful in your resolve functions:
- `getNullableType()`
- `getNamedType()`
These two can return you a type that was wrapped up in the `NonNullType` so you can get it's fields, arguments or name.
## Building your schema
It's always a good idea to give your heads up about any possible errors as soon as possible, better on the development stage.
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.
If you want to implement a new type consider extending the following classes:
* AbstractType
* AbstractScalarType
* AbstractObjectType
* AbstractMutationObjectType
* AbstractInputObjectType
* AbstractInterfaceType
* AbstractEnumType
* AbstractListType
* AbstractUnionType
* AbstractSchemaType
### Mutation helper class
Usually you can create a mutation buy extending `AbstractObjectType` or by creating a new field of `ObjectType` inside your `Schema::build` method.
It is crucial for the class to have a `getType` method returning the actual OutputType of your mutation.
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.
## Useful information
Expand Down
2 changes: 1 addition & 1 deletion Tests/ProcessorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ public function predefinedSchemaProvider()
'__schema' => [
'types' => [
['name' => 'TestSchema', 'fields' => [['name' => 'latest']]],
['name' => 'latest', 'fields' => [['name' => 'id'], ['name' => 'name']]],
['name' => 'Int', 'fields' => null],
['name' => 'latest', 'fields' => [['name' => 'id'], ['name' => 'name']]],
['name' => 'String', 'fields' => null],
['name' => '__Schema', 'fields' => [['name' => 'queryType'], ['name' => 'mutationType'], ['name' => 'subscriptionType'], ['name' => 'types'], ['name' => 'directives']]],
['name' => '__Type', 'fields' => [['name' => 'name'], ['name' => 'kind'], ['name' => 'description'], ['name' => 'ofType'], ['name' => 'inputFields'], ['name' => 'enumValues'], ['name' => 'fields'], ['name' => 'interfaces'], ['name' => 'possibleTypes']]],
Expand Down
27 changes: 13 additions & 14 deletions examples/02_blog/Schema/BlogSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,6 @@ class BlogSchema extends AbstractSchema
{
public function build(SchemaConfig $config)
{
$config->getMutation()->addFields([
'likePost' => new LikePost(),
'createPost' => [
'type' => new PostType(),
'args' => [
'post' => new PostInputType(),
'author' => new StringType()
],
'resolve' => function($value, $args, $type) {
return DataProvider::getPost(10);
}
]
]);
$config->getQuery()->addFields([
'latestPost' => new PostType(),
'randomBanner' => [
Expand All @@ -48,7 +35,19 @@ public function build(SchemaConfig $config)
}
]
]);

$config->getMutation()->addFields([
'likePost' => new LikePost(),
'createPost' => [
'type' => new PostType(),
'args' => [
'post' => new PostInputType(),
'author' => new StringType()
],
'resolve' => function($value, $args, $type) {
return DataProvider::getPost(10);
}
]
]);
}

}
9 changes: 3 additions & 6 deletions examples/02_blog/Schema/PostInputType.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
<?php
/*
* This file is a part of GraphQL project.
*
* @author Alexandr Viniychuk <[email protected]>
* created: 5/5/16 11:39 PM
*/
/**
* PostInputType.php
*/

namespace Examples\Blog\Schema;

Expand Down

0 comments on commit 461e07f

Please sign in to comment.