Completed
Push — master ( 1826e3...724c6f )
by Rafael
07:29
created

SchemaExporter   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 126
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
dl 0
loc 126
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A export() 0 11 2
1
<?php
2
/*******************************************************************************
3
 *  This file is part of the GraphQL Bundle package.
4
 *
5
 *  (c) YnloUltratech <[email protected]>
6
 *
7
 *  For the full copyright and license information, please view the LICENSE
8
 *  file that was distributed with this source code.
9
 ******************************************************************************/
10
11
namespace Ynlo\GraphQLBundle\Schema;
12
13
use GraphQL\GraphQL;
14
use GraphQL\Utils\SchemaPrinter;
15
16
class SchemaExporter
17
{
18
    /**
19
     * @var SchemaCompiler
20
     */
21
    protected $compiler;
22
23
    private const INTROSPECTION_QUERY = <<<GraphQL
24
query IntrospectionQuery {
25
    __schema {
26
      queryType { name }
27
      mutationType { name }
28
      subscriptionType { name }
29
      types {
30
        ...FullType
31
      }
32
      directives {
33
        name
34
        description
35
        locations
36
        args {
37
          ...InputValue
38
        }
39
      }
40
    }
41
  }
42
43
  fragment FullType on __Type {
44
    kind
45
    name
46
    description
47
    fields(includeDeprecated: true) {
48
      name
49
      description
50
      args {
51
        ...InputValue
52
      }
53
      type {
54
        ...TypeRef
55
      }
56
      isDeprecated
57
      deprecationReason
58
    }
59
    inputFields {
60
      ...InputValue
61
    }
62
    interfaces {
63
      ...TypeRef
64
    }
65
    enumValues(includeDeprecated: true) {
66
      name
67
      description
68
      isDeprecated
69
      deprecationReason
70
    }
71
    possibleTypes {
72
      ...TypeRef
73
    }
74
  }
75
76
  fragment InputValue on __InputValue {
77
    name
78
    description
79
    type { ...TypeRef }
80
    defaultValue
81
  }
82
83
  fragment TypeRef on __Type {
84
    kind
85
    name
86
    ofType {
87
      kind
88
      name
89
      ofType {
90
        kind
91
        name
92
        ofType {
93
          kind
94
          name
95
          ofType {
96
            kind
97
            name
98
            ofType {
99
              kind
100
              name
101
              ofType {
102
                kind
103
                name
104
                ofType {
105
                  kind
106
                  name
107
                }
108
              }
109
            }
110
          }
111
        }
112
      }
113
    }
114
  }
115
GraphQL;
116
117
    /**
118
     * @param SchemaCompiler $compiler
119
     */
120
    public function __construct(SchemaCompiler $compiler)
121
    {
122
        $this->compiler = $compiler;
123
    }
124
125
    /**
126
     * @param string $endpoint
127
     * @param bool   $json
128
     *
129
     * @return string
130
     */
131
    public function export(string $endpoint, bool $json = false): string
132
    {
133
        $schema = $this->compiler->compile($endpoint);
134
135
        if ($json) {
136
            $result = GraphQL::executeQuery($schema, self::INTROSPECTION_QUERY);
137
138
            return json_encode($result->toArray(), JSON_PRETTY_PRINT);
139
        }
140
141
        return SchemaPrinter::doPrint($schema);
142
    }
143
}
144