Skip to content

Commit 1e11044

Browse files
popovserhiiPowerKiKi
authored andcommitted
An entity without relation to other entity must not have joins
In our case `JoinOnNoInversedBy` cannot have a `joins` field because the entity NoInversedBy has no relation to any other entity. Fixes #23
1 parent 3e18176 commit 1e11044

File tree

4 files changed

+97
-8
lines changed

4 files changed

+97
-8
lines changed

src/Factory/Type/JoinOnTypeFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,21 @@ public function create(string $className, string $typeName): Type
4646
$type = new InputObjectType([
4747
'name' => $typeName,
4848
'fields' => function () use ($className): array {
49-
return [
49+
$fields = [
5050
[
5151
'name' => 'type',
5252
'type' => $this->types->get('JoinType'),
5353
'defaultValue' => 'innerJoin',
5454
],
55-
$this->filterGroupJoinTypeFactory->getField($className),
5655
$this->filterGroupConditionTypeFactory->getField($className),
5756
];
57+
58+
// Only create join type, if there is anything to join on
59+
if ($this->filterGroupJoinTypeFactory->canCreate($className)) {
60+
$fields[] = $this->filterGroupJoinTypeFactory->getField($className);
61+
}
62+
63+
return $fields;
5864
},
5965
]);
6066

tests/Blog/Model/Post.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use DateTime;
88
use Doctrine\ORM\Mapping as ORM;
99
use GraphQL\Doctrine\Annotation as API;
10+
use GraphQLTests\Doctrine\Blog\Model\Special\NoInversedBy;
1011

1112
/**
1213
* A blog post with title and body
@@ -57,6 +58,13 @@ final class Post extends AbstractModel
5758
*/
5859
private $user;
5960

61+
/**
62+
* @var NoInversedBy
63+
*
64+
* @ORM\ManyToOne(targetEntity="GraphQLTests\Doctrine\Blog\Model\Special\NoInversedBy")
65+
*/
66+
private $noInversedBy;
67+
6068
/**
6169
* Set title
6270
*
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace GraphQLTests\Doctrine\Blog\Model\Special;
6+
7+
use Doctrine\ORM\Mapping as ORM;
8+
use GraphQLTests\Doctrine\Blog\Model\AbstractModel;
9+
10+
/**
11+
* @ORM\Entity
12+
*/
13+
final class NoInversedBy extends AbstractModel
14+
{
15+
}

tests/data/PostFilter.graphqls

Lines changed: 66 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,28 +169,35 @@ input InOperatorString {
169169
not: Boolean = false
170170
}
171171

172+
input JoinOnNoInversedBy {
173+
type: JoinType = innerJoin
174+
175+
"""Conditions to be applied on fields"""
176+
conditions: [NoInversedByFilterGroupCondition!]
177+
}
178+
172179
input JoinOnPost {
173180
type: JoinType = innerJoin
174181

182+
"""Conditions to be applied on fields"""
183+
conditions: [PostFilterGroupCondition!]
184+
175185
"""
176186
Optional joins to either filter the query or fetch related objects from DB in a single query
177187
"""
178188
joins: PostFilterGroupJoin
179-
180-
"""Conditions to be applied on fields"""
181-
conditions: [PostFilterGroupCondition!]
182189
}
183190

184191
input JoinOnUser {
185192
type: JoinType = innerJoin
186193

194+
"""Conditions to be applied on fields"""
195+
conditions: [UserFilterGroupCondition!]
196+
187197
"""
188198
Optional joins to either filter the query or fetch related objects from DB in a single query
189199
"""
190200
joins: UserFilterGroupJoin
191-
192-
"""Conditions to be applied on fields"""
193-
conditions: [UserFilterGroupCondition!]
194201
}
195202

196203
"""Join types to be used in DQL"""
@@ -269,6 +276,41 @@ input ModuloOperatorInt {
269276
value: Int!
270277
}
271278

279+
"""Type to specify conditions on fields"""
280+
input NoInversedByFilterGroupCondition {
281+
creationDate: NoInversedByFilterGroupConditionCreationDate
282+
id: NoInversedByFilterGroupConditionId
283+
}
284+
285+
"""Type to specify a condition on a specific field"""
286+
input NoInversedByFilterGroupConditionCreationDate {
287+
between: BetweenOperatorDateTime
288+
equal: EqualOperatorDateTime
289+
greater: GreaterOperatorDateTime
290+
greaterOrEqual: GreaterOrEqualOperatorDateTime
291+
in: InOperatorDateTime
292+
less: LessOperatorDateTime
293+
lessOrEqual: LessOrEqualOperatorDateTime
294+
like: LikeOperatorDateTime
295+
null: NullOperatorDateTime
296+
group: GroupOperatorDateTime
297+
}
298+
299+
"""Type to specify a condition on a specific field"""
300+
input NoInversedByFilterGroupConditionId {
301+
between: BetweenOperatorID
302+
equal: EqualOperatorID
303+
greater: GreaterOperatorID
304+
greaterOrEqual: GreaterOrEqualOperatorID
305+
in: InOperatorID
306+
less: LessOperatorID
307+
lessOrEqual: LessOrEqualOperatorID
308+
like: LikeOperatorID
309+
null: NullOperatorID
310+
group: GroupOperatorID
311+
modulo: ModuloOperatorInt
312+
}
313+
272314
input NullOperatorBoolean {
273315
not: Boolean = false
274316
}
@@ -316,6 +358,7 @@ input PostFilterGroupCondition {
316358
status: PostFilterGroupConditionStatus
317359
id: PostFilterGroupConditionId
318360
user: PostFilterGroupConditionUser
361+
noInversedBy: PostFilterGroupConditionNoInversedBy
319362
custom: PostFilterGroupConditionCustom
320363
}
321364

@@ -367,6 +410,22 @@ input PostFilterGroupConditionId {
367410
modulo: ModuloOperatorInt
368411
}
369412

413+
"""Type to specify a condition on a specific field"""
414+
input PostFilterGroupConditionNoInversedBy {
415+
have: HaveOperatorID
416+
empty: EmptyOperatorID
417+
between: BetweenOperatorID
418+
equal: EqualOperatorID
419+
greater: GreaterOperatorID
420+
greaterOrEqual: GreaterOrEqualOperatorID
421+
in: InOperatorID
422+
less: LessOperatorID
423+
lessOrEqual: LessOrEqualOperatorID
424+
like: LikeOperatorID
425+
null: NullOperatorID
426+
group: GroupOperatorID
427+
}
428+
370429
"""Type to specify a condition on a specific field"""
371430
input PostFilterGroupConditionPublicationDate {
372431
between: BetweenOperatorDateTime
@@ -428,6 +487,7 @@ input PostFilterGroupConditionUser {
428487
"""Type to specify join tables in a filter"""
429488
input PostFilterGroupJoin {
430489
user: JoinOnUser
490+
noInversedBy: JoinOnNoInversedBy
431491
}
432492

433493
input SearchOperatorString {

0 commit comments

Comments
 (0)