Skip to content

Commit 7e844c6

Browse files
committed
Shortened field name, merge entity type & bundle if duplicate, add screenshot.
1 parent e211ea0 commit 7e844c6

File tree

5 files changed

+58
-30
lines changed

5 files changed

+58
-30
lines changed

README.md

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ GraphQL API for Drupal 7
33

44
[![Build Status](https://travis-ci.org/olragon/graphql_api.svg?branch=master)](https://travis-ci.org/olragon/graphql_api)
55

6+
![GraphQL API with GraphiQL](/screenshot.png?raw=true "GraphQL API with GraphiQL")
7+
68
The problems
79
------------
810

@@ -41,8 +43,17 @@ The plans
4143
- Field API: entityreference -> Interface/Object target entity
4244
- Field API: relation -> Interface/Object target entity
4345
3. Create GraphQL endpoint `/graphql`
44-
- receive POST content with GrapQL query and variables
45-
- execute query and return result
46+
- receive POST content with GraphQL query and variables
47+
- query using Drupal's `EntityFieldQuery`
48+
- check entity access using `entity_access`
49+
- resolve property using `entity_metadata_wrapper`
50+
- return result
4651

4752
[GraphQL]: https://www.drupal.org/project/graphql "GraphQL module"
4853
[https://chrome.google.com/webstore/detail/graphiql-feen/mcbfdonlkfpbfdpi…]: https://chrome.google.com/webstore/detail/graphiql-feen/mcbfdonlkfpbfdpimkjilhdneikhfklp
54+
55+
Notes
56+
-----
57+
58+
1. Field will be shortened `field_tags` -> `tags`
59+
2. If entity type have single bundle, we skip GraphQL interface and just use GraphQL object. Eg: user, file, ...

screenshot.png

171 KB
Loading

src/Schema.php

Lines changed: 41 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use GraphQL\Type\Definition\IDType;
99
use GraphQL\Schema as GraphQLSchema;
1010
use GraphQL\Type\Definition\ResolveInfo;
11-
use GraphQL\Type\Definition\UnionType;
1211

1312
class Schema {
1413

@@ -170,44 +169,55 @@ public function entityToGqlQueryArg($entity_type, $bundle = NULL) {
170169
public function addEntityGqlDefinition($entity_type) {
171170
$self = $this;
172171
$entity_type_info = $this->getEntityInfo($entity_type);
172+
$isSingleBundle = FALSE;
173173
$defs = [
174174
'interface' => NULL,
175175
'objects' => []
176176
];
177177

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+
}
193199
}
194200

201+
// entity have single bundle: user, taxonomy_vocabulary, file
195202
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,
200206
'description' => '',
201207
'fields' => $this->getFields($entity_type, $bundle),
202-
'interfaces' => [$this->interfaceTypes[$entity_type]],
208+
'interfaces' => $isSingleBundle ? [] : [$this->interfaceTypes[$entity_type]],
203209
'__entity_bundle' => $bundle,
204210
'__entity_type' => $entity_type
205211
]);
206212
} else {
207-
$defs['objects'][$machine_name] = $this->objectTypes[$machine_name];
213+
$defs['objects'][$bundle] = $this->objectTypes[$bundle];
208214
}
209215
}
210216

217+
if ($isSingleBundle) {
218+
$defs['interface'] = reset($defs['objects']);
219+
}
220+
211221
return $defs;
212222
}
213223

@@ -270,8 +280,15 @@ public function getFields($entity_type, $bundle = '') {
270280
if ($bundle && !empty($properties_info['bundles'][$bundle])) {
271281
foreach ($properties_info['bundles'][$bundle]['properties'] as $field => $field_info) {
272282
$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] = [
275292
'type' => $fieldType,
276293
'description' => $field_info['description'],
277294
'resolve' => function ($value, $args, $context, ResolveInfo $info) use ($entity_type, $bundle, $field) {

tests/fixtures/queryNodeWithTags.graphql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
query nodeWithTags($nid: Int) {
2-
node__article (nid: $nid) {
2+
article (nid: $nid) {
33
nid,
44
title,
5-
field_tags {
5+
tags {
66
name
77
}
88
}

tests/graphql_api.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ class Graphql_API_Test extends DrupalWebTestCase {
6666
]);
6767
$this->assertEqual(curl_getinfo($this->curlHandle, CURLINFO_CONTENT_TYPE), 'application/json', 'HTTP content type is correct.');
6868
$result = drupal_json_decode($response);
69-
$this->assertEqual($result['data']['node__article'][0]['title'], $this->nodeWithTags->title, 'Node title is valid.');
70-
$this->assertEqual($result['data']['node__article'][0]['field_tags'][0]['name'], $this->tag->name, 'Tag is valid.');
69+
$this->assertEqual($result['data']['article'][0]['title'], $this->nodeWithTags->title, 'Node title is valid.');
70+
$this->assertEqual($result['data']['article'][0]['tags'][0]['name'], $this->tag->name, 'Tag is valid.');
7171
}
7272

7373
/**

0 commit comments

Comments
 (0)