Skip to content

Commit 8758712

Browse files
authored
Refine example for $typeConfigDecorator usage (#1704)
1 parent ca77f56 commit 8758712

File tree

14 files changed

+151
-130
lines changed

14 files changed

+151
-130
lines changed

CHANGELOG.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12-
### Added
13-
- Add **examples/05-using-typeConfigDecorator-with-SDL** to provide a solid example about resolving
14-
fields on using [SDL](https://webonyx.github.io/graphql-php/schema-definition-language/) instead of [Object Types](https://webonyx.github.io/graphql-php/type-definitions/object-types/).
15-
1612
## v15.20.0
1713

1814
### Added

docs/schema-definition-language.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ $contents = file_get_contents('schema.graphql');
5353
$schema = BuildSchema::build($contents, $typeConfigDecorator);
5454
```
5555

56-
You can learn more about using `$typeConfigDecorator` in [examples/05-using-typeConfigDecorator-with-SDL](../examples/05-using-typeConfigDecorator-with-SDL/)
56+
You can learn more about using `$typeConfigDecorator` in [examples/05-type-config-decorator](https://github.com/webonyx/graphql-php/blob/master/examples/05-type-config-decorator).
5757

5858
## Performance considerations
5959

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Using schema definition language and `typeConfigDecorator`
2+
3+
This example shows how to resolve object types in a query using the schema definition language instead of defining the schema in pure PHP code.
4+
5+
The business logic matches [the Apollo GraphQL course](https://www.apollographql.com/tutorials/lift-off-part2/03-apollo-restdatasource).
6+
There is [an accompanying issue](https://github.com/webonyx/graphql-php/issues/1699) that provides further explanations.
7+
8+
## How to follow along?
9+
10+
1. Navigate to [examples/05-type-config-decorator](/examples/05-type-config-decorator) using the command line
11+
2. Run `composer install`
12+
3. Run `php -S localhost:8080 graphql.php`
13+
4. You may use `curl` to make a request to the running API:
14+
15+
```
16+
curl --data '{"query": "{ tracksForHome { title thumbnail author { id name } } }"}' --header "Content-Type: application/json" http://localhost:8080
17+
```
18+
19+
Alternatively, use your favorite API client and make a request to `localhost:8080` with the following request body:
20+
21+
```graphql
22+
{
23+
tracksForHome {
24+
title
25+
thumbnail
26+
author {
27+
id
28+
name
29+
}
30+
}
31+
}
32+
```
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Models;
4+
5+
use Core\Model;
6+
7+
final class Author extends Model
8+
{
9+
/**
10+
* @throws \RuntimeException
11+
*
12+
* @return array<string, mixed>
13+
*/
14+
public static function find(int $id): array
15+
{
16+
return static::get("author/{$id}");
17+
}
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace App\Models;
4+
5+
use Core\Model;
6+
7+
final class Track extends Model
8+
{
9+
/**
10+
* @throws \RuntimeException
11+
*
12+
* @return array<int, array<string, mixed>>
13+
*/
14+
public static function all(): array
15+
{
16+
return self::get('tracks');
17+
}
18+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Core;
4+
5+
abstract class Model
6+
{
7+
/**
8+
* @throws \RuntimeException
9+
*
10+
* @return mixed
11+
*/
12+
protected static function get(string $endpoint)
13+
{
14+
$url = "https://odyssey-lift-off-rest-api.herokuapp.com/{$endpoint}";
15+
16+
$contents = file_get_contents($url);
17+
if ($contents === false) {
18+
throw new \RuntimeException("Failed to fetch data from URL: {$url}.");
19+
}
20+
21+
return json_decode($contents, true);
22+
}
23+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php declare(strict_types=1);
2+
3+
require __DIR__ . '/vendor/autoload.php';
4+
5+
use App\Models\Author;
6+
use App\Models\Track;
7+
use GraphQL\GraphQL;
8+
use GraphQL\Utils\BuildSchema;
9+
10+
$typeConfigDecorator = function (array $typeConfig): array {
11+
// For object types, the typeConfig array contains the name of the type and a closure that returns the fields of the type.
12+
//
13+
// For the Query type, it might look like this:
14+
// [
15+
// 'name': 'Query',
16+
// 'fields': fn () => [...],
17+
// ],
18+
switch ($typeConfig['name']) {
19+
case 'Query':
20+
$typeConfig['fields'] = function () use ($typeConfig): array {
21+
$fields = $typeConfig['fields']();
22+
$fields['tracksForHome']['resolve'] = fn (): array => Track::all();
23+
24+
return $fields;
25+
};
26+
27+
return $typeConfig;
28+
case 'Track':
29+
$typeConfig['fields'] = function () use ($typeConfig): array {
30+
$fields = $typeConfig['fields']();
31+
$fields['author']['resolve'] = fn (array $track): array => Author::find($track['authorId']);
32+
33+
return $fields;
34+
};
35+
36+
return $typeConfig;
37+
}
38+
39+
return $typeConfig;
40+
};
41+
$contents = file_get_contents(__DIR__ . '/schema.graphql');
42+
if ($contents === false) {
43+
throw new RuntimeException('Failed to read schema.graphql.');
44+
}
45+
$schema = BuildSchema::build($contents, $typeConfigDecorator);
46+
47+
$requestBody = file_get_contents('php://input');
48+
if ($requestBody === false) {
49+
throw new RuntimeException('Failed to read php://input.');
50+
}
51+
$parsedBody = json_decode($requestBody, true, 10);
52+
$queryString = $parsedBody['query'];
53+
54+
$result = GraphQL::executeQuery($schema, $queryString);
55+
56+
header('Content-Type: application/json');
57+
echo json_encode($result);

examples/05-using-typeConfigDecorator-with-SDL/schema.graphql renamed to examples/05-type-config-decorator/schema.graphql

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
type Query {
2-
tracksForHome: [Track]
3-
2+
tracksForHome: [Track!]!
43
}
54

65
type Track {
@@ -16,4 +15,4 @@ type Author {
1615
id: ID!
1716
name: String
1817
photo: String
19-
}
18+
}

examples/05-using-typeConfigDecorator-with-SDL/README.md

Lines changed: 0 additions & 32 deletions
This file was deleted.

0 commit comments

Comments
 (0)