Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit06b62ae

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 parente195b5d commit06b62ae

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+
publicfunctiontestResolvingEntityReferenceWithoutAllKeys()
90+
{
91+
$expectedRef = [
92+
'id' =>1,
93+
'email' =>'luke@skywalker.com',
94+
'firstName' =>'Luke',
95+
'lastName' =>'Skywalker',
96+
'__typename' =>'User'
97+
];
98+
99+
$userType =newEntityObjectType([
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' =>'luke@skywalker.com','__typename' =>'User']);
114+
115+
$this->assertEquals($expectedRef,$actualRef);
116+
}
117+
118+
publicfunctiontestResolvingEntityReferenceWithoutAnyKeys()
119+
{
120+
$userType =newEntityObjectType([
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+
returnnull;
131+
}
132+
]);
133+
134+
$this->expectException(InvariantViolation::class);
135+
136+
$userType->resolveReference(['__typename' =>'User']);
137+
}
138+
89139
publicfunctiontestCreatingEntityRefType()
90140
{
91141
$userTypeKeyFields = ['id','email'];

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp