Skip to content

Commit b12a0d0

Browse files
authored
fix(graphql): register types for parameter args (#6895)
1 parent 05cbd0f commit b12a0d0

File tree

2 files changed

+36
-11
lines changed

2 files changed

+36
-11
lines changed

src/GraphQl/Type/FieldsBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,15 @@ private function getResourceFieldConfiguration(?string $property, ?string $field
415415
}
416416

417417
$args = $this->getFilterArgs($args, $resourceClass, $rootResource, $resourceOperation, $rootOperation, $property, $depth);
418-
$args = $this->getParameterArgs($rootOperation, $args);
418+
419+
// Also register parameter args in the types container
420+
// Note: This is a workaround, for more information read the comment on the parameterToObjectType function.
421+
foreach ($this->getParameterArgs($rootOperation) as $key => $arg) {
422+
if ($arg instanceof InputObjectType || (\is_array($arg) && isset($arg['name']))) {
423+
$this->typesContainer->set(\is_array($arg) ? $arg['name'] : $arg->name(), $arg);
424+
}
425+
$args[$key] = $arg;
426+
}
419427
}
420428
}
421429

src/Laravel/Tests/GraphQlTest.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -79,20 +79,37 @@ public function testGetBooksWithSimplePagination(): void
7979

8080
public function testGetBooksWithPaginationAndOrder(): void
8181
{
82-
BookFactory::new()->has(AuthorFactory::new())->count(10)->create();
83-
$response = $this->postJson('/api/graphql', ['query' => '{
84-
books(first: 3, order: {name: "desc"}) {
85-
edges {
86-
node {
87-
id, name, publicationDate, author { id, name }
88-
}
89-
}
90-
}
91-
}'], ['accept' => ['application/json']]);
82+
// Create books in reverse alphabetical order to test the 'asc' order
83+
BookFactory::new()
84+
->count(10)
85+
->sequence(fn ($sequence) => ['name' => \chr(122 - $sequence->index)]) // ASCII codes starting from 'z'
86+
->has(AuthorFactory::new())
87+
->create();
88+
89+
$response = $this->postJson('/api/graphql', [
90+
'query' => '
91+
query getBooks($first: Int!, $order: orderBookcollection_query!) {
92+
books(first: $first, order: $order) {
93+
edges {
94+
node {
95+
id, name, publicationDate, author { id, name }
96+
}
97+
}
98+
}
99+
}
100+
',
101+
'variables' => [
102+
'first' => 3,
103+
'order' => ['name' => 'asc'],
104+
],
105+
], ['accept' => ['application/json']]);
92106
$response->assertStatus(200);
93107
$data = $response->json();
94108
$this->assertArrayHasKey('data', $data);
95109
$this->assertCount(3, $data['data']['books']['edges']);
110+
$this->assertEquals('q', $data['data']['books']['edges'][0]['node']['name']);
111+
$this->assertEquals('r', $data['data']['books']['edges'][1]['node']['name']);
112+
$this->assertEquals('s', $data['data']['books']['edges'][2]['node']['name']);
96113
$this->assertArrayNotHasKey('errors', $data);
97114
}
98115

0 commit comments

Comments
 (0)