From 461e07fa079d0c7711e215e19bfed2ae74c3fc73 Mon Sep 17 00:00:00 2001 From: Alexandr Viniychuk Date: Fri, 6 May 2016 01:44:27 -0400 Subject: [PATCH] finished beta version of documentation --- README.md | 88 ++++++++++++++++++++++- Tests/ProcessorTest.php | 2 +- examples/02_blog/Schema/BlogSchema.php | 27 ++++--- examples/02_blog/Schema/PostInputType.php | 9 +-- 4 files changed, 103 insertions(+), 23 deletions(-) diff --git a/README.md b/README.md index 2757cce4..d38ccede 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 +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 +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 diff --git a/Tests/ProcessorTest.php b/Tests/ProcessorTest.php index ecda49fc..c1f4cd19 100644 --- a/Tests/ProcessorTest.php +++ b/Tests/ProcessorTest.php @@ -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']]], diff --git a/examples/02_blog/Schema/BlogSchema.php b/examples/02_blog/Schema/BlogSchema.php index 98ffb7a5..2c033ffa 100644 --- a/examples/02_blog/Schema/BlogSchema.php +++ b/examples/02_blog/Schema/BlogSchema.php @@ -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' => [ @@ -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); + } + ] + ]); } } diff --git a/examples/02_blog/Schema/PostInputType.php b/examples/02_blog/Schema/PostInputType.php index cc3f0b89..6ae832e3 100644 --- a/examples/02_blog/Schema/PostInputType.php +++ b/examples/02_blog/Schema/PostInputType.php @@ -1,10 +1,7 @@ -* created: 5/5/16 11:39 PM -*/ +/** + * PostInputType.php + */ namespace Examples\Blog\Schema;