public function testDefinesAQueryOnlySchema() { $blogSchema = new Schema($this->blogQuery); $this->assertSame($this->blogQuery, $blogSchema->getQueryType()); /** @var \Fubhy\GraphQL\Type\Definition\FieldDefinition $articleField */ /** @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType $articleFieldType */ $articleField = $this->blogQuery->getFields()['article']; $articleFieldType = $articleField->getType(); $this->assertEquals('article', $articleField->getName()); $this->assertSame($this->blogArticle, $articleFieldType); $this->assertEquals('Article', $articleFieldType->getName()); /** @var \Fubhy\GraphQL\Type\Definition\FieldDefinition $titleField */ /** @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType $titleFieldType */ $titleField = $articleFieldType->getFields()['title']; $titleFieldType = $titleField->getType(); $this->assertEquals('title', $titleField->getName()); $this->assertEquals(Type::stringType(), $titleFieldType); $this->assertEquals('String', $titleFieldType->getName()); /** @var \Fubhy\GraphQL\Type\Definition\FieldDefinition $authorField */ /** @var \Fubhy\GraphQL\Type\Definition\Types\ObjectType $authorFieldType */ /** @var \Fubhy\GraphQL\Type\Definition\FieldDefinition $recentArticleField */ $authorField = $articleFieldType->getFields()['author']; $authorFieldType = $authorField->getType(); $recentArticleField = $authorFieldType->getFields()['recentArticle']; $this->assertSame($this->blogArticle, $recentArticleField->getType()); /** @var \Fubhy\GraphQL\Type\Definition\FieldDefinition $feedField */ /** @var \Fubhy\GraphQL\Type\Definition\Types\ListModifier $feedFieldType */ $feedField = $this->blogQuery->getFields()['feed']; $feedFieldType = $feedField->getType(); $this->assertEquals('feed', $feedField->getName()); $this->assertSame($this->blogArticle, $feedFieldType->getWrappedType()); }
/** * Not exactly the same as the executor's definition of getFieldDef, in this * statically evaluated environment we do not always have an Object type, * and need to handle Interface and Union types. * * @param Schema $schema * @param Type $parentType * @param Field $fieldAST * * @return FieldDefinition */ protected static function getFieldDefinition(Schema $schema, Type $parentType, Field $fieldAST) { $name = $fieldAST->get('name')->get('value'); $schemaMeta = Introspection::schemaMetaFieldDefinition(); if ($name === $schemaMeta->getName() && $schema->getQueryType() === $parentType) { return $schemaMeta; } $typeMeta = Introspection::typeMetaFieldDefinition(); if ($name === $typeMeta->getName() && $schema->getQueryType() === $parentType) { return $typeMeta; } $typeNameMeta = Introspection::typeNameMetaFieldDefinition(); if ($name === $typeNameMeta->getName() && ($parentType instanceof ObjectType || $parentType instanceof InterfaceType || $parentType instanceof UnionType)) { return $typeNameMeta; } if ($parentType instanceof ObjectType || $parentType instanceof InterfaceType) { $fields = $parentType->getFields(); return isset($fields[$name]) ? $fields[$name] : NULL; } return NULL; }
/** * This method looks up the field on the given type defintion. * It has special casing for the two introspection fields, __schema * and __typename. __typename is special because it can always be * queried as a field, even in situations where no other fields * are allowed, like on a Union. __schema could get automatically * added to the query type, but that would require mutating type * definitions, which would cause issues. * * @param Schema $schema * @param ObjectType $parent * @param Field $ast * * @return FieldDefinition */ protected static function getFieldDefinition(Schema $schema, ObjectType $parent, Field $ast) { $name = $ast->get('name')->get('value'); $schemaMeta = Introspection::schemaMetaFieldDefinition(); $typeMeta = Introspection::typeMetaFieldDefinition(); $typeNameMeta = Introspection::typeNameMetaFieldDefinition(); if ($name === $schemaMeta->getName() && $schema->getQueryType() === $parent) { return $schemaMeta; } else { if ($name === $typeMeta->getName() && $schema->getQueryType() === $parent) { return $typeMeta; } else { if ($name === $typeNameMeta->getName()) { return $typeNameMeta; } } } $tmp = $parent->getFields(); return isset($tmp[$name]) ? $tmp[$name] : NULL; }