1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace GraphQLAPI\GraphQLAPI\Blocks; |
||
6 | |||
7 | use GraphQLAPI\GraphQLAPI\Blocks\AbstractBlock; |
||
8 | use GraphQLAPI\GraphQLAPI\General\EndpointHelpers; |
||
9 | use GraphQLAPI\GraphQLAPI\Blocks\GraphQLByPoPBlockTrait; |
||
10 | use PoP\ComponentModel\Facades\Instances\InstanceManagerFacade; |
||
11 | use GraphQLAPI\GraphQLAPI\BlockCategories\AbstractBlockCategory; |
||
12 | use GraphQLAPI\GraphQLAPI\BlockCategories\PersistedQueryBlockCategory; |
||
13 | |||
14 | /** |
||
15 | * GraphiQL block |
||
16 | */ |
||
17 | abstract class AbstractGraphiQLBlock extends AbstractBlock |
||
18 | { |
||
19 | use GraphQLByPoPBlockTrait; |
||
20 | |||
21 | public const ATTRIBUTE_NAME_QUERY = 'query'; |
||
22 | public const ATTRIBUTE_NAME_VARIABLES = 'variables'; |
||
23 | |||
24 | protected function getBlockName(): string |
||
25 | { |
||
26 | return 'graphiql'; |
||
27 | } |
||
28 | |||
29 | protected function getBlockCategory(): ?AbstractBlockCategory |
||
30 | { |
||
31 | $instanceManager = InstanceManagerFacade::getInstance(); |
||
32 | /** |
||
33 | * @var PersistedQueryBlockCategory |
||
34 | */ |
||
35 | $blockCategory = $instanceManager->getInstance(PersistedQueryBlockCategory::class); |
||
36 | return $blockCategory; |
||
37 | } |
||
38 | |||
39 | protected function isDynamicBlock(): bool |
||
40 | { |
||
41 | return true; |
||
42 | } |
||
43 | |||
44 | protected function getAdminGraphQLEndpoint(): string |
||
45 | { |
||
46 | return EndpointHelpers::getAdminGraphQLEndpoint(true); |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Pass localized data to the block |
||
51 | * |
||
52 | * @return array<string, mixed> |
||
53 | */ |
||
54 | protected function getLocalizedData(): array |
||
55 | { |
||
56 | return array_merge( |
||
57 | parent::getLocalizedData(), |
||
58 | [ |
||
59 | 'nonce' => \wp_create_nonce('wp_rest'), |
||
60 | 'endpoint' => $this->getAdminGraphQLEndpoint(), |
||
61 | 'defaultQuery' => $this->getDefaultQuery(), |
||
62 | ] |
||
63 | ); |
||
64 | } |
||
65 | |||
66 | /** |
||
67 | * Load index.css from editor.scss |
||
68 | * |
||
69 | * @return boolean |
||
70 | */ |
||
71 | protected function registerEditorCSS(): bool |
||
72 | { |
||
73 | return true; |
||
74 | } |
||
75 | |||
76 | /** |
||
77 | * GraphiQL default query |
||
78 | * |
||
79 | * @return string |
||
80 | */ |
||
81 | protected function getDefaultQuery(): string |
||
82 | { |
||
83 | // Temporarily print nothing, until "Ctrl+A" works well: |
||
84 | // @see https://github.com/WordPress/gutenberg/issues/22689 |
||
85 | return ''; |
||
86 | return <<<EOT |
||
0 ignored issues
–
show
|
|||
87 | # Welcome to GraphiQL |
||
88 | # |
||
89 | # GraphiQL is an in-browser tool for writing, validating, and |
||
90 | # testing GraphQL queries. |
||
91 | # |
||
92 | # Type queries into this side of the screen, and you will see intelligent |
||
93 | # typeaheads aware of the current GraphQL type schema and live syntax and |
||
94 | # validation errors highlighted within the text. |
||
95 | # |
||
96 | # GraphQL queries typically start with a "{" character. Lines that starts |
||
97 | # with a # are ignored. |
||
98 | # |
||
99 | # An example GraphQL query might look like: |
||
100 | # |
||
101 | # { |
||
102 | # field(arg: "value") { |
||
103 | # subField |
||
104 | # } |
||
105 | # } |
||
106 | # |
||
107 | # Run the query (at any moment): |
||
108 | # |
||
109 | # Ctrl-Enter (or press the play button above) |
||
110 | # |
||
111 | |||
112 | EOT; |
||
113 | } |
||
114 | |||
115 | /** |
||
116 | * @param array<string, mixed> $attributes |
||
117 | */ |
||
118 | public function renderBlock(array $attributes, string $content): string |
||
119 | { |
||
120 | $content = sprintf( |
||
121 | '<div class="%s">', |
||
122 | $this->getBlockClassName() . ' ' . $this->getAlignClass() |
||
123 | ); |
||
124 | $query = $attributes[self::ATTRIBUTE_NAME_QUERY] ?? ''; |
||
125 | $variables = $attributes[self::ATTRIBUTE_NAME_VARIABLES] ?? null; |
||
126 | $content .= sprintf( |
||
127 | '<p><strong>%s</strong></p>', |
||
128 | \__('GraphQL Query:', 'graphql-api') |
||
129 | ) . ( |
||
130 | $query ? sprintf( |
||
131 | '<pre><code class="prettyprint language-graphql">%s</code></pre>', |
||
132 | $query |
||
133 | ) : sprintf( |
||
134 | '<p><em>%s</em></p>', |
||
135 | \__('(not set)', 'graphql-api') |
||
136 | ) |
||
137 | ); |
||
138 | if ($variables) { |
||
139 | $content .= sprintf( |
||
140 | '<p><strong>%s</strong></p>', |
||
141 | \__('Variables:', 'graphql-api') |
||
142 | ) . sprintf( |
||
143 | '<pre><code class="prettyprint language-json">%s</code></pre>', |
||
144 | $variables |
||
145 | ); |
||
146 | } |
||
147 | $content .= '</div>'; |
||
148 | return $content; |
||
149 | } |
||
150 | } |
||
151 |
This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.
Unreachable code is most often the result of
return
,die
orexit
statements that have been added for debug purposes.In the above example, the last
return false
will never be executed, because a return statement has already been met in every possible execution path.