22GraphQL is data-storage agnostic. You can use any underlying data storage engine, including SQL or NoSQL database,
33plain files or in-memory data structures.
44
5- In order to convert GraphQL query to PHP array ** graphql-php** traverses query fields (using depth-first algorithm) and
5+ In order to convert the GraphQL query to PHP array, ** graphql-php** traverses query fields (using depth-first algorithm) and
66runs special ** resolve** function on each field. This ** resolve** function is provided by you as a part of
77[ field definition] ( type-system/object-types.md#field-configuration-options ) or [ query execution call] ( executing-queries.md#overview ) .
88
9- Result returned by ** resolve** function is directly included in response (for scalars and enums)
9+ Result returned by ** resolve** function is directly included in the response (for scalars and enums)
1010or passed down to nested fields (for objects).
1111
1212Let's walk through an example. Consider following GraphQL query:
@@ -25,6 +25,9 @@ Let's walk through an example. Consider following GraphQL query:
2525We need a Schema that can fulfill it. On the very top level the Schema contains Query type:
2626
2727``` php
28+ <?php
29+ use GraphQL\Type\Definition\ObjectType;
30+
2831$queryType = new ObjectType([
2932 'name' => 'Query',
3033 'fields' => [
@@ -46,12 +49,16 @@ $queryType = new ObjectType([
4649
4750As we see field ** lastStory** has ** resolve** function that is responsible for fetching data.
4851
49- In our example we simply return array value, but in real-world application you would query
50- your database/cache/search index and return result.
52+ In our example, we simply return array value, but in the real-world application you would query
53+ your database/cache/search index and return the result.
5154
5255Since ** lastStory** is of composite type ** BlogStory** this result is passed down to fields of this type:
5356
5457``` php
58+ <?php
59+ use GraphQL\Type\Definition\Type;
60+ use GraphQL\Type\Definition\ObjectType;
61+
5562$blogStoryType = new ObjectType([
5663 'name' => 'BlogStory',
5764 'fields' => [
@@ -83,8 +90,8 @@ $blogStoryType = new ObjectType([
8390
8491Here ** $blogStory** is the array returned by ** lastStory** field above.
8592
86- Again: in real-world applications you would fetch user data from datastore by ** authorId** and return it.
87- Also note that you don't have to return arrays. You can return any value, ** graphql-php** will pass it untouched
93+ Again: in the real-world applications you would fetch user data from data store by ** authorId** and return it.
94+ Also, note that you don't have to return arrays. You can return any value, ** graphql-php** will pass it untouched
8895to nested resolvers.
8996
9097But then the question appears - field ** title** has no ** resolve** option. How is it resolved?
@@ -95,7 +102,8 @@ for a field you simply override this default resolver.
95102# Default Field Resolver
96103** graphql-php** provides following default field resolver:
97104``` php
98- function defaultFieldResolver($source, $args, $context, ResolveInfo $info)
105+ <?php
106+ function defaultFieldResolver($source, $args, $context, \GraphQL\Type\Definition\ResolveInfo $info)
99107{
100108 $fieldName = $info->fieldName;
101109 $property = null;
@@ -115,7 +123,7 @@ function defaultFieldResolver($source, $args, $context, ResolveInfo $info)
115123```
116124
117125As you see it returns value by key (for arrays) or property (for objects).
118- If value is not set - it returns ** null** .
126+ If the value is not set - it returns ** null** .
119127
120128To override the default resolver, pass it as an argument of [ executeQuery] ( executing-queries.md ) call.
121129
@@ -124,6 +132,11 @@ Sometimes it might be convenient to set default field resolver per type. You can
124132[ resolveField option in type config] ( type-system/object-types.md#configuration-options ) . For example:
125133
126134``` php
135+ <?php
136+ use GraphQL\Type\Definition\Type;
137+ use GraphQL\Type\Definition\ObjectType;
138+ use GraphQL\Type\Definition\ResolveInfo;
139+
127140$userType = new ObjectType([
128141 'name' => 'User',
129142 'fields' => [
@@ -167,13 +180,14 @@ Consider following GraphQL query:
167180}
168181```
169182
170- Naive field resolution process would require up to 10 calls to underlying data store to fetch authors for all 10 stories.
183+ Naive field resolution process would require up to 10 calls to the underlying data store to fetch authors for all 10 stories.
171184
172- ** graphql-php** provides tools to mitigate this problem: it allows you to defer actual field resolution to later stage
185+ ** graphql-php** provides tools to mitigate this problem: it allows you to defer actual field resolution to a later stage
173186when one batched query could be executed instead of 10 distinct queries.
174187
175- Here is an example of ` BlogStory ` resolver for field ` author ` that uses deferring:
188+ Here is an example of ** BlogStory** resolver for field ** author** that uses deferring:
176189``` php
190+ <?php
177191'resolve' => function($blogStory) {
178192 MyUserBuffer::add($blogStory['authorId']);
179193
@@ -184,18 +198,16 @@ Here is an example of `BlogStory` resolver for field `author` that uses deferrin
184198}
185199```
186200
187- In this example we fill up buffer with 10 author ids first. Then ** graphql-php** continues
201+ In this example, we fill up the buffer with 10 author ids first. Then ** graphql-php** continues
188202resolving other non-deferred fields until there are none of them left.
189203
190- After that it calls ` Closures ` wrapped by ` GraphQL\Deferred ` which in turn load all buffered
191- ids once (using SQL IN(?), Redis MGET or other similar tools) and return final field value.
204+ After that, it calls closures wrapped by ` GraphQL\Deferred ` which in turn load all buffered
205+ ids once (using SQL IN(?), Redis MGET or other similar tools) and returns final field value.
192206
193207Originally this approach was advocated by Facebook in their [ Dataloader] ( https://github.com/facebook/dataloader )
194- project.
195-
196- This solution enables very interesting optimizations at no cost. Consider following query:
208+ project. This solution enables very interesting optimizations at no cost. Consider the following query:
197209
198- ```
210+ ``` graphql
199211{
200212 topStories (limit : 10 ) {
201213 author {
@@ -212,14 +224,14 @@ This solution enables very interesting optimizations at no cost. Consider follow
212224}
213225```
214226
215- Even if ` author ` field is located on different levels of query - it can be buffered in the same buffer.
216- In this example only one query will be executed for all story authors comparing to 20 queries
217- in naive implementation.
227+ Even though ** author** field is located on different levels of the query - it can be buffered in the same buffer.
228+ In this example, only one query will be executed for all story authors comparing to 20 queries
229+ in a naive implementation.
218230
219231# Async PHP
220- Since: 0.10.0 (version 0.9.0 had slightly different API which is deprecated)
232+ Since: 0.10.0 (version 0.9.0 had slightly different API which still works, but is deprecated)
221233
222- If your project runs in environment that supports async operations
234+ If your project runs in an environment that supports async operations
223235(like HHVM, ReactPHP, Icicle.io, appserver.io, PHP threads, etc)
224236you can leverage the power of your platform to resolve some fields asynchronously.
225237
@@ -230,6 +242,10 @@ To start using this feature, switch facade method for query execution from
230242** executeQuery** to ** promiseToExecute** :
231243
232244``` php
245+ <?php
246+ use GraphQL\GraphQL;
247+ use GraphQL\Executor\ExecutionResult;
248+
233249$promise = GraphQL::promiseToExecute(
234250 $promiseAdapter,
235251 $schema,
@@ -252,6 +268,6 @@ Where **$promiseAdapter** is an instance of:
252268 ` GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter `
253269
254270* Other platforms: write your own class implementing interface: <br >
255- ` GraphQL\Executor\Promise\PromiseAdapter ` .
271+ [ ` GraphQL\Executor\Promise\PromiseAdapter ` ] ( reference.md#graphqlexecutorpromisepromiseadapter ) .
256272
257273Then your ** resolve** functions should return promises of your platform instead of ` GraphQL\Deferred ` s.
0 commit comments