Skip to content

Commit aae5663

Browse files
author
Florian Engelhardt
committed
cleanup and split schema and app in two files
1 parent 7cdc2c0 commit aae5663

File tree

2 files changed

+97
-95
lines changed

2 files changed

+97
-95
lines changed

api/app.php

Lines changed: 2 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -10,116 +10,23 @@
1010
use Psr\Http\Message\ResponseInterface;
1111

1212
use GraphQL\GraphQL;
13-
use GraphQL\Type\Schema;
1413
use GraphQL\Executor\ExecutionResult;
1514
use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter;
1615

17-
use GraphQL\Type\Definition\ObjectType;
18-
use GraphQL\Type\Definition\Type;
19-
2016
require __DIR__ . '/vendor/autoload.php';
2117

2218
$loop = Factory::create();
2319
$browser = new Browser($loop);
2420

25-
$productType = new ObjectType([
26-
'name' => 'Product',
27-
'fields' => [
28-
'id' => [
29-
'type' => Type::id(),
30-
],
31-
'name' => [
32-
'type' => Type::string(),
33-
],
34-
'description' => [
35-
'type' => Type::string(),
36-
]
37-
]
38-
]);
39-
40-
$reviewType = new ObjectType([
41-
'name' => 'Review',
42-
'fields' => [
43-
'id' => [
44-
'type' => Type::id(),
45-
],
46-
'title' => [
47-
'type' => Type::string(),
48-
],
49-
'grade' => [
50-
'type' => Type::int(),
51-
],
52-
'comment' => [
53-
'type' => Type::string(),
54-
],
55-
'product' => [
56-
'type' => $productType,
57-
'resolve' => function ($review) use ($browser) {
58-
return $browser->get('http://product-service:3000/')->then(function($response) {
59-
$rawBody = (string)$response->getBody();
60-
return json_decode($rawBody);
61-
});
62-
}
63-
]
64-
]
65-
]);
66-
67-
$query = new ObjectType([
68-
'name' => 'Query',
69-
'fields' => [
70-
'echo' => [
71-
'type' => Type::string(),
72-
'args' => [
73-
'message' => Type::nonNull(Type::string()),
74-
],
75-
'resolve' => function($root, $args) {
76-
return $root['prefix'] . $args['message'];
77-
}
78-
],
79-
'product' => [
80-
'type' => $productType,
81-
'args' => [
82-
'id' => Type::nonNull(Type::id()),
83-
],
84-
'resolve' => function ($root, $args) use ($browser) {
85-
return $browser->get('http://product-service:3000/')->then(function($response) {
86-
$rawBody = (string)$response->getBody();
87-
return json_decode($rawBody);
88-
});
89-
}
90-
],
91-
'review' => [
92-
'type' => $reviewType,
93-
'args' => [
94-
'id' => Type::nonNull(Type::id()),
95-
],
96-
'resolve' => function ($root, $args) use ($browser) {
97-
return $browser->get('http://review-service:3000/')->then(function($response) {
98-
$rawBody = (string)$response->getBody();
99-
return json_decode($rawBody);
100-
});
101-
}
102-
],
103-
104-
],
105-
]);
106-
107-
$schema = new Schema([
108-
'query' => $query,
109-
'types' => [
110-
$productType
111-
]
112-
]);
21+
require __DIR__ . '/schema.php';
11322

11423
$react = new ReactPromiseAdapter();
11524

11625
$server = new Server(function (ServerRequestInterface $request) use ($schema, $react) {
117-
$rawInput = (string)$request->getBody();
118-
$input = json_decode($rawInput, true);
26+
$input = json_decode((string)$request->getBody(), true);
11927
$query = $input['query'];
12028
$variableValues = isset($input['variables']) ? $input['variables'] : null;
12129
$rootValue = ['prefix' => 'You said: '];
122-
var_dump($query);
12330
$promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues);
12431
return $promise->then(function(ExecutionResult $result) {
12532
$output = $result->toArray();

api/schema.php

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
use GraphQL\Type\Schema;
5+
use GraphQL\Executor\ExecutionResult;
6+
use GraphQL\Type\Definition\ObjectType;
7+
use GraphQL\Type\Definition\Type;
8+
9+
$productResolver = function ($review) use ($browser) {
10+
return $browser->get('http://product-service:3000/')->then(function($response) {
11+
$rawBody = (string)$response->getBody();
12+
return json_decode($rawBody);
13+
});
14+
};
15+
16+
$reviewResolver = function ($product) use ($browser) {
17+
return $browser->get('http://review-service:3000/')->then(function($response) {
18+
$rawBody = (string)$response->getBody();
19+
return json_decode($rawBody);
20+
});
21+
};
22+
23+
$productType = new ObjectType([
24+
'name' => 'Product',
25+
'fields' => [
26+
'id' => [
27+
'type' => Type::id(),
28+
],
29+
'name' => [
30+
'type' => Type::string(),
31+
],
32+
'description' => [
33+
'type' => Type::string(),
34+
]
35+
]
36+
]);
37+
38+
$reviewType = new ObjectType([
39+
'name' => 'Review',
40+
'fields' => [
41+
'id' => [
42+
'type' => Type::id(),
43+
],
44+
'title' => [
45+
'type' => Type::string(),
46+
],
47+
'grade' => [
48+
'type' => Type::int(),
49+
],
50+
'comment' => [
51+
'type' => Type::string(),
52+
],
53+
'product' => [
54+
'type' => $productType,
55+
'resolve' => $productResolver
56+
]
57+
]
58+
]);
59+
60+
$query = new ObjectType([
61+
'name' => 'Query',
62+
'fields' => [
63+
'echo' => [
64+
'type' => Type::string(),
65+
'args' => [
66+
'message' => Type::nonNull(Type::string()),
67+
],
68+
'resolve' => function($root, $args) {
69+
return $root['prefix'] . $args['message'];
70+
}
71+
],
72+
'product' => [
73+
'type' => $productType,
74+
'args' => [
75+
'id' => Type::nonNull(Type::id()),
76+
],
77+
'resolve' => $productResolver
78+
],
79+
'review' => [
80+
'type' => $reviewType,
81+
'args' => [
82+
'id' => Type::nonNull(Type::id()),
83+
],
84+
'resolve' => $reviewResolver
85+
],
86+
87+
],
88+
]);
89+
90+
$schema = new Schema([
91+
'query' => $query,
92+
'types' => [
93+
$productType
94+
]
95+
]);

0 commit comments

Comments
 (0)