Skip to content

Commit

Permalink
Merge branch 'master' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
Mikk Mihkel Nurges committed Mar 7, 2019
2 parents 74474e4 + 8399305 commit 5bc0bea
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 3 deletions.
3 changes: 2 additions & 1 deletion Readme.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Laravel GraphQL

[![Latest Stable Version](https://poser.pugx.org/rebing/graphql-laravel/v/stable)](https://packagist.org/packages/rebing/graphql-laravel)
[![Build Status](https://travis-ci.org/mfn/graphql-laravel.svg?branch=master)](https://travis-ci.org/mfn/graphql-laravel)
[![Build Status](https://travis-ci.org/rebing/graphql-laravel.svg?branch=master)](https://travis-ci.org/mfn/graphql-laravel)
[![License](https://poser.pugx.org/rebing/graphql-laravel/license)](https://packagist.org/packages/rebing/graphql-laravel)
[![Get on Slack](https://img.shields.io/badge/slack-join-orange.svg)](https://join.slack.com/t/rebing-graphql/shared_invite/enQtNTE5NjQzNDI5MzQ4LWVjMTMxNzIyZjBlNTFhZGQ5MDVjZDAwZDNjODA3ODE2NjdiOGJkMjMwMTZkZmNhZjhiYTE1MjEyNDk0MWJmMzk)

Expand Down Expand Up @@ -562,3 +562,4 @@ Note: You can test your file upload implementation using [Altair](https://altair
- [Interfaces](docs/advanced.md#interfaces)
- [Input Object](docs/advanced.md#input-object)
- [JSON Columns](docs/advanced.md#json-columns)
- [Field deprecation](docs/advanced.md#field-deprecation)
56 changes: 56 additions & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
- [Interfaces](#interfaces)
- [Input Object](#input-object)
- [JSON Columns](#json-columns)
- [Field deprecation](#field-deprecation)

### Authorization

Expand Down Expand Up @@ -935,3 +936,58 @@ class UserType extends GraphQLType
// ...
}
```

#### Field deprecation

Sometimes you would want to deprecate a field but still have to maintain backward compatibility
until clients completely stop using that field. You can deprecate a field using
[directive](https://www.apollographql.com/docs/graphql-tools/schema-directives.html). If you add `deprecationReason`
to field attributes it will become marked as deprecated in GraphQL documentation. You can validate schema on client
using [Apollo Engine](https://blog.apollographql.com/schema-validation-with-apollo-engine-4032456425ba).


```php
<?php

namespace App\GraphQL\Type;

use App\User;
use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class UserType extends GraphQLType
{
protected $attributes = [
'name' => 'User',
'description' => 'A user',
'model' => User::class,
];

public function fields()
{
return [
'id' => [
'type' => Type::nonNull(Type::string()),
'description' => 'The id of the user',
],
'email' => [
'type' => Type::string(),
'description' => 'The email of user',
],
'address' => [
'type' => Type::string(),
'description' => 'The address of user',
'deprecationReason' => 'Deprecated due to address field split'
],
'address_line_1' => [
'type' => Type::string(),
'description' => 'The address line 1 of user',
],
'address_line_2' => [
'type' => Type::string(),
'description' => 'The address line 2 of user',
],
];
}
}
```
6 changes: 6 additions & 0 deletions src/Rebing/GraphQL/Support/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,12 @@ public function getInputTypeRules(InputObjectType $input, $prefix, $resolutionAr

// then recursively call the parent method to see if this is an
// input object, passing in the new prefix
if($field->type instanceof InputObjectType) {
// in case the field is a self reference we must not do
// a recursive call as it will never stop
if($field->type->toString() == $input->toString())
continue;
}
$rules = array_merge($rules, $this->inferRulesFromType($field->type, $key, $resolutionArguments));
}

Expand Down
25 changes: 25 additions & 0 deletions tests/GraphQLQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,31 @@ public function testQueryAndReturnResultWithVariables()
]);
}

/**
* Test query with complex variables.
*
* @test
*/
public function testQueryAndReturnResultWithFilterVariables()
{
$result = GraphQL::queryAndReturnResult($this->queries['examplesWithFilterVariables'], [
'filter' => [
'test' => 'Example 1'
]
]);

$this->assertObjectHasAttribute('data', $result);
// When XDebug is used with breaking on exceptions the real error will
// be visible in case the recursion for getInputTypeRules runs away.
// GraphQL\Error\Error: Maximum function nesting level of '256' reached, aborting!
$this->assertCount(0, $result->errors);
$this->assertEquals($result->data, [
'examplesFiltered' => [
$this->data[0]
]
]);
}

/**
* Test query with authorize
*
Expand Down
34 changes: 34 additions & 0 deletions tests/Objects/ExampleFilterInputType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Type as GraphQLType;

class ExampleFilterInputType extends GraphQLType
{
protected $inputObject = true;

protected $attributes = [
'name' => 'ExampleFilterInput',
'description' => 'A nested filter input with self reference'
];

public function fields()
{
return [
'AND' => [
'type' => Type::listOf(Type::nonNull(GraphQL::type('ExampleFilterInput'))),
'description' => 'List of self references'
],
'test' => [
'type' => Type::String(),
'description' => 'Test field filter'
],
// This field can trigger an infinite recursion
// in case recursion in Field.getRules() is not handled correctly
'parent' => [
'type' => GraphQL::type('ExampleFilterInput'),
'description' => 'Self reference'
]
];
}
}
44 changes: 44 additions & 0 deletions tests/Objects/ExamplesFilteredQuery.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

use GraphQL\Type\Definition\Type;
use Rebing\GraphQL\Support\Query;

class ExamplesFilteredQuery extends Query
{
protected $attributes = [
'name' => 'Filtered examples'
];

public function type()
{
return Type::listOf(GraphQL::type('Example'));
}

public function args()
{
return [
'filter' => [
'name' => 'filter',
'type' => GraphQL::type('ExampleFilterInput')
]
];
}

public function resolve($root, $args)
{
$data = include(__DIR__.'/data.php');
$result = [];

if (isset($args['filter'])) {
if(isset($args['filter']['test'])) {
foreach($data as $item) {
if($item['test'] == $args['filter']['test']) {
$result[] = $item;
}
}
}
}

return $result;
}
}
2 changes: 1 addition & 1 deletion tests/Objects/ExamplesQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function type()
public function args()
{
return [
'index' => ['name' => 'index', 'type' => Type::int()]
'index' => ['name' => 'index', 'type' => Type::int()],
];
}

Expand Down
8 changes: 8 additions & 0 deletions tests/Objects/queries.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@
}
",

'examplesWithFilterVariables' => "
query QueryExamplesWithFilterVariables(\$filter: ExampleFilterInput) {
examplesFiltered(filter: \$filter) {
test
}
}
",

'shorthandExamplesWithVariables' => "
query QueryShorthandExamplesVariables(\$message: String!) {
echo(message: \$message)
Expand Down
4 changes: 3 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ protected function getEnvironmentSetUp($app)
'examples' => ExamplesQuery::class,
'examplesAuthorize' => ExamplesAuthorizeQuery::class,
'examplesPagination' => ExamplesPaginationQuery::class,
'examplesFiltered' => ExamplesFilteredQuery::class,
],
'mutation' => [
'updateExample' => UpdateExampleMutation::class
Expand All @@ -41,7 +42,8 @@ protected function getEnvironmentSetUp($app)
]);

$app['config']->set('graphql.types', [
'Example' => ExampleType::class
'Example' => ExampleType::class,
'ExampleFilterInput' => ExampleFilterInputType::class,
]);

$app['config']->set('app.debug', true);
Expand Down

0 comments on commit 5bc0bea

Please sign in to comment.