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

Transfer config in schema extender #1672

Draft
wants to merge 5 commits 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
4 changes: 2 additions & 2 deletions benchmarks/HugeSchemaBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ private function createLazySchema(): Schema
{
return new Schema(
(new SchemaConfig())
->setQuery($this->schemaGenerator->buildQueryType())
->setTypeLoader(fn (string $name): Type => $this->schemaGenerator->loadType($name))
->setQuery($this->schemaGenerator->buildQueryType())
->setTypeLoader(fn (string $name): Type => $this->schemaGenerator->loadType($name))
);
}
}
2 changes: 1 addition & 1 deletion benchmarks/Utils/SchemaGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public function buildSchema(): Schema
{
return new Schema(
(new SchemaConfig())
->setQuery($this->buildQueryType())
->setQuery($this->buildQueryType())
);
}

Expand Down
4 changes: 2 additions & 2 deletions examples/00-hello-world/graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
// https://webonyx.github.io/graphql-php/schema-definition/#configuration-options
$schema = new Schema(
(new SchemaConfig())
->setQuery($queryType)
->setMutation($mutationType)
->setQuery($queryType)
->setMutation($mutationType)
);

$rawInput = file_get_contents('php://input');
Expand Down
4 changes: 2 additions & 2 deletions examples/03-standard-server/graphql.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
// https://webonyx.github.io/graphql-php/schema-definition/#configuration-options
$schema = new Schema(
(new SchemaConfig())
->setQuery($queryType)
->setMutation($mutationType)
->setQuery($queryType)
->setMutation($mutationType)
);

$rootValue = ['prefix' => 'You said: '];
Expand Down
26 changes: 5 additions & 21 deletions src/Executor/ReferenceExecutor.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,41 +355,29 @@ protected function getOperationRootType(Schema $schema, OperationDefinitionNode
case 'query':
$queryType = $schema->getQueryType();
if ($queryType === null) {
throw new Error(
'Schema does not define the required query root type.',
[$operation]
);
throw new Error('Schema does not define the required query root type.', [$operation]);
}

return $queryType;

case 'mutation':
$mutationType = $schema->getMutationType();
if ($mutationType === null) {
throw new Error(
'Schema is not configured for mutations.',
[$operation]
);
throw new Error('Schema is not configured for mutations.', [$operation]);
}

return $mutationType;

case 'subscription':
$subscriptionType = $schema->getSubscriptionType();
if ($subscriptionType === null) {
throw new Error(
'Schema is not configured for subscriptions.',
[$operation]
);
throw new Error('Schema is not configured for subscriptions.', [$operation]);
}

return $subscriptionType;

default:
throw new Error(
'Can only execute queries, mutations and subscriptions.',
[$operation]
);
throw new Error('Can only execute queries, mutations and subscriptions.', [$operation]);
}
}

Expand Down Expand Up @@ -1075,11 +1063,7 @@ protected function completeLeafValue(LeafType $returnType, &$result)
} catch (\Throwable $error) {
$safeReturnType = Utils::printSafe($returnType);
$safeResult = Utils::printSafe($result);
throw new InvariantViolation(
"Expected a value of type {$safeReturnType} but received: {$safeResult}. {$error->getMessage()}",
0,
$error
);
throw new InvariantViolation("Expected a value of type {$safeReturnType} but received: {$safeResult}. {$error->getMessage()}", 0, $error);
}
}

Expand Down
20 changes: 4 additions & 16 deletions src/Executor/Values.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,23 +238,14 @@ public static function getArgumentValuesForMap($def, array $argumentValueMap, ?a
$safeArgType = Utils::printSafe($argType);

if ($isNull) {
throw new Error(
"Argument \"{$name}\" of non-null type \"{$safeArgType}\" must not be null.",
$referenceNode
);
throw new Error("Argument \"{$name}\" of non-null type \"{$safeArgType}\" must not be null.", $referenceNode);
}

if ($argumentValueNode instanceof VariableNode) {
throw new Error(
"Argument \"{$name}\" of required type \"{$safeArgType}\" was provided the variable \"\${$argumentValueNode->name->value}\" which was not provided a runtime value.",
[$argumentValueNode]
);
throw new Error("Argument \"{$name}\" of required type \"{$safeArgType}\" was provided the variable \"\${$argumentValueNode->name->value}\" which was not provided a runtime value.", [$argumentValueNode]);
}

throw new Error(
"Argument \"{$name}\" of required type \"{$safeArgType}\" was not provided.",
$referenceNode
);
throw new Error("Argument \"{$name}\" of required type \"{$safeArgType}\" was not provided.", $referenceNode);
} elseif ($hasValue) {
assert($argumentValueNode instanceof Node);

Expand All @@ -275,10 +266,7 @@ public static function getArgumentValuesForMap($def, array $argumentValueMap, ?a
// execution. This is a runtime check to ensure execution does not
// continue with an invalid argument value.
$invalidValue = Printer::doPrint($argumentValueNode);
throw new Error(
"Argument \"{$name}\" has invalid value {$invalidValue}.",
[$argumentValueNode]
);
throw new Error("Argument \"{$name}\" has invalid value {$invalidValue}.", [$argumentValueNode]);
}

$coercedValues[$name] = $coercedValue;
Expand Down
48 changes: 8 additions & 40 deletions src/Language/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,7 @@ private function readToken(Token $prev): Token
->readString($line, $col, $prev);
}

throw new SyntaxError(
$this->source,
$position,
$this->unexpectedCharacterMessage($code)
);
throw new SyntaxError($this->source, $position, $this->unexpectedCharacterMessage($code));
}

/** @throws \JsonException */
Expand Down Expand Up @@ -329,11 +325,7 @@ private function readNumber(int $line, int $col, Token $prev): Token
[$char, $code] = $this->moveStringCursor(1, 1)->readChar();

if ($code >= 48 && $code <= 57) {
throw new SyntaxError(
$this->source,
$this->position,
'Invalid number, unexpected digit after 0: ' . Utils::printCharCode($code)
);
throw new SyntaxError($this->source, $this->position, 'Invalid number, unexpected digit after 0: ' . Utils::printCharCode($code));
}
} else {
$value .= $this->readDigits();
Expand Down Expand Up @@ -398,11 +390,7 @@ private function readDigits(): string
$code = null;
}

throw new SyntaxError(
$this->source,
$this->position,
'Invalid number, expected digit but got: ' . Utils::printCharCode($code)
);
throw new SyntaxError($this->source, $this->position, 'Invalid number, expected digit but got: ' . Utils::printCharCode($code));
}

/**
Expand Down Expand Up @@ -477,11 +465,7 @@ private function readString(int $line, int $col, Token $prev): Token
$position = $this->position;
[$hex] = $this->readChars(4);
if (\preg_match('/[0-9a-fA-F]{4}/', $hex) !== 1) {
throw new SyntaxError(
$this->source,
$position - 1,
"Invalid character escape sequence: \\u{$hex}"
);
throw new SyntaxError($this->source, $position - 1, "Invalid character escape sequence: \\u{$hex}");
}

$code = \hexdec($hex);
Expand All @@ -492,11 +476,7 @@ private function readString(int $line, int $col, Token $prev): Token
if ($highOrderByte >= 0xD8 && $highOrderByte <= 0xDF) {
[$utf16Continuation] = $this->readChars(6);
if (\preg_match('/^\\\u[0-9a-fA-F]{4}$/', $utf16Continuation) !== 1) {
throw new SyntaxError(
$this->source,
$this->position - 5,
'Invalid UTF-16 trailing surrogate: ' . $utf16Continuation
);
throw new SyntaxError($this->source, $this->position - 5, 'Invalid UTF-16 trailing surrogate: ' . $utf16Continuation);
}

$surrogatePairHex = $hex . \substr($utf16Continuation, 2, 4);
Expand All @@ -513,11 +493,7 @@ private function readString(int $line, int $col, Token $prev): Token
continue 2;
default:
$chr = Utils::chr($code);
throw new SyntaxError(
$this->source,
$this->position - 1,
"Invalid character escape sequence: \\{$chr}"
);
throw new SyntaxError($this->source, $this->position - 1, "Invalid character escape sequence: \\{$chr}");
}

$chunk = '';
Expand All @@ -528,11 +504,7 @@ private function readString(int $line, int $col, Token $prev): Token
[$char, $code, $bytes] = $this->readChar();
}

throw new SyntaxError(
$this->source,
$this->position,
'Unterminated string.'
);
throw new SyntaxError($this->source, $this->position, 'Unterminated string.');
}

/**
Expand Down Expand Up @@ -612,11 +584,7 @@ private function readBlockString(int $line, int $col, Token $prev): Token
[$char, $code, $bytes] = $this->readChar();
}

throw new SyntaxError(
$this->source,
$this->position,
'Unterminated string.'
);
throw new SyntaxError($this->source, $this->position, 'Unterminated string.');
}

/**
Expand Down
12 changes: 2 additions & 10 deletions src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,7 @@ private function expect(string $kind): Token
return $token;
}

throw new SyntaxError(
$this->lexer->source,
$token->start,
"Expected {$kind}, found {$token->getDescription()}"
);
throw new SyntaxError($this->lexer->source, $token->start, "Expected {$kind}, found {$token->getDescription()}");
}

/**
Expand All @@ -397,11 +393,7 @@ private function expectKeyword(string $value): void
{
$token = $this->lexer->token;
if ($token->kind !== Token::NAME || $token->value !== $value) {
throw new SyntaxError(
$this->lexer->source,
$token->start,
"Expected \"{$value}\", found {$token->getDescription()}"
);
throw new SyntaxError($this->lexer->source, $token->start, "Expected \"{$value}\", found {$token->getDescription()}");
}

$this->lexer->advance();
Expand Down
5 changes: 1 addition & 4 deletions src/Type/Definition/EnumType.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,7 @@ public function parseLiteral(Node $valueNode, ?array $variables = null)
{
if (! $valueNode instanceof EnumValueNode) {
$valueStr = Printer::doPrint($valueNode);
throw new Error(
"Enum \"{$this->name}\" cannot represent non-enum value: {$valueStr}.{$this->didYouMean($valueStr)}",
$valueNode
);
throw new Error("Enum \"{$this->name}\" cannot represent non-enum value: {$valueStr}.{$this->didYouMean($valueStr)}", $valueNode);
}

$name = $valueNode->value;
Expand Down
9 changes: 1 addition & 8 deletions src/Utils/ASTDefinitionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,7 @@ private function internalBuildType(string $typeName, ?Node $typeNode = null): Ty
);
} catch (\Throwable $e) {
$class = static::class;
throw new Error(
"Type config decorator passed to {$class} threw an error when building {$typeName} type: {$e->getMessage()}",
null,
null,
[],
null,
$e
);
throw new Error("Type config decorator passed to {$class} threw an error when building {$typeName} type: {$e->getMessage()}", null, null, [], null, $e);
}

// @phpstan-ignore-next-line should not happen, but function types are not enforced by PHP
Expand Down
12 changes: 6 additions & 6 deletions src/Utils/BuildClientSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ public function buildSchema(): Schema

return new Schema(
(new SchemaConfig())
->setQuery($queryType)
->setMutation($mutationType)
->setSubscription($subscriptionType)
->setTypes($this->typeMap)
->setDirectives($directives)
->setAssumeValid($this->options['assumeValid'] ?? false)
->setQuery($queryType)
->setMutation($mutationType)
->setSubscription($subscriptionType)
->setTypes($this->typeMap)
->setDirectives($directives)
->setAssumeValid($this->options['assumeValid'] ?? false)
);
}

Expand Down
32 changes: 16 additions & 16 deletions src/Utils/BuildSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,24 @@ static function (string $typeName): Type {
return new Schema(
(new SchemaConfig())
// @phpstan-ignore-next-line
->setQuery(isset($operationTypes['query'])
? $definitionBuilder->maybeBuildType($operationTypes['query'])
: null)
->setQuery(isset($operationTypes['query'])
? $definitionBuilder->maybeBuildType($operationTypes['query'])
: null)
// @phpstan-ignore-next-line
->setMutation(isset($operationTypes['mutation'])
? $definitionBuilder->maybeBuildType($operationTypes['mutation'])
: null)
->setMutation(isset($operationTypes['mutation'])
? $definitionBuilder->maybeBuildType($operationTypes['mutation'])
: null)
// @phpstan-ignore-next-line
->setSubscription(isset($operationTypes['subscription'])
? $definitionBuilder->maybeBuildType($operationTypes['subscription'])
: null)
->setTypeLoader(static fn (string $name): ?Type => $definitionBuilder->maybeBuildType($name))
->setDirectives($directives)
->setAstNode($schemaDef)
->setTypes(fn (): array => \array_map(
static fn (TypeDefinitionNode $def): Type => $definitionBuilder->buildType($def->getName()->value),
$typeDefinitionsMap,
))
->setSubscription(isset($operationTypes['subscription'])
? $definitionBuilder->maybeBuildType($operationTypes['subscription'])
: null)
->setTypeLoader(static fn (string $name): ?Type => $definitionBuilder->maybeBuildType($name))
->setDirectives($directives)
->setAstNode($schemaDef)
->setTypes(fn (): array => \array_map(
static fn (TypeDefinitionNode $def): Type => $definitionBuilder->buildType($def->getName()->value),
$typeDefinitionsMap,
))
);
}

Expand Down
1 change: 1 addition & 0 deletions src/Utils/SchemaExtender.php
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,7 @@ protected function extendFieldMap(Type $type): array
$field = $oldFieldMap[$fieldName];

$newFieldMap[$fieldName] = [
...$field->config,
'name' => $fieldName,
'description' => $field->description,
'deprecationReason' => $field->deprecationReason,
Expand Down
16 changes: 2 additions & 14 deletions src/Validator/Rules/QueryComplexity.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,7 @@ protected function directiveExcludesField(FieldNode $node): bool
$this->getRawVariableValues()
);
if ($errors !== null && $errors !== []) {
throw new Error(\implode(
"\n\n",
\array_map(
static fn (Error $error): string => $error->getMessage(),
$errors
)
));
throw new Error(\implode("\n\n", \array_map(static fn (Error $error): string => $error->getMessage(), $errors)));
}

if ($directiveNode->name->value === Directive::INCLUDE_NAME) {
Expand Down Expand Up @@ -248,13 +242,7 @@ protected function buildFieldArguments(FieldNode $node): array
);

if (is_array($errors) && $errors !== []) {
throw new Error(\implode(
"\n\n",
\array_map(
static fn ($error) => $error->getMessage(),
$errors
)
));
throw new Error(\implode("\n\n", \array_map(static fn ($error) => $error->getMessage(), $errors)));
}

$args = Values::getArgumentValues($fieldDef, $node, $variableValues);
Expand Down
Loading