Skip to content

Commit 8404965

Browse files
authored
Make querying isOneOf optional in Introspection query (#1737)
Including it by default makes introspection fail on servers that do not support it: ``` Cannot query field "isOneOf" on type "__Type". ``` Thus, I added a configuration option `typeIsOneOf` which works analogous to `directiveIsRepeatable`.
1 parent 686f537 commit 8404965

File tree

6 files changed

+32
-10
lines changed

6 files changed

+32
-10
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,12 @@ You can find and compare releases at the [GitHub release page](https://github.co
99

1010
## Unreleased
1111

12+
## v15.21.3
13+
14+
### Fixed
15+
16+
- Make querying `isOneOf` optional in Introspection query https://github.com/webonyx/graphql-php/pull/1737
17+
1218
## v15.21.2
1319

1420
### Fixed

src/Type/Introspection.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@
3030
* @phpstan-type IntrospectionOptions array{
3131
* descriptions?: bool,
3232
* directiveIsRepeatable?: bool,
33+
* typeIsOneOf?: bool,
3334
* }
3435
*
3536
* Available options:
3637
* - descriptions
37-
* Whether to include descriptions in the introspection result.
38+
* Include descriptions in the introspection result?
3839
* Default: true
3940
* - directiveIsRepeatable
40-
* Whether to include `isRepeatable` flag on directives.
41+
* Include field `isRepeatable` for directives?
42+
* Default: false
43+
* - typeIsOneOf
44+
* Include field `isOneOf` for types?
4145
* Default: false
4246
*
4347
* @see \GraphQL\Tests\Type\IntrospectionTest
@@ -81,6 +85,7 @@ public static function getIntrospectionQuery(array $options = []): string
8185
$optionsWithDefaults = array_merge([
8286
'descriptions' => true,
8387
'directiveIsRepeatable' => false,
88+
'typeIsOneOf' => false,
8489
], $options);
8590

8691
$descriptions = $optionsWithDefaults['descriptions']
@@ -89,6 +94,9 @@ public static function getIntrospectionQuery(array $options = []): string
8994
$directiveIsRepeatable = $optionsWithDefaults['directiveIsRepeatable']
9095
? 'isRepeatable'
9196
: '';
97+
$typeIsOneOf = $optionsWithDefaults['typeIsOneOf']
98+
? 'isOneOf'
99+
: '';
92100

93101
return <<<GRAPHQL
94102
query IntrospectionQuery {
@@ -115,7 +123,7 @@ public static function getIntrospectionQuery(array $options = []): string
115123
kind
116124
name
117125
{$descriptions}
118-
isOneOf
126+
{$typeIsOneOf}
119127
fields(includeDeprecated: true) {
120128
name
121129
{$descriptions}
@@ -210,7 +218,10 @@ enumValues(includeDeprecated: true) {
210218
*/
211219
public static function fromSchema(Schema $schema, array $options = []): array
212220
{
213-
$optionsWithDefaults = array_merge(['directiveIsRepeatable' => true], $options);
221+
$optionsWithDefaults = array_merge([
222+
'directiveIsRepeatable' => true,
223+
'typeIsOneOf' => true,
224+
], $options);
214225

215226
$result = GraphQL::executeQuery(
216227
$schema,

src/Utils/Utils.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ public static function chr(int $ord, string $encoding = 'UTF-8'): string
116116
return pack('N', $ord);
117117
}
118118

119-
$converted = mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
120-
121-
return $converted;
119+
return mb_convert_encoding(self::chr($ord, 'UCS-4BE'), $encoding, 'UCS-4BE');
122120
}
123121

124122
/** UTF-8 compatible ord(). */

tests/Type/IntrospectionTest.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public function testExecutesAnIntrospectionQuery(): void
4141
$source = Introspection::getIntrospectionQuery([
4242
'descriptions' => false,
4343
'directiveIsRepeatable' => true,
44+
'typeIsOneOf' => true,
4445
]);
4546

4647
$expected = [
@@ -1773,7 +1774,10 @@ public function testExecutesAnIntrospectionQueryWithoutCallingGlobalFieldResolve
17731774
$schema = new Schema([
17741775
'query' => $QueryRoot,
17751776
]);
1776-
$source = Introspection::getIntrospectionQuery(['directiveIsRepeatable' => true]);
1777+
$source = Introspection::getIntrospectionQuery([
1778+
'directiveIsRepeatable' => true,
1779+
'typeIsOneOf' => true,
1780+
]);
17771781

17781782
$calledForFields = [];
17791783
$fieldResolver = static function ($value, array $args, $context, ResolveInfo $info) use (&$calledForFields) {

tests/Utils/BuildClientSchemaTest.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@ final class BuildClientSchemaTest extends TestCase
2626
*/
2727
protected static function assertCycleIntrospection(string $sdl): void
2828
{
29-
$options = ['directiveIsRepeatable' => true];
29+
$options = [
30+
'directiveIsRepeatable' => true,
31+
'typeIsOneOf' => true,
32+
];
3033

3134
$serverSchema = BuildSchema::build($sdl);
3235
$initialIntrospection = Introspection::fromSchema($serverSchema, $options);

tests/Validator/QueryComplexityTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function testQueryWithCustomAndSkipDirective(): void
164164

165165
public function testComplexityIntrospectionQuery(): void
166166
{
167-
$this->assertIntrospectionQuery(188);
167+
$this->assertIntrospectionQuery(187);
168168
}
169169

170170
public function testIntrospectionTypeMetaFieldQuery(): void

0 commit comments

Comments
 (0)