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 */ }
public function build(SchemaConfig $config) { $config->getQuery()->addFields(['me' => ['type' => new TestObjectType(), 'resolve' => function ($value, $args, ResolveInfo $info) { return $info->getReturnType()->getData(); }], 'status' => ['type' => new TestEnumType(), 'resolve' => function () { return $this->testStatusValue; }]]); $config->getMutation()->addFields(['updateStatus' => ['type' => new TestEnumType(), 'resolve' => function () { return $this->testStatusValue; }, 'args' => ['newStatus' => new TestEnumType()]]]); }
public function build(SchemaConfig $config) { $config->getQuery()->addFields(['latestPost' => ['type' => new PostType(), 'resolve' => function ($value, array $args, ResolveInfo $info) { return $info->getReturnType()->getOne(empty($args['id']) ? 1 : $args['id']); }], 'randomBanner' => ['type' => new BannerType(), 'resolve' => function () { return DataProvider::getBanner(rand(1, 10)); }], 'pageContentUnion' => ['type' => new ListType(new ContentBlockUnion()), 'resolve' => function () { return [DataProvider::getPost(1), DataProvider::getBanner(1)]; }], 'pageContentInterface' => ['type' => new ListType(new ContentBlockInterface()), 'resolve' => function () { return [DataProvider::getPost(2), DataProvider::getBanner(3)]; }]]); $config->getMutation()->addFields([new LikePostField(), 'createPost' => ['type' => new PostType(), 'args' => ['post' => new PostInputType(), 'author' => new StringType()], 'resolve' => function ($value, array $args, ResolveInfo $info) { // code for creating a new post goes here // we simple use our DataProvider for now $post = DataProvider::getPost(10); if (!empty($args['post']['title'])) { $post['title'] = $args['post']['title']; } return $post; }]]); }
public final function getMutationType() { return $this->config->getMutation(); }