Skip to content

Commit c6a3928

Browse files
committed
Make root query operation type optional
graphql/graphql-spec#631
1 parent a596c74 commit c6a3928

8 files changed

+13
-68
lines changed

src/Executor/ReferenceExecutor.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -608,16 +608,6 @@ protected function getFieldDef(Schema $schema, ObjectType $parentType, string $f
608608
$typeMetaFieldDef ??= Introspection::typeMetaFieldDef();
609609
$typeNameMetaFieldDef ??= Introspection::typeNameMetaFieldDef();
610610

611-
$queryType = $schema->getQueryType();
612-
613-
if ($fieldName === $schemaMetaFieldDef->name && $queryType === $parentType) {
614-
return $schemaMetaFieldDef;
615-
}
616-
617-
if ($fieldName === $typeMetaFieldDef->name && $queryType === $parentType) {
618-
return $typeMetaFieldDef;
619-
}
620-
621611
if ($fieldName === $typeNameMetaFieldDef->name) {
622612
return $typeNameMetaFieldDef;
623613
}

src/Type/Definition/ObjectType.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
/**
2222
* Object Type Definition
2323
*
24-
* Almost all of the GraphQL types you define will be object types. Object types
25-
* have a name, but most importantly describe their fields.
24+
* Most GraphQL types you define will be object types. Object types
25+
* have a name, but most importantly they describe their fields.
2626
*
2727
* Example:
2828
*
@@ -87,13 +87,11 @@ class ObjectType extends TypeWithFields implements OutputType, CompositeType, Nu
8787
*/
8888
public function __construct(array $config)
8989
{
90-
if (! isset($config['name'])) {
91-
$config['name'] = $this->tryInferName();
92-
}
90+
$name = $config['name'] ?? $this->tryInferName();
9391

94-
Utils::invariant(is_string($config['name']), 'Must provide name.');
92+
Utils::invariant(is_string($name), 'Must provide name.');
9593

96-
$this->name = $config['name'];
94+
$this->name = $name;
9795
$this->description = $config['description'] ?? null;
9896
$this->resolveFieldFn = $config['resolveField'] ?? null;
9997
$this->astNode = $config['astNode'] ?? null;

src/Type/Definition/TypeWithFields.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ abstract class TypeWithFields extends Type implements HasFieldsType
1515
*
1616
* @var array<string, FieldDefinition>
1717
*/
18-
private array $fields;
18+
protected array $fields;
1919

20-
private function initializeFields(): void
20+
protected function initializeFields(): void
2121
{
2222
if (isset($this->fields)) {
2323
return;

src/Type/Schema.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -125,9 +125,6 @@ public function __construct($config)
125125
$this->config = $config;
126126
$this->extensionASTNodes = $config->extensionASTNodes;
127127

128-
// TODO can we make the following assumption hold true?
129-
// No need to check for the existence of the root query type
130-
// since we already validated the schema thus it must exist.
131128
$query = $config->query;
132129
if ($query !== null) {
133130
$this->resolvedTypes[$query->name] = $query;

src/Type/SchemaValidationContext.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -84,14 +84,8 @@ public function getErrors(): array
8484

8585
public function validateRootTypes(): void
8686
{
87-
if ($this->schema->getQueryType() === null) {
88-
$this->reportError(
89-
'Query root type must be provided.',
90-
$this->schema->getAstNode()
91-
);
92-
}
93-
9487
// Triggers a type error if wrong
88+
$this->schema->getQueryType();
9589
$this->schema->getMutationType();
9690
$this->schema->getSubscriptionType();
9791
}

tests/Type/DefinitionTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,10 +300,9 @@ public function testInputObjectFieldPublicTypeIssetDeprecation(): void
300300
/**
301301
* @see it('defines a mutation schema')
302302
*/
303-
public function testDefinesAMutationSchema(): void
303+
public function testDefinesAMutationOnlySchema(): void
304304
{
305305
$schema = new Schema([
306-
'query' => $this->blogQuery,
307306
'mutation' => $this->blogMutation,
308307
]);
309308

tests/Type/IntrospectionTest.php

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,7 @@ class IntrospectionTest extends TestCase
2929
*/
3030
public function testExecutesAnIntrospectionQuery(): void
3131
{
32-
$emptySchema = new Schema([
33-
'query' => new ObjectType([
34-
'name' => 'QueryRoot',
35-
'fields' => ['a' => Type::string()],
36-
]),
37-
]);
32+
$emptySchema = new Schema([]);
3833

3934
$request = Introspection::getIntrospectionQuery([
4035
'descriptions' => false,
@@ -47,32 +42,9 @@ public function testExecutesAnIntrospectionQuery(): void
4742
[
4843
'mutationType' => null,
4944
'subscriptionType' => null,
50-
'queryType' =>
51-
['name' => 'QueryRoot'],
45+
'queryType' => null,
5246
'types' =>
5347
[
54-
[
55-
'kind' => 'OBJECT',
56-
'name' => 'QueryRoot',
57-
'inputFields' => null,
58-
'interfaces' =>
59-
[],
60-
'enumValues' => null,
61-
'possibleTypes' => null,
62-
'fields' => [
63-
[
64-
'name' => 'a',
65-
'args' => [],
66-
'type' => [
67-
'kind' => 'SCALAR',
68-
'name' => 'String',
69-
'ofType' => null,
70-
],
71-
'isDeprecated' => false,
72-
'deprecationReason' => null,
73-
],
74-
],
75-
],
7648
[
7749
'kind' => 'SCALAR',
7850
'name' => 'String',

tests/Type/ValidationTest.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ public function testRejectsASchemaWithoutAQueryType(): void
315315

316316
$this->assertMatchesValidationMessage(
317317
$schema->validate(),
318-
[['message' => 'Query root type must be provided.']]
318+
[]
319319
);
320320

321321
$schemaWithDef = BuildSchema::build('
@@ -330,12 +330,7 @@ public function testRejectsASchemaWithoutAQueryType(): void
330330

331331
$this->assertMatchesValidationMessage(
332332
$schemaWithDef->validate(),
333-
[
334-
[
335-
'message' => 'Query root type must be provided.',
336-
'locations' => [['line' => 2, 'column' => 7]],
337-
],
338-
]
333+
[]
339334
);
340335
}
341336

0 commit comments

Comments
 (0)