Skip to content

Commit

Permalink
Merge pull request #61 from overblog/spec-april2016
Browse files Browse the repository at this point in the history
GraphQL Spec april 2016 migration
  • Loading branch information
mcg-web authored Sep 14, 2016
2 parents 07c1d3a + 1a35c6f commit f100592
Show file tree
Hide file tree
Showing 10 changed files with 41 additions and 14 deletions.
7 changes: 6 additions & 1 deletion Definition/Builder/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ public function create($queryAlias = null, $mutationAlias = null, $subscriptionA
$mutation = $this->typeResolver->resolve($mutationAlias);
$subscription = $this->typeResolver->resolve($subscriptionAlias);

return new Schema($query, $mutation, $subscription);
return new Schema([
'query' => $query,
'mutation' => $mutation,
'subscription' => $subscription,
'types' => $this->typeResolver->getSolutions(),
]);
}
}
25 changes: 18 additions & 7 deletions Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,25 @@ protected function resolveTypeCode($alias)
return sprintf('$container->get(\'%s\')->resolve(%s)', 'overblog_graphql.type_resolver', var_export($alias, true));
}

/**
* todo replace generateResolve in vendor after spec-april2016 is merged
*
* @param array $value
* @return string
*/
protected function generateResolve2(array $value)
{
return $this->callableCallbackFromArrayValue($value, 'resolve', '$value, $args, $context, \\GraphQL\\Type\\Definition\\ResolveInfo $info');
}

protected function generateResolve(array $value)
{
$accessIsSet = $this->arrayKeyExistsAndIsNotNull($value, 'access');
$fieldOptions = $value;
$fieldOptions['resolve'] = $this->arrayKeyExistsAndIsNotNull($fieldOptions, 'resolve') ? $fieldOptions['resolve'] : $this->defaultResolver;

if (!$accessIsSet || true === $fieldOptions['access']) { // access granted to this field
$resolveCallback = parent::generateResolve($fieldOptions);
$resolveCallback = $this->generateResolve2($fieldOptions);
if ('null' === $resolveCallback) {
return $resolveCallback;
}
Expand All @@ -88,10 +99,10 @@ protected function generateResolve(array $value)
$resolveInfoClass = $this->shortenClassName('\\GraphQL\\Type\\Definition\\ResolveInfo');

$code = <<<'CODE'
function ($value, $args, %s $info) <closureUseStatements> {
function ($value, $args, $context, %s $info) <closureUseStatements> {
<spaces><spaces>$resolverCallback = %s;
<spaces><spaces>return call_user_func_array($resolverCallback, [$value, new %s($args), $info]);
<spaces><spaces>return call_user_func_array($resolverCallback, [$value, new %s($args), $context, $info]);
<spaces>}
CODE;

Expand All @@ -101,17 +112,17 @@ function ($value, $args, %s $info) <closureUseStatements> {

return sprintf('function () { throw new %s(\'Access denied to this field.\'); }', $exceptionClass);
} else { // wrap resolver with access
$resolveCallback = parent::generateResolve($fieldOptions);
$accessChecker = $this->callableCallbackFromArrayValue($fieldOptions, 'access', '$value, $args, \\GraphQL\\Type\\Definition\\ResolveInfo $info, $object');
$resolveCallback = $this->generateResolve2($fieldOptions);
$accessChecker = $this->callableCallbackFromArrayValue($fieldOptions, 'access', '$value, $args, $context, \\GraphQL\\Type\\Definition\\ResolveInfo $info, $object');
$resolveInfoClass = $this->shortenClassName('\\GraphQL\\Type\\Definition\\ResolveInfo');
$argumentClass = $this->shortenClassName('\\Overblog\\GraphQLBundle\\Definition\\Argument');

$code = <<<'CODE'
function ($value, $args, %s $info) <closureUseStatements> {
function ($value, $args, $context, %s $info) <closureUseStatements> {
<spaces><spaces>$resolverCallback = %s;
<spaces><spaces>$accessChecker = %s;
<spaces><spaces>$isMutation = $info instanceof ResolveInfo && 'mutation' === $info->operation->operation && $info->parentType === $info->schema->getMutationType();
<spaces><spaces>return $container->get('overblog_graphql.access_resolver')->resolve($accessChecker, $resolverCallback, [$value, new %s($args), $info], $isMutation);
<spaces><spaces>return $container->get('overblog_graphql.access_resolver')->resolve($accessChecker, $resolverCallback, [$value, new %s($args), $context, $info], $isMutation);
<spaces>}
CODE;

Expand Down
1 change: 1 addition & 0 deletions Request/Executor.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public function execute(array $data, array $context = [])
$this->schema,
isset($data['query']) ? $data['query'] : null,
$context,
$context,
$data['variables'],
$data['operationName']
);
Expand Down
8 changes: 8 additions & 0 deletions Resolver/AbstractResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ public function getSolution($name)
return isset($this->solutions[$name]) ? $this->solutions[$name] : null;
}

/**
* @return array
*/
public function getSolutions()
{
return $this->solutions;
}

/**
* @param $name
*
Expand Down
4 changes: 2 additions & 2 deletions Resolver/Resolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ class Resolver
*/
private static $accessor;

public static function defaultResolveFn($objectOrArray, $args, ResolveInfo $info)
public static function defaultResolveFn($objectOrArray, $args, $context, ResolveInfo $info)
{
$fieldName = $info->fieldName;
$value = static::valueFromObjectOrArray($objectOrArray, $fieldName);

return $value instanceof \Closure ? $value($objectOrArray, $args, $info) : $value;
return $value instanceof \Closure ? $value($objectOrArray, $args, $context, $info) : $value;
}

public static function valueFromObjectOrArray($objectOrArray, $fieldName)
Expand Down
2 changes: 2 additions & 0 deletions Resolver/ResolverInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,7 @@ public function addSolution($name, $solution, $extraOptions = []);

public function getSolution($name);

public function getSolutions();

public function getSolutionOptions($name);
}
2 changes: 1 addition & 1 deletion Tests/Functional/Command/GraphDumpSchemaCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ public function testExecute()
);

$this->assertEquals(0, $commandTester->getStatusCode());
$this->assertEquals(file_get_contents(__DIR__.'/schema.json'), file_get_contents($file));
$this->assertEquals(trim(file_get_contents(__DIR__.'/schema.json')), file_get_contents($file));
}
}
2 changes: 1 addition & 1 deletion Tests/Functional/Command/schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Tests/Resolver/ResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function testDefaultResolveFn($fieldName, $source, $expected)
{
$info = new ResolveInfo(['fieldName' => $fieldName]);

$this->assertEquals($expected, Resolver::defaultResolveFn($source, [], $info));
$this->assertEquals($expected, Resolver::defaultResolveFn($source, [], [], $info));
}

public function resolverProvider()
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"symfony/framework-bundle": "^2.7|^3.0",
"symfony/options-resolver": "^2.7|^3.0",
"symfony/property-access": "^2.7|^3.0",
"webonyx/graphql-php": "0.6.4"
"webonyx/graphql-php": "0.7.0"
},
"suggest": {
"twig/twig": "If you want to use graphiQL."
Expand Down

0 comments on commit f100592

Please sign in to comment.