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

Appearance settings

Commit e9b54d6

Browse filesBrowse files
author
emfrouin
authored
feat: Allow callable fields in entity validation (#8)
* feat: Allow callable fields in entity validation * feat: Allow callable in federated schema
1 parent 1ce511e commit e9b54d6
Copy full SHA for e9b54d6

File tree

Expand file treeCollapse file tree

4 files changed

+44
-3
lines changed
Filter options
Expand file treeCollapse file tree

4 files changed

+44
-3
lines changed

‎src/FederatedSchema.php

Copy file name to clipboardExpand all lines: src/FederatedSchema.php
+3Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ private function getEntityDirectivesConfig(): array
105105
private function getQueryTypeConfig(array $config): array
106106
{
107107
$queryTypeConfig = $config['query']->config;
108+
if (is_callable($queryTypeConfig['fields'])) {
109+
$queryTypeConfig['fields'] = $queryTypeConfig['fields']();
110+
}
108111

109112
$queryTypeConfig['fields'] = array_merge(
110113
$queryTypeConfig['fields'],

‎src/Types/EntityObjectType.php

Copy file name to clipboardExpand all lines: src/Types/EntityObjectType.php
+15-3Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,15 @@ private function validateReferenceKeys($ref)
123123

124124
private static function validateFields(array $config)
125125
{
126-
Utils::invariant(isset($config['fields']) && is_array($config['fields']), 'Fields must be specified.');
126+
if (is_callable($config['fields'])) {
127+
$fields = $config['fields']();
128+
} else {
129+
$fields = $config['fields'];
130+
}
131+
132+
Utils::invariant(isset($fields) && is_array($fields), 'Fields must be specified.');
127133

128-
foreach ($config['fields'] as $field) {
134+
foreach ($fields as $field) {
129135
if (isset($field['isExternal'])) {
130136
Utils::invariant(is_bool($field['isExternal']), "Config property 'isExternal' should be a boolean.");
131137
}
@@ -142,14 +148,20 @@ private static function validateFields(array $config)
142148

143149
public static function validateKeyFields(array $config)
144150
{
151+
if (is_callable($config['fields'])) {
152+
$fields = $config['fields']();
153+
} else {
154+
$fields = $config['fields'];
155+
}
156+
145157
Utils::invariant(
146158
isset($config['keyFields']) && is_array($config['keyFields']),
147159
'Entity key fields must be provided and has to be an array.'
148160
);
149161

150162
foreach ($config['keyFields'] as $keyField) {
151163
Utils::invariant(
152-
array_key_exists($keyField, $config['fields']),
164+
array_key_exists($keyField, $fields),
153165
'Entity key refers to a field that does not exist in the fields array.'
154166
);
155167
}

‎test/EntitiesTest.php

Copy file name to clipboardExpand all lines: test/EntitiesTest.php
+21Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ public function testCreatingEntityType()
3636
$this->assertMatchesSnapshot($userType->config);
3737
}
3838

39+
public function testCreatingEntityTypeWithCallable()
40+
{
41+
$userTypeKeyFields = ['id', 'email'];
42+
43+
$userType = new EntityObjectType([
44+
'name' => 'User',
45+
'keyFields' => $userTypeKeyFields,
46+
'fields' => function () {
47+
return [
48+
'id' => ['type' => Type::int()],
49+
'email' => ['type' => Type::string()],
50+
'firstName' => ['type' => Type::string()],
51+
'lastName' => ['type' => Type::string()]
52+
];
53+
}
54+
]);
55+
56+
$this->assertEqualsCanonicalizing($userType->getKeyFields(), $userTypeKeyFields);
57+
$this->assertMatchesSnapshot($userType->config);
58+
}
59+
3960
public function testResolvingEntityReference()
4061
{
4162
$expectedRef = [
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
name: User
2+
keyFields:
3+
- id
4+
- email
5+
fields: null

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.