|
10 | 10 | use Psr\Http\Message\ResponseInterface; |
11 | 11 |
|
12 | 12 | use GraphQL\GraphQL; |
13 | | -use GraphQL\Type\Schema; |
14 | 13 | use GraphQL\Executor\ExecutionResult; |
15 | 14 | use GraphQL\Executor\Promise\Adapter\ReactPromiseAdapter; |
16 | 15 |
|
17 | | -use GraphQL\Type\Definition\ObjectType; |
18 | | -use GraphQL\Type\Definition\Type; |
19 | | - |
20 | 16 | require __DIR__ . '/vendor/autoload.php'; |
21 | 17 |
|
22 | 18 | $loop = Factory::create(); |
23 | 19 | $browser = new Browser($loop); |
24 | 20 |
|
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'; |
113 | 22 |
|
114 | 23 | $react = new ReactPromiseAdapter(); |
115 | 24 |
|
116 | 25 | $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); |
119 | 27 | $query = $input['query']; |
120 | 28 | $variableValues = isset($input['variables']) ? $input['variables'] : null; |
121 | 29 | $rootValue = ['prefix' => 'You said: ']; |
122 | | - var_dump($query); |
123 | 30 | $promise = GraphQL::promiseToExecute($react, $schema, $query, $rootValue, null, $variableValues); |
124 | 31 | return $promise->then(function(ExecutionResult $result) { |
125 | 32 | $output = $result->toArray(); |
|
0 commit comments