public function testDefinesAQueryOnlySchema() { $blogSchema = new Schema($this->blogQuery); $this->assertSame($blogSchema->getQueryType(), $this->blogQuery); $articleField = $this->blogQuery->getField('article'); $this->assertSame($articleField->getType(), $this->blogArticle); $this->assertSame($articleField->getType()->name, 'Article'); $this->assertSame($articleField->name, 'article'); /** @var ObjectType $articleFieldType */ $articleFieldType = $articleField->getType(); $titleField = $articleFieldType->getField('title'); $this->assertInstanceOf('GraphQL\\Type\\Definition\\FieldDefinition', $titleField); $this->assertSame('title', $titleField->name); $this->assertSame(Type::string(), $titleField->getType()); $authorField = $articleFieldType->getField('author'); $this->assertInstanceOf('GraphQL\\Type\\Definition\\FieldDefinition', $authorField); /** @var ObjectType $authorFieldType */ $authorFieldType = $authorField->getType(); $this->assertSame($this->blogAuthor, $authorFieldType); $recentArticleField = $authorFieldType->getField('recentArticle'); $this->assertInstanceOf('GraphQL\\Type\\Definition\\FieldDefinition', $recentArticleField); $this->assertSame($this->blogArticle, $recentArticleField->getType()); $feedField = $this->blogQuery->getField('feed'); $this->assertInstanceOf('GraphQL\\Type\\Definition\\FieldDefinition', $feedField); /** @var ListOfType $feedFieldType */ $feedFieldType = $feedField->getType(); $this->assertInstanceOf('GraphQL\\Type\\Definition\\ListOfType', $feedFieldType); $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. * * @return FieldDefinition */ private static function _getFieldDef(Schema $schema, Type $parentType, Field $fieldAST) { $name = $fieldAST->name->value; $schemaMeta = Introspection::schemaMetaFieldDef(); if ($name === $schemaMeta->name && $schema->getQueryType() === $parentType) { return $schemaMeta; } $typeMeta = Introspection::typeMetaFieldDef(); if ($name === $typeMeta->name && $schema->getQueryType() === $parentType) { return $typeMeta; } $typeNameMeta = Introspection::typeNameMetaFieldDef(); if ($name === $typeNameMeta->name && ($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 $parentType * @param $fieldName * * @return FieldDefinition */ private static function getFieldDef(Schema $schema, ObjectType $parentType, $fieldName) { static $schemaMetaFieldDef, $typeMetaFieldDef, $typeNameMetaFieldDef; $schemaMetaFieldDef = $schemaMetaFieldDef ?: Introspection::schemaMetaFieldDef(); $typeMetaFieldDef = $typeMetaFieldDef ?: Introspection::typeMetaFieldDef(); $typeNameMetaFieldDef = $typeNameMetaFieldDef ?: Introspection::typeNameMetaFieldDef(); if ($fieldName === $schemaMetaFieldDef->name && $schema->getQueryType() === $parentType) { return $schemaMetaFieldDef; } else { if ($fieldName === $typeMetaFieldDef->name && $schema->getQueryType() === $parentType) { return $typeMetaFieldDef; } else { if ($fieldName === $typeNameMetaFieldDef->name) { return $typeNameMetaFieldDef; } } } $tmp = $parentType->getFields(); return isset($tmp[$fieldName]) ? $tmp[$fieldName] : null; }