|
8 | 8 | use GraphQL\Type\Definition\IDType;
|
9 | 9 | use GraphQL\Schema as GraphQLSchema;
|
10 | 10 | use GraphQL\Type\Definition\ResolveInfo;
|
11 |
| -use GraphQL\Type\Definition\UnionType; |
12 | 11 |
|
13 | 12 | class Schema {
|
14 | 13 |
|
@@ -170,44 +169,55 @@ public function entityToGqlQueryArg($entity_type, $bundle = NULL) {
|
170 | 169 | public function addEntityGqlDefinition($entity_type) {
|
171 | 170 | $self = $this;
|
172 | 171 | $entity_type_info = $this->getEntityInfo($entity_type);
|
| 172 | + $isSingleBundle = FALSE; |
173 | 173 | $defs = [
|
174 | 174 | 'interface' => NULL,
|
175 | 175 | 'objects' => []
|
176 | 176 | ];
|
177 | 177 |
|
178 |
| - if (!isset($this->interfaceTypes[$entity_type])) { |
179 |
| - $defs['interface'] = $this->addInterfaceType($entity_type, [ |
180 |
| - 'name' => $entity_type, |
181 |
| - 'description' => isset($entity_type_info['description']) ? $entity_type_info['description'] : '', |
182 |
| - 'fields' => $this->getFields($entity_type), |
183 |
| - 'resolveType' => function ($obj) use($entity_type, &$self) { |
184 |
| - list($id, $rid, $bundle) = entity_extract_ids($entity_type, $obj); |
185 |
| - $objectType = $self->objectTypes[str_replace('-', '_', $entity_type .'__'. $bundle)]; |
186 |
| - return $objectType; |
187 |
| - }, |
188 |
| - '__entity_bundle' => null, |
189 |
| - '__entity_type' => $entity_type |
190 |
| - ]); |
191 |
| - } else { |
192 |
| - $defs['interface'] = $this->interfaceTypes[$entity_type]; |
| 178 | + if (count($entity_type_info['bundles']) === 1 && key($entity_type_info['bundles']) === $entity_type) { |
| 179 | + $isSingleBundle = TRUE; |
| 180 | + } |
| 181 | + |
| 182 | + if (!$isSingleBundle) { |
| 183 | + if (!isset($this->interfaceTypes[$entity_type])) { |
| 184 | + $defs['interface'] = $this->addInterfaceType($entity_type, [ |
| 185 | + 'name' => $entity_type, |
| 186 | + 'description' => isset($entity_type_info['description']) ? $entity_type_info['description'] : '', |
| 187 | + 'fields' => $this->getFields($entity_type), |
| 188 | + 'resolveType' => function ($obj) use($entity_type, &$self) { |
| 189 | + list($id, $rid, $bundle) = entity_extract_ids($entity_type, $obj); |
| 190 | + $objectType = $self->objectTypes[$bundle]; |
| 191 | + return $objectType; |
| 192 | + }, |
| 193 | + '__entity_bundle' => null, |
| 194 | + '__entity_type' => $entity_type |
| 195 | + ]); |
| 196 | + } else { |
| 197 | + $defs['interface'] = $this->interfaceTypes[$entity_type]; |
| 198 | + } |
193 | 199 | }
|
194 | 200 |
|
| 201 | + // entity have single bundle: user, taxonomy_vocabulary, file |
195 | 202 | foreach ($entity_type_info['bundles'] as $bundle => $bundle_info) {
|
196 |
| - $machine_name = str_replace('-', '_', "{$entity_type}__{$bundle}"); |
197 |
| - if (!isset($this->objectTypes[$machine_name])) { |
198 |
| - $defs['objects'][$machine_name] = $this->addObjectType($machine_name, [ |
199 |
| - 'name' => $machine_name, |
| 203 | + if (!isset($this->objectTypes[$bundle])) { |
| 204 | + $defs['objects'][$bundle] = $this->addObjectType($bundle, [ |
| 205 | + 'name' => $bundle, |
200 | 206 | 'description' => '',
|
201 | 207 | 'fields' => $this->getFields($entity_type, $bundle),
|
202 |
| - 'interfaces' => [$this->interfaceTypes[$entity_type]], |
| 208 | + 'interfaces' => $isSingleBundle ? [] : [$this->interfaceTypes[$entity_type]], |
203 | 209 | '__entity_bundle' => $bundle,
|
204 | 210 | '__entity_type' => $entity_type
|
205 | 211 | ]);
|
206 | 212 | } else {
|
207 |
| - $defs['objects'][$machine_name] = $this->objectTypes[$machine_name]; |
| 213 | + $defs['objects'][$bundle] = $this->objectTypes[$bundle]; |
208 | 214 | }
|
209 | 215 | }
|
210 | 216 |
|
| 217 | + if ($isSingleBundle) { |
| 218 | + $defs['interface'] = reset($defs['objects']); |
| 219 | + } |
| 220 | + |
211 | 221 | return $defs;
|
212 | 222 | }
|
213 | 223 |
|
@@ -270,8 +280,15 @@ public function getFields($entity_type, $bundle = '') {
|
270 | 280 | if ($bundle && !empty($properties_info['bundles'][$bundle])) {
|
271 | 281 | foreach ($properties_info['bundles'][$bundle]['properties'] as $field => $field_info) {
|
272 | 282 | $fieldType = $this->gqlFieldType($field_info['type'], ['entity_type' => $entity_type, 'bundle' => $bundle, 'property' => $field, 'info' => $field_info]);
|
273 |
| - if (!$fieldType) continue; |
274 |
| - $fields[$field] = [ |
| 283 | + if (!$fieldType) { |
| 284 | + throw new Error("Cannot detect field type of {$field}"); |
| 285 | + } |
| 286 | + $short_field = preg_replace('/^field_/', '', $field); |
| 287 | + if (isset($fields[$short_field])) { |
| 288 | + $short_field = $field; |
| 289 | + } |
| 290 | + |
| 291 | + $fields[$short_field] = [ |
275 | 292 | 'type' => $fieldType,
|
276 | 293 | 'description' => $field_info['description'],
|
277 | 294 | 'resolve' => function ($value, $args, $context, ResolveInfo $info) use ($entity_type, $bundle, $field) {
|
|
0 commit comments