Skip to content

Instantly share code, notes, and snippets.

@renepardon
Created October 23, 2019 07:25
Show Gist options
  • Save renepardon/58b34ba1be427fade9cd2f8020dd1d89 to your computer and use it in GitHub Desktop.
Save renepardon/58b34ba1be427fade9cd2f8020dd1d89 to your computer and use it in GitHub Desktop.
<?php
namespace App\GraphQL\Queries;
use App\GraphQL\SchemaPrinter;
use GraphQL\Type\Definition\ResolveInfo;
use Nuwave\Lighthouse\GraphQL;
use Nuwave\Lighthouse\Support\Contracts\GraphQLContext;
/**
* Class Federation
*
* @package App\GraphQL\Queries
*/
class Federation
{
/**
* @var GraphQL
*/
protected $graphQL;
/**
* Federation constructor.
*
* @param \Nuwave\Lighthouse\GraphQL $graphQL
*/
public function __construct(GraphQL $graphQL)
{
$this->graphQL = $graphQL;
}
/**
* Return a value for the field.
*
* @param null $rootValue Usually contains the result returned from the parent field. In this case, it is always `null`.
* @param mixed[] $args The arguments that were passed into the field.
* @param \Nuwave\Lighthouse\Support\Contracts\GraphQLContext $context Arbitrary data that is shared between all fields of a single query.
* @param \GraphQL\Type\Definition\ResolveInfo $resolveInfo Information about the query itself, such as the execution state, the field name, path to the field from the root, and more.
*
* @return mixed
*/
public function __invoke($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
{
switch ($resolveInfo->fieldName) {
case '_service':
return $this->service($rootValue, $args, $context, $resolveInfo);
case '_entities':
return $this->entities($rootValue, $args, $context, $resolveInfo);
default:
// no default for now
}
}
/**
* @param $rootValue
* @param array $args
* @param GraphQLContext $context
* @param ResolveInfo $resolveInfo
*
* @return array
*/
protected function entities($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
{
// TODO to be implemented (resolve reference)
return [];
}
/**
* @param $rootValue
* @param array $args
* @param GraphQLContext $context
* @param ResolveInfo $resolveInfo
*
* @return array
*/
protected function service($rootValue, array $args, GraphQLContext $context, ResolveInfo $resolveInfo)
{
$schema = $this->graphQL->prepSchema();
$newSchema = SchemaPrinter::printFederatedSchema(
$schema,
['commentDescriptions' => false, 'federated' => true]
);
return ['sdl' => $newSchema];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment