Skip to content

Commit 53555f6

Browse files
committed
Fix graphql support for relationship fieldtype
1 parent 9c9af00 commit 53555f6

File tree

5 files changed

+48
-8
lines changed

5 files changed

+48
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99

1010
- Filesystem errors on some Windows environments when running coilpack install command
1111
- GraphQL fieldtype modifier registration bug
12-
- Use coding standard
12+
- GraphQL support for relationship fieldtype
13+
14+
### Changed
15+
16+
- Use Laravel coding standard
1317
- Improved typehints
1418

1519
## [0.0.1] - 2022-12-29

src/Api/Graph/Queries/ChannelEntriesQuery.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ public function args(): array
2626
'channel' => [
2727
'type' => Type::string(),
2828
],
29+
'title' => [
30+
'type' => Type::string(),
31+
],
2932
'status' => [
3033
'type' => Type::string(),
3134
],
@@ -45,14 +48,19 @@ public function resolve($root, array $args, $context, ResolveInfo $info, \Closur
4548

4649
$entries = ChannelEntry::select($select)->with($with);
4750

48-
// Filter by Channel name
51+
// Filter by Channel Name
4952
$entries->when($args['channel'] ?? false, function ($query) use ($args) {
5053
$query->whereHas('channel', function ($query) use ($args) {
5154
$query->where('channel_name', $args['channel']);
5255
});
5356
});
5457

55-
// Filter by status
58+
// Filter by Entry Title
59+
$entries->when($args['title'] ?? false, function ($query) use ($args) {
60+
$query->where('title', $args['title']);
61+
});
62+
63+
// Filter by Status
5664
$entries->when($args['status'] ?? false, function ($query) use ($args) {
5765
$query->where('status', $args['status']);
5866
});

src/FieldtypeOutput.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public function toJson()
6262

6363
public function getIterator(): \Traversable
6464
{
65+
// If we have an object set and it is iterable we will prefer to use that
66+
if ($this->object && method_exists($this->object, 'getIterator')) {
67+
return $this->object->getIterator();
68+
}
69+
6570
if (is_object($this->array) && method_exists($this->array, 'getIterator')) {
6671
return $this->array->getIterator();
6772
}
@@ -129,6 +134,10 @@ public function __toString()
129134

130135
public function __get($key)
131136
{
137+
if ($this->object && property_exists($this->object, $key)) {
138+
return $this->object->$key;
139+
}
140+
132141
if (array_key_exists($key, (array) $this->array)) {
133142
return $this->array[$key];
134143
}

src/Fieldtypes/Relationship.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ protected function loadData(FieldContent $content)
4646

4747
public function graphType()
4848
{
49-
return \Rebing\GraphQL\Support\Facades\GraphQL::type('ChannelEntry');
50-
51-
return \GraphQL\Type\Definition\Type::listOf(
52-
\Rebing\GraphQL\Support\Facades\GraphQL::type('ChannelEntry')
53-
);
49+
return 'ChannelEntry';
5450
}
5551
}

tests/Graphql/ChannelEntriesTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,29 @@ public function test_entries_grid_field_image_modifier()
133133
->assertJsonFragment(['width' => 100]);
134134
}
135135

136+
public function test_entries_relationship()
137+
{
138+
$this->postJson('graphql', [
139+
'query' => <<<GQL
140+
{
141+
channel_entries(title:"Test Fieldtypes" limit:1){
142+
entry_id
143+
test_relationships {
144+
title
145+
blog_audio {
146+
id
147+
type
148+
}
149+
}
150+
}
151+
}
152+
GQL
153+
])
154+
->assertJsonFragment(['title' => 'Entry with SoundCloud audio'])
155+
->assertJsonFragment(['id' => '164768245'])
156+
->assertJsonFragment(['type' => 'soundcloud']);
157+
}
158+
136159
public function test_entries_text_field_modifier()
137160
{
138161
$this->postJson('graphql', [

0 commit comments

Comments
 (0)