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

[0.12][WIP] Small upstream updates #241

Closed
wants to merge 4 commits into from
Closed
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
1 change: 0 additions & 1 deletion src/Language/Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,6 @@ function parseOperationType()
switch ($operationToken->value) {
case 'query': return 'query';
case 'mutation': return 'mutation';
// Note: subscription is an experimental non-spec addition.
case 'subscription': return 'subscription';
}

Expand Down
125 changes: 58 additions & 67 deletions src/Utils/BuildSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
use GraphQL\Language\AST\NodeKind;
use GraphQL\Language\AST\ObjectTypeDefinitionNode;
use GraphQL\Language\AST\ScalarTypeDefinitionNode;
use GraphQL\Language\AST\TypeDefinitionNode;
use GraphQL\Language\AST\SchemaDefinitionNode;
use GraphQL\Language\AST\TypeNode;
use GraphQL\Language\AST\UnionTypeDefinitionNode;
use GraphQL\Language\Parser;
Expand Down Expand Up @@ -41,7 +41,7 @@ class BuildSchema
/**
* @param Type $innerType
* @param TypeNode $inputTypeNode
* @return Type
* @return Type
*/
private function buildWrappedType(Type $innerType, TypeNode $inputTypeNode)
{
Expand Down Expand Up @@ -99,9 +99,10 @@ public function __construct(DocumentNode $ast, callable $typeConfigDecorator = n
$this->typeConfigDecorator = $typeConfigDecorator;
$this->loadedTypeDefs = [];
}

public function buildSchema()
{
/** @var SchemaDefinitionNode $schemaDef */
$schemaDef = null;
$typeDefs = [];
$this->nodeMap = [];
Expand Down Expand Up @@ -133,61 +134,13 @@ public function buildSchema()
}
}

$queryTypeName = null;
$mutationTypeName = null;
$subscriptionTypeName = null;
if ($schemaDef) {
foreach ($schemaDef->operationTypes as $operationType) {
$typeName = $operationType->type->name->value;
if ($operationType->operation === 'query') {
if ($queryTypeName) {
throw new Error('Must provide only one query type in schema.');
}
if (!isset($this->nodeMap[$typeName])) {
throw new Error(
'Specified query type "' . $typeName . '" not found in document.'
);
}
$queryTypeName = $typeName;
} else if ($operationType->operation === 'mutation') {
if ($mutationTypeName) {
throw new Error('Must provide only one mutation type in schema.');
}
if (!isset($this->nodeMap[$typeName])) {
throw new Error(
'Specified mutation type "' . $typeName . '" not found in document.'
);
}
$mutationTypeName = $typeName;
} else if ($operationType->operation === 'subscription') {
if ($subscriptionTypeName) {
throw new Error('Must provide only one subscription type in schema.');
}
if (!isset($this->nodeMap[$typeName])) {
throw new Error(
'Specified subscription type "' . $typeName . '" not found in document.'
);
}
$subscriptionTypeName = $typeName;
}
}
} else {
if (isset($this->nodeMap['Query'])) {
$queryTypeName = 'Query';
}
if (isset($this->nodeMap['Mutation'])) {
$mutationTypeName = 'Mutation';
}
if (isset($this->nodeMap['Subscription'])) {
$subscriptionTypeName = 'Subscription';
}
}

if (!$queryTypeName) {
throw new Error(
'Must provide schema definition with query type or a type named Query.'
);
}
$operationTypes = $schemaDef
? $this->getOperationTypes($schemaDef)
: [
'query' => isset($this->nodeMap['Query']) ? 'Query' : null,
'mutation' => isset($this->nodeMap['Mutation']) ? 'Mutation' : null,
'subscription' => isset($this->nodeMap['Subscription']) ? 'Subscription' : null,
];

$this->innerTypeMap = [
'String' => Type::string(),
Expand Down Expand Up @@ -229,13 +182,19 @@ public function buildSchema()
$directives[] = Directive::deprecatedDirective();
}

if (!isset($operationTypes['query'])) {
throw new Error(
'Must provide schema definition with query type or a type named Query.'
);
}

$schema = new Schema([
'query' => $this->getObjectType($this->nodeMap[$queryTypeName]),
'mutation' => $mutationTypeName ?
$this->getObjectType($this->nodeMap[$mutationTypeName]) :
'query' => $this->getObjectType($operationTypes['query']),
'mutation' => isset($operationTypes['mutation']) ?
$this->getObjectType($operationTypes['mutation']) :
null,
'subscription' => $subscriptionTypeName ?
$this->getObjectType($this->nodeMap[$subscriptionTypeName]) :
'subscription' => isset($operationTypes['subscription']) ?
$this->getObjectType($operationTypes['subscription']) :
null,
'typeLoader' => function($name) {
return $this->typeDefNamed($name);
Expand All @@ -256,6 +215,33 @@ public function buildSchema()
return $schema;
}

/**
* @param SchemaDefinitionNode $schemaDef
* @return array
* @throws Error
*/
private function getOperationTypes($schemaDef)
{
$opTypes = [];

foreach ($schemaDef->operationTypes as $operationType) {
$typeName = $operationType->type->name->value;
$operation = $operationType->operation;

if (isset($opTypes[$operation])) {
throw new Error("Must provide only one $operation type in schema.");
}

if (!isset($this->nodeMap[$typeName])) {
throw new Error("Specified $operation type \"$typeName\" not found in document.");
}

$opTypes[$operation] = $typeName;
}

return $opTypes;
}

private function getDirective(DirectiveDefinitionNode $directiveNode)
{
return new Directive([
Expand All @@ -269,9 +255,14 @@ private function getDirective(DirectiveDefinitionNode $directiveNode)
]);
}

private function getObjectType(TypeDefinitionNode $typeNode)
/**
* @param string $name
* @return CustomScalarType|EnumType|InputObjectType|UnionType
* @throws Error
*/
private function getObjectType($name)
{
$type = $this->typeDefNamed($typeNode->name->value);
$type = $this->typeDefNamed($name);
Utils::invariant(
$type instanceof ObjectType,
'AST must provide object type.'
Expand Down Expand Up @@ -619,7 +610,7 @@ public function getDescription($node)
/**
* A helper function to build a GraphQLSchema directly from a source
* document.
*
*
* @api
* @param DocumentNode|Source|string $source
* @param callable $typeConfigDecorator
Expand All @@ -644,4 +635,4 @@ public function cannotExecuteSchema()
);
}

}
}
Loading