本文整理汇总了PHP中GraphQL\Type\Definition\Type类的典型用法代码示例。如果您正苦于以下问题:PHP Type类的具体用法?PHP Type怎么用?PHP Type使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Type类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getDefaultSchema
/**
* @return Schema
*/
protected function getDefaultSchema()
{
$Being = new InterfaceType(['name' => 'Being', 'fields' => ['name' => ['type' => Type::string()]]]);
$Pet = new InterfaceType(['name' => 'Pet', 'fields' => ['name' => ['type' => Type::string()]]]);
$DogCommand = new EnumType(['name' => 'DogCommand', 'values' => ['SIT' => ['value' => 0], 'HEEL' => ['value' => 1], 'DOWN' => ['value' => 3]]]);
$Dog = new ObjectType(['name' => 'Dog', 'fields' => ['name' => ['type' => Type::string()], 'nickname' => ['type' => Type::string()], 'barkVolume' => ['type' => Type::int()], 'barks' => ['type' => Type::boolean()], 'doesKnowCommand' => ['type' => Type::boolean(), 'args' => ['dogCommand' => ['type' => $DogCommand]]], 'isHousetrained' => ['type' => Type::boolean(), 'args' => ['atOtherHomes' => ['type' => Type::boolean(), 'defaultValue' => true]]], 'isAtLocation' => ['type' => Type::boolean(), 'args' => ['x' => ['type' => Type::int()], 'y' => ['type' => Type::int()]]]], 'interfaces' => [$Being, $Pet]]);
$FurColor = new EnumType(['name' => 'FurColor', 'values' => ['BROWN' => ['value' => 0], 'BLACK' => ['value' => 1], 'TAN' => ['value' => 2], 'SPOTTED' => ['value' => 3]]]);
$Cat = new ObjectType(['name' => 'Cat', 'fields' => ['name' => ['type' => Type::string()], 'nickname' => ['type' => Type::string()], 'meows' => ['type' => Type::boolean()], 'meowVolume' => ['type' => Type::int()], 'furColor' => ['type' => $FurColor]], 'interfaces' => [$Being, $Pet]]);
$CatOrDog = new UnionType(['name' => 'CatOrDog', 'types' => [$Dog, $Cat], 'resolveType' => function ($value) {
// not used for validation
return null;
}]);
$Intelligent = new InterfaceType(['name' => 'Intelligent', 'fields' => ['iq' => ['type' => Type::int()]]]);
$Human = $this->humanType = new ObjectType(['name' => 'Human', 'interfaces' => [$Being, $Intelligent], 'fields' => ['name' => ['args' => ['surname' => ['type' => Type::boolean()]], 'type' => Type::string()], 'pets' => ['type' => Type::listOf($Pet)], 'relatives' => ['type' => function () {
return Type::listOf($this->humanType);
}]]]);
$Alien = new ObjectType(['name' => 'Alien', 'interfaces' => [$Being, $Intelligent], 'fields' => ['iq' => ['type' => Type::int()], 'numEyes' => ['type' => Type::int()]]]);
$DogOrHuman = new UnionType(['name' => 'DogOrHuman', 'types' => [$Dog, $Human], 'resolveType' => function () {
// not used for validation
return null;
}]);
$HumanOrAlien = new UnionType(['name' => 'HumanOrAlien', 'types' => [$Human, $Alien], 'resolveType' => function () {
// not used for validation
return null;
}]);
$ComplexInput = new InputObjectType(['name' => 'ComplexInput', 'fields' => ['requiredField' => ['type' => Type::nonNull(Type::boolean())], 'intField' => ['type' => Type::int()], 'stringField' => ['type' => Type::string()], 'booleanField' => ['type' => Type::boolean()], 'stringListField' => ['type' => Type::listOf(Type::string())]]]);
$ComplicatedArgs = new ObjectType(['name' => 'ComplicatedArgs', 'fields' => ['intArgField' => ['type' => Type::string(), 'args' => ['intArg' => ['type' => Type::int()]]], 'nonNullIntArgField' => ['type' => Type::string(), 'args' => ['nonNullIntArg' => ['type' => Type::nonNull(Type::int())]]], 'stringArgField' => ['type' => Type::string(), 'args' => ['stringArg' => ['type' => Type::string()]]], 'booleanArgField' => ['type' => Type::string(), 'args' => ['booleanArg' => ['type' => Type::boolean()]]], 'enumArgField' => ['type' => Type::string(), 'args' => ['enumArg' => ['type' => $FurColor]]], 'floatArgField' => ['type' => Type::string(), 'args' => ['floatArg' => ['type' => Type::float()]]], 'idArgField' => ['type' => Type::string(), 'args' => ['idArg' => ['type' => Type::id()]]], 'stringListArgField' => ['type' => Type::string(), 'args' => ['stringListArg' => ['type' => Type::listOf(Type::string())]]], 'complexArgField' => ['type' => Type::string(), 'args' => ['complexArg' => ['type' => $ComplexInput]]], 'multipleReqs' => ['type' => Type::string(), 'args' => ['req1' => ['type' => Type::nonNull(Type::int())], 'req2' => ['type' => Type::nonNull(Type::int())]]], 'multipleOpts' => ['type' => Type::string(), 'args' => ['opt1' => ['type' => Type::int(), 'defaultValue' => 0], 'opt2' => ['type' => Type::int(), 'defaultValue' => 0]]], 'multipleOptAndReq' => ['type' => Type::string(), 'args' => ['req1' => ['type' => Type::nonNull(Type::int())], 'req2' => ['type' => Type::nonNull(Type::int())], 'opt1' => ['type' => Type::int(), 'defaultValue' => 0], 'opt2' => ['type' => Type::int(), 'defaultValue' => 0]]]]]);
$queryRoot = new ObjectType(['name' => 'QueryRoot', 'fields' => ['human' => ['args' => ['id' => ['type' => Type::id()]], 'type' => $Human], 'alien' => ['type' => $Alien], 'dog' => ['type' => $Dog], 'cat' => ['type' => $Cat], 'pet' => ['type' => $Pet], 'catOrDog' => ['type' => $CatOrDog], 'dogOrHuman' => ['type' => $DogOrHuman], 'humanOrAlien' => ['type' => $HumanOrAlien], 'complicatedArgs' => ['type' => $ComplicatedArgs]]]);
$defaultSchema = new Schema($queryRoot);
return $defaultSchema;
}
示例2: resolveType
/**
* @param string $name
* @return Type
*/
public function resolveType($name)
{
if (preg_match('#^(.+)\\!$#', $name, $regs)) {
return Type::nonNull($this->resolveType($regs[1]));
}
if (preg_match('#^\\[(.+)\\]$#', $name, $regs)) {
return Type::listOf($this->resolveType($regs[1]));
}
switch ($name) {
case Type::INT:
return Type::int();
case Type::STRING:
return Type::string();
case Type::BOOLEAN:
return Type::boolean();
case Type::FLOAT:
return Type::float();
case Type::ID:
return Type::id();
default:
if (!isset($this->types[$name])) {
throw new \InvalidArgumentException(sprintf('Type "%s" is not defined', $name));
}
return $this->types[$name];
}
}
示例3: setUp
public function setUp()
{
$NamedType = new InterfaceType(['name' => 'Named', 'fields' => ['name' => ['type' => Type::string()]]]);
$DogType = new ObjectType(['name' => 'Dog', 'interfaces' => [$NamedType], 'fields' => ['name' => ['type' => Type::string()], 'barks' => ['type' => Type::boolean()]], 'isTypeOf' => function ($value) {
return $value instanceof Dog;
}]);
$CatType = new ObjectType(['name' => 'Cat', 'interfaces' => [$NamedType], 'fields' => ['name' => ['type' => Type::string()], 'meows' => ['type' => Type::boolean()]], 'isTypeOf' => function ($value) {
return $value instanceof Cat;
}]);
$PetType = new UnionType(['name' => 'Pet', 'types' => [$DogType, $CatType], 'resolveType' => function ($value) use($DogType, $CatType) {
if ($value instanceof Dog) {
return $DogType;
}
if ($value instanceof Cat) {
return $CatType;
}
}]);
$PersonType = new ObjectType(['name' => 'Person', 'interfaces' => [$NamedType], 'fields' => ['name' => ['type' => Type::string()], 'pets' => ['type' => Type::listOf($PetType)], 'friends' => ['type' => Type::listOf($NamedType)]], 'isTypeOf' => function ($value) {
return $value instanceof Person;
}]);
$this->schema = new Schema($PersonType);
$this->garfield = new Cat('Garfield', false);
$this->odie = new Dog('Odie', true);
$this->liz = new Person('Liz');
$this->john = new Person('John', [$this->garfield, $this->odie], [$this->liz, $this->odie]);
}
示例4: testFieldSelection
public function testFieldSelection()
{
$image = new ObjectType(['name' => 'Image', 'fields' => ['url' => ['type' => Type::string()], 'width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]]]);
$article = null;
$author = new ObjectType(['name' => 'Author', 'fields' => function () use($image, &$article) {
return ['id' => ['type' => Type::string()], 'name' => ['type' => Type::string()], 'pic' => ['type' => $image, 'args' => ['width' => ['type' => Type::int()], 'height' => ['type' => Type::int()]]], 'recentArticle' => ['type' => $article]];
}]);
$reply = new ObjectType(['name' => 'Reply', 'fields' => ['author' => ['type' => $author], 'body' => ['type' => Type::string()]]]);
$article = new ObjectType(['name' => 'Article', 'fields' => ['id' => ['type' => Type::string()], 'isPublished' => ['type' => Type::boolean()], 'author' => ['type' => $author], 'title' => ['type' => Type::string()], 'body' => ['type' => Type::string()], 'image' => ['type' => $image], 'replies' => ['type' => Type::listOf($reply)]]]);
$doc = '
query Test {
article {
author {
name
pic {
url
width
}
}
image {
width
height
}
replies {
body
author {
id
name
pic {
url
width
}
recentArticle {
id
title
body
}
}
}
}
}
';
$expectedDefaultSelection = ['author' => true, 'image' => true, 'replies' => true];
$expectedDeepSelection = ['author' => ['name' => true, 'pic' => ['url' => true, 'width' => true]], 'image' => ['width' => true, 'height' => true], 'replies' => ['body' => true, 'author' => ['id' => true, 'name' => true, 'pic' => ['url' => true, 'width' => true], 'recentArticle' => ['id' => true, 'title' => true, 'body' => true]]]];
$hasCalled = false;
$actualDefaultSelection = null;
$actualDeepSelection = null;
$blogQuery = new ObjectType(['name' => 'Query', 'fields' => ['article' => ['type' => $article, 'resolve' => function ($value, $args, $context, ResolveInfo $info) use(&$hasCalled, &$actualDefaultSelection, &$actualDeepSelection) {
$hasCalled = true;
$actualDefaultSelection = $info->getFieldSelection();
$actualDeepSelection = $info->getFieldSelection(5);
return null;
}]]]);
$schema = new Schema(['query' => $blogQuery]);
$result = GraphQL::execute($schema, $doc);
$this->assertTrue($hasCalled);
$this->assertEquals(['data' => ['article' => null]], $result);
$this->assertEquals($expectedDefaultSelection, $actualDefaultSelection);
$this->assertEquals($expectedDeepSelection, $actualDeepSelection);
}
示例5: getType
public function getType()
{
if (null === $this->resolvedType) {
$this->resolvedType = Type::resolve($this->type);
}
return $this->resolvedType;
}
示例6: setup
public function setup()
{
$this->allUsers = [['name' => 'Dan', 'friends' => [1, 2, 3, 4]], ['name' => 'Nick', 'friends' => [0, 2, 3, 4]], ['name' => 'Lee', 'friends' => [0, 1, 3, 4]], ['name' => 'Joe', 'friends' => [0, 1, 2, 4]], ['name' => 'Tim', 'friends' => [0, 1, 2, 3]]];
$this->userType = new ObjectType(['name' => 'User', 'fields' => function () {
return ['name' => ['type' => Type::string()], 'friends' => ['type' => $this->friendConnection, 'args' => Connection::connectionArgs(), 'resolve' => function ($user, $args) {
return ArrayConnection::connectionFromArray($user['friends'], $args);
}], 'friendsForward' => ['type' => $this->userConnection, 'args' => Connection::forwardConnectionArgs(), 'resolve' => function ($user, $args) {
return ArrayConnection::connectionFromArray($user['friends'], $args);
}], 'friendsBackward' => ['type' => $this->userConnection, 'args' => Connection::backwardConnectionArgs(), 'resolve' => function ($user, $args) {
return ArrayConnection::connectionFromArray($user['friends'], $args);
}]];
}]);
$this->friendEdge = Connection::createEdgeType(['name' => 'Friend', 'nodeType' => $this->userType, 'resolveNode' => function ($edge) {
return $this->allUsers[$edge['node']];
}, 'edgeFields' => function () {
return ['friendshipTime' => ['type' => Type::string(), 'resolve' => function () {
return 'Yesterday';
}]];
}]);
$this->friendConnection = Connection::createConnectionType(['name' => 'Friend', 'nodeType' => $this->userType, 'edgeType' => $this->friendEdge, 'connectionFields' => function () {
return ['totalCount' => ['type' => Type::int(), 'resolve' => function () {
return count($this->allUsers) - 1;
}]];
}]);
$this->userEdge = Connection::createEdgeType(['nodeType' => $this->userType, 'resolveNode' => function ($edge) {
return $this->allUsers[$edge['node']];
}]);
$this->userConnection = Connection::createConnectionType(['nodeType' => $this->userType, 'edgeType' => $this->userEdge]);
$this->queryType = new ObjectType(['name' => 'Query', 'fields' => function () {
return ['user' => ['type' => $this->userType, 'resolve' => function () {
return $this->allUsers[0];
}]];
}]);
$this->schema = new Schema(['query' => $this->queryType]);
}
示例7: setUp
public function setUp()
{
$this->syncError = new Error('sync');
$this->nonNullSyncError = new Error('nonNullSync');
$this->throwingData = ['sync' => function () {
throw $this->syncError;
}, 'nonNullSync' => function () {
throw $this->nonNullSyncError;
}, 'nest' => function () {
return $this->throwingData;
}, 'nonNullNest' => function () {
return $this->throwingData;
}];
$this->nullingData = ['sync' => function () {
return null;
}, 'nonNullSync' => function () {
return null;
}, 'nest' => function () {
return $this->nullingData;
}, 'nonNullNest' => function () {
return $this->nullingData;
}];
$dataType = new ObjectType(['name' => 'DataType', 'fields' => ['sync' => ['type' => Type::string()], 'nonNullSync' => ['type' => Type::nonNull(Type::string())], 'nest' => ['type' => function () use(&$dataType) {
return $dataType;
}], 'nonNullNest' => ['type' => function () use(&$dataType) {
return Type::nonNull($dataType);
}]]]);
$this->schema = new Schema($dataType);
}
示例8: fields
/**
* Type fields.
*
* @return array
*/
public function fields()
{
return ['name' => ['type' => Type::string(), 'description' => 'Name of the user.'], 'email' => ['type' => Type::string(), 'description' => 'Email of the user.'], 'tasks' => GraphQL::connection('task')->args(['order' => ['type' => Type::string(), 'description' => 'Sort order of tasks.']])->resolve(function ($parent, array $args) {
return $parent->tasks->transform(function ($task) {
return array_merge($task->toArray(), ['title' => 'foo']);
});
})->field()];
}
示例9: buildDogType
public static function buildDogType()
{
if (null !== self::$dogType) {
return self::$dogType;
}
self::$dogType = new ObjectType(['name' => 'Dog', 'fields' => ['name' => ['type' => Type::nonNull(Type::string())], 'master' => ['type' => self::buildHumanType()]]]);
return self::$dogType;
}
示例10: __construct
public function __construct()
{
$config = ['name' => 'Query', 'fields' => ['user' => ['type' => Types::user(), 'description' => 'Returns user by id (in range of 1-5)', 'args' => ['id' => Types::nonNull(Types::id())]], 'viewer' => ['type' => Types::user(), 'description' => 'Represents currently logged-in user (for the sake of example - simply returns user with id == 1)'], 'stories' => ['type' => Types::listOf(Types::story()), 'description' => 'Returns subset of stories posted for this blog', 'args' => ['after' => ['type' => Types::id(), 'description' => 'Fetch stories listed after the story with this ID'], 'limit' => ['type' => Types::int(), 'description' => 'Number of stories to be returned', 'defaultValue' => 10]]], 'lastStoryPosted' => ['type' => Types::story(), 'description' => 'Returns last story posted for this blog'], 'deprecatedField' => ['type' => Types::string(), 'deprecationReason' => 'This field is deprecated!'], 'fieldWithException' => ['type' => Types::string(), 'resolve' => function () {
throw new \Exception("Exception message thrown in field resolver");
}], 'hello' => Type::string()], 'resolveField' => function ($val, $args, $context, ResolveInfo $info) {
return $this->{$info->fieldName}($val, $args, $context, $info);
}];
parent::__construct($config);
}
示例11: getTestObjectType
/**
* Returns the test ObjectType
* @return ObjectType
*/
protected function getTestObjectType()
{
if (!$this->testObject) {
$this->testObject = new ObjectType(['name' => 'TestObject', 'fields' => ['name' => ['type' => Type::string(), 'resolve' => function () {
return 'testname';
}]], 'interfaces' => [$this->getLazyInterfaceType()]]);
}
return $this->testObject;
}
示例12: setUp
public function setUp()
{
$ColorType = new EnumType(['name' => 'Color', 'values' => ['RED' => ['value' => 0], 'GREEN' => ['value' => 1], 'BLUE' => ['value' => 2]]]);
$simpleEnum = new EnumType(['name' => 'SimpleEnum', 'values' => ['ONE', 'TWO', 'THREE']]);
$Complex1 = ['someRandomFunction' => function () {
}];
$Complex2 = new \ArrayObject(['someRandomValue' => 123]);
$ComplexEnum = new EnumType(['name' => 'Complex', 'values' => ['ONE' => ['value' => $Complex1], 'TWO' => ['value' => $Complex2]]]);
$QueryType = new ObjectType(['name' => 'Query', 'fields' => ['colorEnum' => ['type' => $ColorType, 'args' => ['fromEnum' => ['type' => $ColorType], 'fromInt' => ['type' => Type::int()], 'fromString' => ['type' => Type::string()]], 'resolve' => function ($value, $args) {
if (isset($args['fromInt'])) {
return $args['fromInt'];
}
if (isset($args['fromString'])) {
return $args['fromString'];
}
if (isset($args['fromEnum'])) {
return $args['fromEnum'];
}
}], 'simpleEnum' => ['type' => $simpleEnum, 'args' => ['fromName' => ['type' => Type::string()], 'fromValue' => ['type' => Type::string()]], 'resolve' => function ($value, $args) {
if (isset($args['fromName'])) {
return $args['fromName'];
}
if (isset($args['fromValue'])) {
return $args['fromValue'];
}
}], 'colorInt' => ['type' => Type::int(), 'args' => ['fromEnum' => ['type' => $ColorType], 'fromInt' => ['type' => Type::int()]], 'resolve' => function ($value, $args) {
if (isset($args['fromInt'])) {
return $args['fromInt'];
}
if (isset($args['fromEnum'])) {
return $args['fromEnum'];
}
}], 'complexEnum' => ['type' => $ComplexEnum, 'args' => ['fromEnum' => ['type' => $ComplexEnum, 'defaultValue' => $Complex1], 'provideGoodValue' => ['type' => Type::boolean()], 'provideBadValue' => ['type' => Type::boolean()]], 'resolve' => function ($value, $args) use($Complex1, $Complex2) {
if (!empty($args['provideGoodValue'])) {
// Note: this is one of the references of the internal values which
// ComplexEnum allows.
return $Complex2;
}
if (!empty($args['provideBadValue'])) {
// Note: similar shape, but not the same *reference*
// as Complex2 above. Enum internal values require === equality.
return new \ArrayObject(['someRandomValue' => 123]);
}
return $args['fromEnum'];
}]]]);
$MutationType = new ObjectType(['name' => 'Mutation', 'fields' => ['favoriteEnum' => ['type' => $ColorType, 'args' => ['color' => ['type' => $ColorType]], 'resolve' => function ($value, $args) {
return isset($args['color']) ? $args['color'] : null;
}]]]);
$SubscriptionType = new ObjectType(['name' => 'Subscription', 'fields' => ['subscribeToEnum' => ['type' => $ColorType, 'args' => ['color' => ['type' => $ColorType]], 'resolve' => function ($value, $args) {
return isset($args['color']) ? $args['color'] : null;
}]]]);
$this->Complex1 = $Complex1;
$this->Complex2 = $Complex2;
$this->ComplexEnum = $ComplexEnum;
$this->schema = new Schema(['query' => $QueryType, 'mutation' => $MutationType, 'subscription' => $SubscriptionType]);
}
示例13: testHandlesNonNullListOfNonNulls
/**
* @describe [T!]!
*/
public function testHandlesNonNullListOfNonNulls()
{
$type = Type::nonNull(Type::listOf(Type::nonNull(Type::int())));
// Contains values
$this->check($type, [1, 2], ['data' => ['nest' => ['test' => [1, 2]]]]);
// Contains null
$this->check($type, [1, null, 2], ['data' => ['nest' => null], 'errors' => [FormattedError::create('Cannot return null for non-nullable field DataType.test.', [new SourceLocation(1, 10)])]]);
// Returns null
$this->check($type, null, ['data' => ['nest' => null], 'errors' => [FormattedError::create('Cannot return null for non-nullable field DataType.test.', [new SourceLocation(1, 10)])]]);
}
示例14: testUsesProvidedResolveFunction
/**
* @it uses provided resolve function
*/
public function testUsesProvidedResolveFunction()
{
$schema = $this->buildSchema(['type' => Type::string(), 'args' => ['aStr' => ['type' => Type::string()], 'aInt' => ['type' => Type::int()]], 'resolve' => function ($source, $args) {
return json_encode([$source, $args]);
}]);
$this->assertEquals(['data' => ['test' => '[null,[]]']], GraphQL::execute($schema, '{ test }'));
$this->assertEquals(['data' => ['test' => '["Source!",[]]']], GraphQL::execute($schema, '{ test }', 'Source!'));
$this->assertEquals(['data' => ['test' => '["Source!",{"aStr":"String!"}]']], GraphQL::execute($schema, '{ test(aStr: "String!") }', 'Source!'));
$this->assertEquals(['data' => ['test' => '["Source!",{"aStr":"String!","aInt":-123}]']], GraphQL::execute($schema, '{ test(aInt: -123, aStr: "String!") }', 'Source!'));
}
示例15: getTypeMap
public function getTypeMap()
{
if (null === $this->_typeMap) {
$map = [];
foreach ([$this->getQueryType(), $this->getMutationType(), Introspection::_schema()] as $type) {
$this->_extractTypes($type, $map);
}
$this->_typeMap = $map + Type::getInternalTypes();
}
return $this->_typeMap;
}