Youshido\GraphQL\Relay\RelayMutation::buildMutation PHP Method

buildMutation() public static method

public static buildMutation ( string $name, array $args, AbstractObjectType | array $output, callable $resolveFunction ) : Field
$name string
$args array
$output Youshido\GraphQL\Type\Object\AbstractObjectType | array
$resolveFunction callable
return Youshido\GraphQL\Field\Field
    public static function buildMutation($name, array $args, $output, callable $resolveFunction)
    {
        if (!is_array($output) || is_object($output) && !$output instanceof AbstractObjectType) {
            throw new \Exception('Output can be instance of AbstractObjectType or array of fields');
        }
        return new Field(['name' => $name, 'args' => [new InputField(['name' => 'input', 'type' => new NonNullType(new InputObjectType(['name' => ucfirst($name) . 'Input', 'fields' => array_merge($args, [new InputField(['name' => 'clientMutationId', 'type' => new NonNullType(new StringType())])])]))])], 'type' => new ObjectType(['fields' => is_object($output) ? $output : array_merge($output, [new Field(['name' => 'clientMutationId', 'type' => new NonNullType(new StringType())])]), 'name' => ucfirst($name) . 'Payload']), 'resolve' => function ($value, $args, ResolveInfo $info) use($resolveFunction) {
            $resolveValue = $resolveFunction($value, $args['input'], $args, $info);
            if (is_object($resolveValue)) {
                $resolveValue->clientMutationId = $args['input']['clientMutationId'];
            } elseif (is_array($resolveValue)) {
                $resolveValue['clientMutationId'] = $args['input']['clientMutationId'];
            }
            return $resolveValue;
        }]);
    }

Usage Example

Exemplo n.º 1
0
 public function build(SchemaConfig $config)
 {
     $fetcher = new CallableFetcher(function ($type, $id) {
         switch ($type) {
             case FactionType::TYPE_KEY:
                 return TestDataProvider::getFaction($id);
             case ShipType::TYPE_KEY:
                 return TestDataProvider::getShip($id);
         }
         return null;
     }, function ($object) {
         return $object && array_key_exists('ships', $object) ? new FactionType() : new ShipType();
     });
     $config->getQuery()->addField(new NodeField($fetcher))->addField('rebels', ['type' => new FactionType(), 'resolve' => function () {
         return TestDataProvider::getFaction('rebels');
     }])->addField('empire', ['type' => new FactionType(), 'resolve' => function () {
         return TestDataProvider::getFaction('empire');
     }])->addField('factions', ['type' => new ListType(new FactionType()), 'args' => ['names' => ['type' => new ListType(new StringType())]], 'resolve' => function ($value = null, $args, $info) {
         return TestDataProvider::getByNames($args['names']);
     }]);
     $config->getMutation()->addField(RelayMutation::buildMutation('introduceShip', [new InputField(['name' => 'shipName', 'type' => new NonNullType(new StringType())]), new InputField(['name' => 'factionId', 'type' => new NonNullType(new StringType())])], ['newShipEdge' => ['type' => Connection::edgeDefinition(new ShipType(), 'newShip'), 'resolve' => function ($value) {
         $allShips = TestDataProvider::getShips();
         $newShip = TestDataProvider::getShip($value['shipId']);
         return ['cursor' => ArrayConnection::cursorForObjectInConnection($allShips, $newShip), 'node' => $newShip];
     }], 'faction' => ['type' => new FactionType(), 'resolve' => function ($value) {
         return TestDataProvider::getFaction($value['factionId']);
     }]], function ($value, $args, $info) {
         $newShip = TestDataProvider::createShip($args['shipName'], $args['factionId']);
         return ['shipId' => $newShip['id'], 'factionId' => $args['factionId']];
     }));
     /** https://github.com/graphql/graphql-relay-js/blob/master/src/__tests__/starWarsSchema.js */
 }
All Usage Examples Of Youshido\GraphQL\Relay\RelayMutation::buildMutation
RelayMutation