From c6a39289b296dad9d469d8a9aef1eb46a5bfaf26 Mon Sep 17 00:00:00 2001 From: Benedikt Franke Date: Mon, 23 Aug 2021 18:43:38 +0200 Subject: [PATCH] Make root query operation type optional https://github.com/graphql/graphql-spec/pull/631 --- src/Executor/ReferenceExecutor.php | 10 -------- src/Type/Definition/ObjectType.php | 12 ++++------ src/Type/Definition/TypeWithFields.php | 4 ++-- src/Type/Schema.php | 3 --- src/Type/SchemaValidationContext.php | 8 +------ tests/Type/DefinitionTest.php | 3 +-- tests/Type/IntrospectionTest.php | 32 ++------------------------ tests/Type/ValidationTest.php | 9 ++------ 8 files changed, 13 insertions(+), 68 deletions(-) diff --git a/src/Executor/ReferenceExecutor.php b/src/Executor/ReferenceExecutor.php index 4d085d04c..61d364937 100644 --- a/src/Executor/ReferenceExecutor.php +++ b/src/Executor/ReferenceExecutor.php @@ -608,16 +608,6 @@ protected function getFieldDef(Schema $schema, ObjectType $parentType, string $f $typeMetaFieldDef ??= Introspection::typeMetaFieldDef(); $typeNameMetaFieldDef ??= Introspection::typeNameMetaFieldDef(); - $queryType = $schema->getQueryType(); - - if ($fieldName === $schemaMetaFieldDef->name && $queryType === $parentType) { - return $schemaMetaFieldDef; - } - - if ($fieldName === $typeMetaFieldDef->name && $queryType === $parentType) { - return $typeMetaFieldDef; - } - if ($fieldName === $typeNameMetaFieldDef->name) { return $typeNameMetaFieldDef; } diff --git a/src/Type/Definition/ObjectType.php b/src/Type/Definition/ObjectType.php index 73cda6cf3..9f77e6ce1 100644 --- a/src/Type/Definition/ObjectType.php +++ b/src/Type/Definition/ObjectType.php @@ -21,8 +21,8 @@ /** * Object Type Definition * - * Almost all of the GraphQL types you define will be object types. Object types - * have a name, but most importantly describe their fields. + * Most GraphQL types you define will be object types. Object types + * have a name, but most importantly they describe their fields. * * Example: * @@ -87,13 +87,11 @@ class ObjectType extends TypeWithFields implements OutputType, CompositeType, Nu */ public function __construct(array $config) { - if (! isset($config['name'])) { - $config['name'] = $this->tryInferName(); - } + $name = $config['name'] ?? $this->tryInferName(); - Utils::invariant(is_string($config['name']), 'Must provide name.'); + Utils::invariant(is_string($name), 'Must provide name.'); - $this->name = $config['name']; + $this->name = $name; $this->description = $config['description'] ?? null; $this->resolveFieldFn = $config['resolveField'] ?? null; $this->astNode = $config['astNode'] ?? null; diff --git a/src/Type/Definition/TypeWithFields.php b/src/Type/Definition/TypeWithFields.php index 33a7031b3..ec80cd9bf 100644 --- a/src/Type/Definition/TypeWithFields.php +++ b/src/Type/Definition/TypeWithFields.php @@ -15,9 +15,9 @@ abstract class TypeWithFields extends Type implements HasFieldsType * * @var array */ - private array $fields; + protected array $fields; - private function initializeFields(): void + protected function initializeFields(): void { if (isset($this->fields)) { return; diff --git a/src/Type/Schema.php b/src/Type/Schema.php index 408445417..7b509a1fb 100644 --- a/src/Type/Schema.php +++ b/src/Type/Schema.php @@ -125,9 +125,6 @@ public function __construct($config) $this->config = $config; $this->extensionASTNodes = $config->extensionASTNodes; - // TODO can we make the following assumption hold true? - // No need to check for the existence of the root query type - // since we already validated the schema thus it must exist. $query = $config->query; if ($query !== null) { $this->resolvedTypes[$query->name] = $query; diff --git a/src/Type/SchemaValidationContext.php b/src/Type/SchemaValidationContext.php index 92c8d42df..3a93519a2 100644 --- a/src/Type/SchemaValidationContext.php +++ b/src/Type/SchemaValidationContext.php @@ -84,14 +84,8 @@ public function getErrors(): array public function validateRootTypes(): void { - if ($this->schema->getQueryType() === null) { - $this->reportError( - 'Query root type must be provided.', - $this->schema->getAstNode() - ); - } - // Triggers a type error if wrong + $this->schema->getQueryType(); $this->schema->getMutationType(); $this->schema->getSubscriptionType(); } diff --git a/tests/Type/DefinitionTest.php b/tests/Type/DefinitionTest.php index 79f318462..76b02e09c 100644 --- a/tests/Type/DefinitionTest.php +++ b/tests/Type/DefinitionTest.php @@ -300,10 +300,9 @@ public function testInputObjectFieldPublicTypeIssetDeprecation(): void /** * @see it('defines a mutation schema') */ - public function testDefinesAMutationSchema(): void + public function testDefinesAMutationOnlySchema(): void { $schema = new Schema([ - 'query' => $this->blogQuery, 'mutation' => $this->blogMutation, ]); diff --git a/tests/Type/IntrospectionTest.php b/tests/Type/IntrospectionTest.php index 10f1af005..10da97c0c 100644 --- a/tests/Type/IntrospectionTest.php +++ b/tests/Type/IntrospectionTest.php @@ -29,12 +29,7 @@ class IntrospectionTest extends TestCase */ public function testExecutesAnIntrospectionQuery(): void { - $emptySchema = new Schema([ - 'query' => new ObjectType([ - 'name' => 'QueryRoot', - 'fields' => ['a' => Type::string()], - ]), - ]); + $emptySchema = new Schema([]); $request = Introspection::getIntrospectionQuery([ 'descriptions' => false, @@ -47,32 +42,9 @@ public function testExecutesAnIntrospectionQuery(): void [ 'mutationType' => null, 'subscriptionType' => null, - 'queryType' => - ['name' => 'QueryRoot'], + 'queryType' => null, 'types' => [ - [ - 'kind' => 'OBJECT', - 'name' => 'QueryRoot', - 'inputFields' => null, - 'interfaces' => - [], - 'enumValues' => null, - 'possibleTypes' => null, - 'fields' => [ - [ - 'name' => 'a', - 'args' => [], - 'type' => [ - 'kind' => 'SCALAR', - 'name' => 'String', - 'ofType' => null, - ], - 'isDeprecated' => false, - 'deprecationReason' => null, - ], - ], - ], [ 'kind' => 'SCALAR', 'name' => 'String', diff --git a/tests/Type/ValidationTest.php b/tests/Type/ValidationTest.php index 501c59e2c..1fbe43100 100644 --- a/tests/Type/ValidationTest.php +++ b/tests/Type/ValidationTest.php @@ -315,7 +315,7 @@ public function testRejectsASchemaWithoutAQueryType(): void $this->assertMatchesValidationMessage( $schema->validate(), - [['message' => 'Query root type must be provided.']] + [] ); $schemaWithDef = BuildSchema::build(' @@ -330,12 +330,7 @@ public function testRejectsASchemaWithoutAQueryType(): void $this->assertMatchesValidationMessage( $schemaWithDef->validate(), - [ - [ - 'message' => 'Query root type must be provided.', - 'locations' => [['line' => 2, 'column' => 7]], - ], - ] + [] ); }