Skip to content

Commit 06b62ae

Browse files
authored
fix(entity-refs): Allow partial references in resolveReference (#20)
* fix: Require one entity key instead of all * fix: Add entity validation tests
1 parent e195b5d commit 06b62ae

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/Types/EntityObjectType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ private function validateReferenceKeys($ref)
111111
Utils::invariant(isset($ref['__typename']), 'Type name must be provided in the reference.');
112112

113113
$refKeys = array_keys($ref);
114-
$refContainsKeys = count(array_intersect($this->getKeyFields(), $refKeys)) === count($this->getKeyFields());
114+
$refContainsKeys = !empty(array_intersect($this->getKeyFields(), $refKeys));
115115

116116
Utils::invariant($refContainsKeys, 'Key fields are missing from the entity reference.');
117117
}

test/EntitiesTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,56 @@ public function testResolvingEntityReference()
8686
$this->assertEquals($expectedRef, $actualRef);
8787
}
8888

89+
public function testResolvingEntityReferenceWithoutAllKeys()
90+
{
91+
$expectedRef = [
92+
'id' => 1,
93+
'email' => '[email protected]',
94+
'firstName' => 'Luke',
95+
'lastName' => 'Skywalker',
96+
'__typename' => 'User'
97+
];
98+
99+
$userType = new EntityObjectType([
100+
'name' => 'User',
101+
'keyFields' => ['id', 'email'],
102+
'fields' => [
103+
'id' => ['type' => Type::int()],
104+
'email' => ['type' => Type::string()],
105+
'firstName' => ['type' => Type::string()],
106+
'lastName' => ['type' => Type::string()]
107+
],
108+
'__resolveReference' => function () use ($expectedRef) {
109+
return $expectedRef;
110+
}
111+
]);
112+
113+
$actualRef = $userType->resolveReference(['email' => '[email protected]', '__typename' => 'User']);
114+
115+
$this->assertEquals($expectedRef, $actualRef);
116+
}
117+
118+
public function testResolvingEntityReferenceWithoutAnyKeys()
119+
{
120+
$userType = new EntityObjectType([
121+
'name' => 'User',
122+
'keyFields' => ['id', 'email'],
123+
'fields' => [
124+
'id' => ['type' => Type::int()],
125+
'email' => ['type' => Type::string()],
126+
'firstName' => ['type' => Type::string()],
127+
'lastName' => ['type' => Type::string()]
128+
],
129+
'__resolveReference' => function () {
130+
return null;
131+
}
132+
]);
133+
134+
$this->expectException(InvariantViolation::class);
135+
136+
$userType->resolveReference(['__typename' => 'User']);
137+
}
138+
89139
public function testCreatingEntityRefType()
90140
{
91141
$userTypeKeyFields = ['id', 'email'];

0 commit comments

Comments
 (0)