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
Example #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 */
 }
Example #2
0
 /**
  * @expectedException \Exception
  */
 public function testInvalidType()
 {
     RelayMutation::buildMutation('ship', ['name' => new StringType()], new IntType(), function ($source, $args, $info) {
     });
 }