Skip to content

Commit 654e42e

Browse files
authored
Merge pull request #1886 from alanpoulain/fix/introspection-custom-types
[GraphQL] Fix introspection (graphql-php / GraphiQL)
2 parents dcdea86 + d317e94 commit 654e42e

File tree

7 files changed

+19
-470
lines changed

7 files changed

+19
-470
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@
6767
"symfony/validator": "^3.3 || ^4.0",
6868
"symfony/web-profiler-bundle": "^3.3 || ^4.0",
6969
"symfony/yaml": "^3.3 || ^4.0",
70-
"webonyx/graphql-php": "^0.10.2"
70+
"webonyx/graphql-php": "^0.11.5"
7171
},
7272
"conflict": {
7373
"symfony/dependency-injection": "<3.4"

features/bootstrap/GraphqlContext.php

+10
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Behat\Gherkin\Node\PyStringNode;
1818
use Behatch\Context\RestContext;
1919
use Behatch\HttpCall\Request;
20+
use GraphQL\Type\Introspection;
2021

2122
/**
2223
* Context for GraphQL.
@@ -95,6 +96,15 @@ public function ISendTheGraphqlRequestWithOperation(string $operation)
9596
$this->sendGraphqlRequest();
9697
}
9798

99+
/**
100+
* @When I send the query to introspect the schema
101+
*/
102+
public function ISendTheQueryToIntrospectTheSchema()
103+
{
104+
$this->graphqlRequest = ['query' => Introspection::getIntrospectionQuery()];
105+
$this->sendGraphqlRequest();
106+
}
107+
98108
private function sendGraphqlRequest()
99109
{
100110
$this->request->setHttpHeader('Accept', null);

features/graphql/introspection.feature

+3-10
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,13 @@ Feature: GraphQL introspection support
99
And the JSON node "errors[0].message" should be equal to "GraphQL query is not valid"
1010

1111
Scenario: Introspect the GraphQL schema
12-
When I send the following GraphQL request:
13-
"""
14-
{
15-
__schema {
16-
types {
17-
name
18-
}
19-
}
20-
}
21-
"""
12+
When I send the query to introspect the schema
2213
Then the response status code should be 200
2314
And the response should be in JSON
2415
And the header "Content-Type" should be equal to "application/json"
2516
And the JSON node "data.__schema.types" should exist
17+
And the JSON node "data.__schema.queryType.name" should be equal to "Query"
18+
And the JSON node "data.__schema.mutationType.name" should be equal to "Mutation"
2619

2720
Scenario: Introspect types
2821
When I send the following GraphQL request:

features/graphql/mutation.feature

-38
Original file line numberDiff line numberDiff line change
@@ -106,44 +106,6 @@ Feature: GraphQL mutation support
106106
And the JSON node "data.createDummy.arrayData[1]" should be equal to baz
107107
And the JSON node "data.createDummy.clientMutationId" should be equal to "myId"
108108

109-
Scenario: Create an item with an embedded field
110-
When I send the following GraphQL request:
111-
"""
112-
mutation {
113-
createRelatedDummy(input: {_id: 2, symfony: "symfony", embeddedDummy: {dummyName: "Embedded"}, clientMutationId: "myId"}) {
114-
id
115-
clientMutationId
116-
}
117-
}
118-
"""
119-
Then the response status code should be 200
120-
And the response should be in JSON
121-
And the header "Content-Type" should be equal to "application/json"
122-
And the JSON node "data.createRelatedDummy.id" should be equal to "/related_dummies/2"
123-
And the JSON node "data.createRelatedDummy.clientMutationId" should be equal to "myId"
124-
125-
Scenario: Create an item and update a nested resource through a mutation
126-
When I send the following GraphQL request:
127-
"""
128-
mutation {
129-
createRelationEmbedder(input: {paris: "paris", krondstadt: "Krondstadt", anotherRelated: {id: 2, symfony: "laravel"}, clientMutationId: "myId"}) {
130-
id
131-
anotherRelated {
132-
id
133-
symfony
134-
}
135-
clientMutationId
136-
}
137-
}
138-
"""
139-
Then the response status code should be 200
140-
And the response should be in JSON
141-
And the header "Content-Type" should be equal to "application/json"
142-
And the JSON node "data.createRelationEmbedder.id" should be equal to "/relation_embedders/1"
143-
And the JSON node "data.createRelationEmbedder.anotherRelated.id" should be equal to "/related_dummies/2"
144-
And the JSON node "data.createRelationEmbedder.anotherRelated.symfony" should be equal to "laravel"
145-
And the JSON node "data.createRelationEmbedder.clientMutationId" should be equal to "myId"
146-
147109
Scenario: Delete an item through a mutation
148110
When I send the following GraphQL request:
149111
"""

src/GraphQl/Type/Definition/InputUnionType.php

-187
This file was deleted.

src/GraphQl/Type/SchemaBuilder.php

+5-26
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use ApiPlatform\Core\Exception\ResourceClassNotFoundException;
1717
use ApiPlatform\Core\GraphQl\Resolver\Factory\ResolverFactoryInterface;
1818
use ApiPlatform\Core\GraphQl\Serializer\ItemNormalizer;
19-
use ApiPlatform\Core\GraphQl\Type\Definition\InputUnionType;
2019
use ApiPlatform\Core\GraphQl\Type\Definition\IterableType;
2120
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
2221
use ApiPlatform\Core\Metadata\Property\Factory\PropertyNameCollectionFactoryInterface;
@@ -343,24 +342,13 @@ private function convertType(Type $type, bool $input = false, string $mutationNa
343342
break;
344343
case Type::BUILTIN_TYPE_ARRAY:
345344
case Type::BUILTIN_TYPE_ITERABLE:
346-
$graphqlType = $this->getIterableType();
345+
if (!isset($this->graphqlTypes['#iterable'])) {
346+
$this->graphqlTypes['#iterable'] = new IterableType();
347+
}
348+
$graphqlType = $this->graphqlTypes['#iterable'];
347349
break;
348350
case Type::BUILTIN_TYPE_OBJECT:
349-
if ($input && $depth > 0) {
350-
if (!isset($this->graphqlTypes['#stringIterableUnionInput'])) {
351-
$this->graphqlTypes['#stringIterableUnionInput'] = new InputUnionType([
352-
'name' => 'StringIterableUnionInput',
353-
'description' => 'Resource\'s IRI or data (embedded entities or when updating a related existing resource)',
354-
'types' => [
355-
GraphQLType::string(),
356-
$this->getIterableType(),
357-
],
358-
]);
359-
}
360-
$graphqlType = $this->graphqlTypes['#stringIterableUnionInput'];
361-
break;
362-
}
363-
if (is_a($type->getClassName(), \DateTimeInterface::class, true)) {
351+
if (($input && $depth > 0) || is_a($type->getClassName(), \DateTimeInterface::class, true)) {
364352
$graphqlType = GraphQLType::string();
365353
break;
366354
}
@@ -511,15 +499,6 @@ private function getResourcePaginatedCollectionType(string $resourceClass, Graph
511499
return $this->graphqlTypes[$resourceClass]['connection'] = new ObjectType($configuration);
512500
}
513501

514-
private function getIterableType(): IterableType
515-
{
516-
if (!isset($this->graphqlTypes['#iterable'])) {
517-
$this->graphqlTypes['#iterable'] = new IterableType();
518-
}
519-
520-
return $this->graphqlTypes['#iterable'];
521-
}
522-
523502
private function isCollection(Type $type): bool
524503
{
525504
return $type->isCollection() && Type::BUILTIN_TYPE_OBJECT === $type->getBuiltinType();

0 commit comments

Comments
 (0)