Skip to content

Commit

Permalink
fix(graphql): wrapped type should always take the query normalization…
Browse files Browse the repository at this point in the history
… context
  • Loading branch information
alanpoulain committed Jun 27, 2022
1 parent ca6353c commit f37afb0
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
2 changes: 1 addition & 1 deletion features/graphql/input_output.feature
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ Feature: GraphQL DTO input and output
{
"errors": [
{
"message": "Cannot query field \"id\" on type \"createDummyDtoNoOutputPayloadData\".",
"message": "Cannot query field \"id\" on type \"DummyDtoNoOutput\".",
"extensions": {
"category": "graphql"
},
Expand Down
2 changes: 1 addition & 1 deletion features/graphql/mutation.feature
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ Feature: GraphQL mutation support
And the header "Content-Type" should be equal to "application/json"
And the JSON node "data.createFoo.foo.id" should be equal to "/foos/1"
And the JSON node "data.createFoo.foo._id" should be equal to 1
And the JSON node "data.createFoo.foo.__typename" should be equal to "createFooPayloadData"
And the JSON node "data.createFoo.foo.__typename" should be equal to "Foo"
And the JSON node "data.createFoo.foo.name" should be equal to "A new one"
And the JSON node "data.createFoo.foo.bar" should be equal to "new"
And the JSON node "data.createFoo.clientMutationId" should be equal to "myId"
Expand Down
20 changes: 15 additions & 5 deletions src/GraphQl/Type/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,7 @@ public function getResourceObjectType(?string $resourceClass, ResourceMetadataCo
'resolveField' => $this->defaultFieldResolver,
'fields' => function () use ($resourceClass, $operation, $operationName, $resourceMetadataCollection, $input, $wrapData, $depth, $ioMetadata) {
if ($wrapData) {
try {
$queryNormalizationContext = $operation instanceof Query ? ($resourceMetadataCollection->getOperation($operationName)->getNormalizationContext() ?? []) : [];
} catch (OperationNotFoundException $e) {
$queryNormalizationContext = [];
}
$queryNormalizationContext = $this->getQueryOperation($resourceMetadataCollection)?->getNormalizationContext() ?? [];

try {
$mutationNormalizationContext = $operation instanceof Mutation || $operation instanceof Subscription ? ($resourceMetadataCollection->getOperation($operationName)->getNormalizationContext() ?? []) : [];
Expand Down Expand Up @@ -310,4 +306,18 @@ private function getPageBasedPaginationFields(GraphQLType $resourceType): array
'paginationInfo' => GraphQLType::nonNull($paginationInfoObjectType),
];
}

private function getQueryOperation(ResourceMetadataCollection $resourceMetadataCollection): ?Operation
{
foreach ($resourceMetadataCollection as $resourceMetadata) {
foreach ($resourceMetadata->getGraphQlOperations() as $operation) {
// Filter the custom queries.
if ($operation instanceof Query && !$operation->getResolver()) {
return $operation;
}
}
}

return null;
}
}

0 comments on commit f37afb0

Please sign in to comment.