Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make root query operation type optional #920

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
12 changes: 5 additions & 7 deletions src/Type/Definition/ObjectType.php
Original file line number Diff line number Diff line change
Expand Up @@ -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:
*
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/Type/Definition/TypeWithFields.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ abstract class TypeWithFields extends Type implements HasFieldsType
*
* @var array<string, FieldDefinition>
*/
private array $fields;
protected array $fields;

private function initializeFields(): void
protected function initializeFields(): void
{
if (isset($this->fields)) {
return;
Expand Down
3 changes: 0 additions & 3 deletions src/Type/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
8 changes: 1 addition & 7 deletions src/Type/SchemaValidationContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
3 changes: 1 addition & 2 deletions tests/Type/DefinitionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]);

Expand Down
32 changes: 2 additions & 30 deletions tests/Type/IntrospectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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',
Expand Down
9 changes: 2 additions & 7 deletions tests/Type/ValidationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ public function testRejectsASchemaWithoutAQueryType(): void

$this->assertMatchesValidationMessage(
$schema->validate(),
[['message' => 'Query root type must be provided.']]
[]
);

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

$this->assertMatchesValidationMessage(
$schemaWithDef->validate(),
[
[
'message' => 'Query root type must be provided.',
'locations' => [['line' => 2, 'column' => 7]],
],
]
[]
);
}

Expand Down