public static isCompositeType ( $type ) : boolean | ||
$type | ||
return | boolean |
public function __invoke(ValidationContext $context) { return [Node::INLINE_FRAGMENT => function (InlineFragment $node) use($context) { $type = $context->getType(); if ($node->typeCondition && $type && !Type::isCompositeType($type)) { $context->reportError(new Error(static::inlineFragmentOnNonCompositeErrorMessage($type), [$node->typeCondition])); } }, Node::FRAGMENT_DEFINITION => function (FragmentDefinition $node) use($context) { $type = $context->getType(); if ($type && !Type::isCompositeType($type)) { $context->reportError(new Error(static::fragmentOnNonCompositeErrorMessage($node->name->value, Printer::doPrint($node->typeCondition)), [$node->typeCondition])); } }]; }
/** * @param Node $node */ function enter(Node $node) { $schema = $this->schema; switch ($node->kind) { case Node::SELECTION_SET: $namedType = Type::getNamedType($this->getType()); $compositeType = null; if (Type::isCompositeType($namedType)) { // isCompositeType is a type refining predicate, so this is safe. $compositeType = $namedType; } $this->parentTypeStack[] = $compositeType; // push break; case Node::FIELD: $parentType = $this->getParentType(); $fieldDef = null; if ($parentType) { $fieldDef = self::getFieldDefinition($schema, $parentType, $node); } $this->fieldDefStack[] = $fieldDef; // push $this->typeStack[] = $fieldDef ? $fieldDef->getType() : null; // push break; case Node::DIRECTIVE: $this->directive = $schema->getDirective($node->name->value); break; case Node::OPERATION_DEFINITION: $type = null; if ($node->operation === 'query') { $type = $schema->getQueryType(); } else { if ($node->operation === 'mutation') { $type = $schema->getMutationType(); } else { if ($node->operation === 'subscription') { $type = $schema->getSubscriptionType(); } } } $this->typeStack[] = $type; // push break; case Node::INLINE_FRAGMENT: case Node::FRAGMENT_DEFINITION: $typeConditionAST = $node->typeCondition; $outputType = $typeConditionAST ? self::typeFromAST($schema, $typeConditionAST) : $this->getType(); $this->typeStack[] = $outputType; // push break; case Node::VARIABLE_DEFINITION: $inputType = self::typeFromAST($schema, $node->type); $this->inputTypeStack[] = $inputType; // push break; case Node::ARGUMENT: $fieldOrDirective = $this->getDirective() ?: $this->getFieldDef(); $argDef = $argType = null; if ($fieldOrDirective) { $argDef = Utils::find($fieldOrDirective->args, function ($arg) use($node) { return $arg->name === $node->name->value; }); if ($argDef) { $argType = $argDef->getType(); } } $this->argument = $argDef; $this->inputTypeStack[] = $argType; // push break; case Node::LST: $listType = Type::getNullableType($this->getInputType()); $this->inputTypeStack[] = $listType instanceof ListOfType ? $listType->getWrappedType() : null; // push break; case Node::OBJECT_FIELD: $objectType = Type::getNamedType($this->getInputType()); $fieldType = null; if ($objectType instanceof InputObjectType) { $tmp = $objectType->getFields(); $inputField = isset($tmp[$node->name->value]) ? $tmp[$node->name->value] : null; $fieldType = $inputField ? $inputField->getType() : null; } $this->inputTypeStack[] = $fieldType; break; } }
/** * @it maintains type info during edit */ public function testMaintainsTypeInfoDuringEdit() { $visited = []; $typeInfo = new TypeInfo(TestCase::getDefaultSchema()); $ast = Parser::parse('{ human(id: 4) { name, pets }, alien }'); $editedAst = Visitor::visit($ast, Visitor::visitWithTypeInfo($typeInfo, ['enter' => function ($node) use($typeInfo, &$visited) { $parentType = $typeInfo->getParentType(); $type = $typeInfo->getType(); $inputType = $typeInfo->getInputType(); $visited[] = ['enter', $node->kind, $node->kind === 'Name' ? $node->value : null, $parentType ? (string) $parentType : null, $type ? (string) $type : null, $inputType ? (string) $inputType : null]; // Make a query valid by adding missing selection sets. if ($node->kind === 'Field' && !$node->selectionSet && Type::isCompositeType(Type::getNamedType($type))) { return new FieldNode(['alias' => $node->alias, 'name' => $node->name, 'arguments' => $node->arguments, 'directives' => $node->directives, 'selectionSet' => new SelectionSetNode(['kind' => 'SelectionSet', 'selections' => [new FieldNode(['name' => new NameNode(['value' => '__typename'])])]])]); } }, 'leave' => function ($node) use($typeInfo, &$visited) { $parentType = $typeInfo->getParentType(); $type = $typeInfo->getType(); $inputType = $typeInfo->getInputType(); $visited[] = ['leave', $node->kind, $node->kind === 'Name' ? $node->value : null, $parentType ? (string) $parentType : null, $type ? (string) $type : null, $inputType ? (string) $inputType : null]; }])); $this->assertEquals(Printer::doPrint(Parser::parse('{ human(id: 4) { name, pets }, alien }')), Printer::doPrint($ast)); $this->assertEquals(Printer::doPrint(Parser::parse('{ human(id: 4) { name, pets { __typename } }, alien { __typename } }')), Printer::doPrint($editedAst)); $this->assertEquals([['enter', 'Document', null, null, null, null], ['enter', 'OperationDefinition', null, null, 'QueryRoot', null], ['enter', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], ['enter', 'Field', null, 'QueryRoot', 'Human', null], ['enter', 'Name', 'human', 'QueryRoot', 'Human', null], ['leave', 'Name', 'human', 'QueryRoot', 'Human', null], ['enter', 'Argument', null, 'QueryRoot', 'Human', 'ID'], ['enter', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], ['leave', 'Name', 'id', 'QueryRoot', 'Human', 'ID'], ['enter', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], ['leave', 'IntValue', null, 'QueryRoot', 'Human', 'ID'], ['leave', 'Argument', null, 'QueryRoot', 'Human', 'ID'], ['enter', 'SelectionSet', null, 'Human', 'Human', null], ['enter', 'Field', null, 'Human', 'String', null], ['enter', 'Name', 'name', 'Human', 'String', null], ['leave', 'Name', 'name', 'Human', 'String', null], ['leave', 'Field', null, 'Human', 'String', null], ['enter', 'Field', null, 'Human', '[Pet]', null], ['enter', 'Name', 'pets', 'Human', '[Pet]', null], ['leave', 'Name', 'pets', 'Human', '[Pet]', null], ['enter', 'SelectionSet', null, 'Pet', '[Pet]', null], ['enter', 'Field', null, 'Pet', 'String!', null], ['enter', 'Name', '__typename', 'Pet', 'String!', null], ['leave', 'Name', '__typename', 'Pet', 'String!', null], ['leave', 'Field', null, 'Pet', 'String!', null], ['leave', 'SelectionSet', null, 'Pet', '[Pet]', null], ['leave', 'Field', null, 'Human', '[Pet]', null], ['leave', 'SelectionSet', null, 'Human', 'Human', null], ['leave', 'Field', null, 'QueryRoot', 'Human', null], ['enter', 'Field', null, 'QueryRoot', 'Alien', null], ['enter', 'Name', 'alien', 'QueryRoot', 'Alien', null], ['leave', 'Name', 'alien', 'QueryRoot', 'Alien', null], ['enter', 'SelectionSet', null, 'Alien', 'Alien', null], ['enter', 'Field', null, 'Alien', 'String!', null], ['enter', 'Name', '__typename', 'Alien', 'String!', null], ['leave', 'Name', '__typename', 'Alien', 'String!', null], ['leave', 'Field', null, 'Alien', 'String!', null], ['leave', 'SelectionSet', null, 'Alien', 'Alien', null], ['leave', 'Field', null, 'QueryRoot', 'Alien', null], ['leave', 'SelectionSet', null, 'QueryRoot', 'QueryRoot', null], ['leave', 'OperationDefinition', null, null, 'QueryRoot', null], ['leave', 'Document', null, null, null, null]], $visited); }
function enter(Node $node) { $schema = $this->_schema; switch ($node->kind) { case Node::SELECTION_SET: // var $compositeType: ?GraphQLCompositeType; $rawType = Type::getUnmodifiedType($this->getType()); $compositeType = null; if (Type::isCompositeType($rawType)) { // isCompositeType is a type refining predicate, so this is safe. $compositeType = $rawType; } array_push($this->_parentTypeStack, $compositeType); break; case Node::FIELD: $parentType = $this->getParentType(); $fieldDef = null; if ($parentType) { $fieldDef = self::_getFieldDef($schema, $parentType, $node); } array_push($this->_fieldDefStack, $fieldDef); array_push($this->_typeStack, $fieldDef ? $fieldDef->getType() : null); break; case Node::OPERATION_DEFINITION: $type = null; if ($node->operation === 'query') { $type = $schema->getQueryType(); } else { if ($node->operation === 'mutation') { $type = $schema->getMutationType(); } } array_push($this->_typeStack, $type); break; case Node::INLINE_FRAGMENT: case Node::FRAGMENT_DEFINITION: $type = $schema->getType($node->typeCondition->value); array_push($this->_typeStack, $type); break; case Node::VARIABLE_DEFINITION: array_push($this->_inputTypeStack, self::typeFromAST($schema, $node->type)); break; case Node::ARGUMENT: $field = $this->getFieldDef(); $argType = null; if ($field) { $argDef = Utils::find($field->args, function ($arg) use($node) { return $arg->name === $node->name->value; }); if ($argDef) { $argType = $argDef->getType(); } } array_push($this->_inputTypeStack, $argType); break; case Node::DIRECTIVE: $directive = $schema->getDirective($node->name->value); array_push($this->_inputTypeStack, $directive ? $directive->type : null); break; case Node::ARR: $arrayType = Type::getNullableType($this->getInputType()); array_push($this->_inputTypeStack, $arrayType instanceof ListOfType ? $arrayType->getWrappedType() : null); break; case Node::OBJECT_FIELD: $objectType = Type::getUnmodifiedType($this->getInputType()); $fieldType = null; if ($objectType instanceof InputObjectType) { $tmp = $objectType->getFields(); $inputField = isset($tmp[$node->name->value]) ? $tmp[$node->name->value] : null; $fieldType = $inputField ? $inputField->getType() : null; } array_push($this->_inputTypeStack, $fieldType); break; } }