Skip to content

Commit

Permalink
Merge branch 'dimitri-koenig-feature/add-custom-resolvers' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
dmongeau committed Feb 1, 2018
2 parents 982b826 + ced0781 commit 7a2745a
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/Folklore/GraphQL/GraphQL.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public function schema($schema = null)
throw new SchemaNotFound('Type '.$schemaName.' not found.');
}

$schema = is_array($schema) ? $schema:$this->schemas[$schemaName];
$schema = is_array($schema) ? $schema : $this->schemas[$schemaName];

if ($schema instanceof Schema) {
return $schema;
}

$schemaQuery = array_get($schema, 'query', []);
$schemaMutation = array_get($schema, 'mutation', []);
Expand Down Expand Up @@ -152,11 +156,14 @@ public function query($query, $variables = [], $opts = [])

public function queryAndReturnResult($query, $variables = [], $opts = [])
{
$root = array_get($opts, 'root', null);
$context = array_get($opts, 'context', null);
$schemaName = array_get($opts, 'schema', null);
$operationName = array_get($opts, 'operationName', null);

$additionalResolversSchemaName = is_string($schemaName) ? $schemaName : config('graphql.schema', 'default');
$additionalResolvers = config('graphql.resolvers.' . $additionalResolversSchemaName, []);
$root = array_merge(array_get($opts, 'root', []), $additionalResolvers);

$schema = $this->schema($schemaName);

$result = GraphQLBase::executeAndReturnResult($schema, $query, $root, $context, $variables, $operationName);
Expand Down
19 changes: 19 additions & 0 deletions src/config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,25 @@
]
],

/*
* Additional resolvers which can also be used with shorthand building of the schema
* using \GraphQL\Utils::BuildSchema feature
*
* Example:
*
* 'resolvers' => [
* 'default' => [
* 'echo' => function ($root, $args, $context) {
* return 'Echo: ' . $args['message'];
* },
* ],
* ],
*/
'resolvers' => [
'default' => [
],
],

/*
* The types available in the application. You can access them from the
* facade like this: GraphQL::type('user')
Expand Down
39 changes: 38 additions & 1 deletion tests/ConfigTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

use GraphQL\Type\Schema;
use GraphQL\Utils\BuildSchema;
use GraphQL\Type\Definition\ObjectType;
use GraphQL\Validator\DocumentValidator;

Expand Down Expand Up @@ -39,7 +40,24 @@ protected function getEnvironmentSetUp($app)
'mutation' => [
'updateExampleCustom' => UpdateExampleMutation::class
]
]
],
'shorthand' => BuildSchema::build('
schema {
query: ShorthandExample
}
type ShorthandExample {
echo(message: String!): String!
}
'),
],

'resolvers' => [
'shorthand' => [
'echo' => function ($root, $args, $context) {
return 'Echo: ' . $args['message'];
},
],
],

'types' => [
Expand Down Expand Up @@ -100,6 +118,7 @@ public function testSchemas()

$this->assertArrayHasKey('default', $schemas);
$this->assertArrayHasKey('custom', $schemas);
$this->assertArrayHasKey('shorthand', $schemas);
}

public function testVariablesInputName()
Expand All @@ -122,6 +141,24 @@ public function testVariablesInputName()
]);
}

public function testVariablesInputNameForShorthandResolver()
{
$response = $this->call('GET', '/graphql_test/query/shorthand', [
'query' => $this->queries['shorthandExamplesWithVariables'],
'params' => [
'message' => 'Hello World!',
],
]);

$this->assertEquals($response->getStatusCode(), 200);

$content = $response->getData(true);
$this->assertArrayHasKey('data', $content);
$this->assertEquals($content['data'], [
'echo' => 'Echo: Hello World!',
]);
}

public function testSecurity()
{
$queryComplexity = DocumentValidator::getRule('QueryComplexity');
Expand Down
6 changes: 6 additions & 0 deletions tests/Objects/queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
}
",

'shorthandExamplesWithVariables' => "
query QueryShorthandExamplesVariables(\$message: String!) {
echo(message: \$message)
}
",

'examplesWithContext' => "
query QueryExamplesContext {
examplesContext {
Expand Down

0 comments on commit 7a2745a

Please sign in to comment.