Skip to content

Commit

Permalink
Merge pull request #75 from moufmouf/no_query_mutation
Browse files Browse the repository at this point in the history
Adding placeholder when there are no queries or no mutations
  • Loading branch information
moufmouf committed Jan 7, 2019
2 parents b7bcf8f + 23f3f96 commit 3977d87
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 4 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ language: php
php:
- 7.1
- 7.2
- 7.3
cache:
directories:
- $HOME/.composer/cache
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/SourceField.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public function isLogged(): bool

/**
* Returns the GraphQL return type of the request (as a string).
* The string can represent the FQCN of the type or an entry in the container resolving to the GraphQL type.
* The string is the GraphQL output type name.
*
* @return string|null
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/SourceFieldInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function isLogged(): bool;

/**
* Returns the GraphQL return type of the request (as a string).
* The string can represent the FQCN of the type or an entry in the container resolving to the GraphQL type.
* The string is the GraphQL output type name.
*
* @return string|null
*/
Expand Down
1 change: 0 additions & 1 deletion src/FieldsBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,6 @@ private function mapType(Type $type, ?Type $docBlockType, bool $isNullable, bool
}
}


return $graphQlType;
}

Expand Down
27 changes: 26 additions & 1 deletion src/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
namespace TheCodingMachine\GraphQL\Controllers;

use GraphQL\Type\Definition\ObjectType;
use GraphQL\Type\Definition\Type;
use GraphQL\Type\SchemaConfig;
use TheCodingMachine\GraphQL\Controllers\Containers\BasicAutoWiringContainer;
use TheCodingMachine\GraphQL\Controllers\Mappers\RecursiveTypeMapperInterface;
Expand All @@ -26,13 +27,37 @@ public function __construct(QueryProviderInterface $queryProvider, RecursiveType
$query = new ObjectType([
'name' => 'Query',
'fields' => function() use ($queryProvider) {
$queries = $queryProvider->getQueries();
if (empty($queries)) {
return [
'dummyQuery' => [
'type' => Type::string(),
'description' => 'A placeholder query used by thecodingmachine/graphql-controllers when there are no declared queries.',
'resolve' => function () {
return 'This is a placeholder query. Please create a query using the @Query annotation.';
}
]
];
}
return $queryProvider->getQueries();
}
]);
$mutation = new ObjectType([
'name' => 'Mutation',
'fields' => function() use ($queryProvider) {
return $queryProvider->getMutations();
$mutations = $queryProvider->getMutations();
if (empty($mutations)) {
return [
'dummyMutation' => [
'type' => Type::string(),
'description' => 'A placeholder query used by thecodingmachine/graphql-controllers when there are no declared mutations.',
'resolve' => function () {
return 'This is a placeholder mutation. Please create a mutation using the @Mutation annotation.';
}
]
];
}
return $mutations;
}
]);

Expand Down
36 changes: 36 additions & 0 deletions tests/SchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace TheCodingMachine\GraphQL\Controllers;

use PHPUnit\Framework\TestCase;

class SchemaTest extends AbstractQueryProviderTest
{

public function testEmptyQuery()
{
$queryProvider = new class implements QueryProviderInterface {
public function getQueries(): array
{
return [];
}

public function getMutations(): array
{
return [];
}
};

$schema = new Schema($queryProvider, $this->getTypeMapper(), $this->getTypeResolver());

$fields = $schema->getQueryType()->getFields();
$this->assertArrayHasKey('dummyQuery', $fields);
$resolve = $fields['dummyQuery']->resolveFn;
$this->assertSame('This is a placeholder query. Please create a query using the @Query annotation.', $resolve());

$fields = $schema->getMutationType()->getFields();
$this->assertArrayHasKey('dummyMutation', $fields);
$resolve = $fields['dummyMutation']->resolveFn;
$this->assertSame('This is a placeholder mutation. Please create a mutation using the @Mutation annotation.', $resolve());
}
}

0 comments on commit 3977d87

Please sign in to comment.