-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02-cep-mock.php
executable file
·81 lines (62 loc) · 2.21 KB
/
02-cep-mock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
require_once __DIR__ . '/vendor/autoload.php';
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\Schema;
use GraphQL\GraphQL;
use GraphQL\Type\Definition\ResolveInfo;
try {
$enderecoType = new ObjectType([
'name' => 'Endereco',
'fields' => [
'cep' => [ 'type' => Type::string() ],
'logradouro' => [ 'type' => Type::string() ],
'complemento' => [ 'type' => Type::string() ],
'bairro' => [ 'type' => Type::string() ],
'cidade' => [ 'type' => Type::string() ],
'uf' => [ 'type' => Type::string() ],
]
]);
$queryType = new ObjectType([
'name' => 'Query',
'fields' => [
'endereco' => [
'type' => $enderecoType,
'args' => [
'cep' => ['type' => Type::string()],
],
'resolve' => function ($root, $args) {
return [
'cep' => '01001-000',
'logradouro' => 'Praça da Sé',
'complemento' => 'lado ímpar',
'bairro' => 'Sé',
'localidade' => 'São Paulo'
'uf' => 'SP',
'unidade' => '',
'ibge' => '3550308',
'gia' => '1004'
];
}
],
]
]);
$schema = new Schema([
'query' => $queryType,
]);
$rawInput = file_get_contents('php://input');
$input = json_decode($rawInput, true);
$query = $input['query'];
$variableValues = isset($input['variables']) ? $input['variables'] : null;
$rootValue = [];
$result = GraphQL::executeQuery($schema, $query, $rootValue, null, $variableValues);
$output = $result->toArray();
} catch (\Exception $e) {
$output = [
'error' => [
'message' => $e->getMessage()
]
];
}
header('Content-Type: application/json; charset=UTF-8');
echo json_encode($output);