Skip to content

Commit 3417fb6

Browse files
committed
Lazy register fields of field_item_image, field_item_file
1 parent a10387a commit 3417fb6

File tree

3 files changed

+48
-41
lines changed

3 files changed

+48
-41
lines changed

graphql_api.graphql.inc

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -32,47 +32,45 @@ function graphql_api_graphql_api_info() {
3232
]),
3333
'field_item_file' => new ObjectType([
3434
'name' => 'field_item_file',
35-
'fields' => [
36-
'file' => [
37-
'type' => Type::string(),
38-
'description' => t('File')
39-
],
40-
'description' => [
41-
'type' => Type::string(),
42-
'description' => t('Alt')
43-
],
44-
'display' => [
45-
'type' => Type::string(),
46-
'description' => t('Display')
47-
]
48-
]
35+
'fields' => function () {
36+
$schema = graphql_api();
37+
return $schema->getInterfaceType('file')->getFields() + [
38+
'description' => [
39+
'type' => Type::string(),
40+
'description' => t('Alt')
41+
],
42+
'display' => [
43+
'type' => Type::string(),
44+
'description' => t('Display')
45+
]
46+
];
47+
}
4948
]),
5049
'field_item_image' => new ObjectType([
5150
'name' => 'field_item_image',
52-
'fields' => [
53-
'file' => [
54-
'type' => Type::string(),
55-
'description' => t('File')
56-
],
57-
'description' => [
58-
'type' => Type::string(),
59-
'description' => t('Alt')
60-
],
61-
'display' => [
62-
'type' => Type::string(),
63-
'description' => t('Display')
64-
],
65-
'alt' => [
66-
'type' => Type::string(),
67-
'description' => t('Alt')
68-
],
69-
'title' => [
70-
'type' => Type::string(),
71-
'description' => t('Title')
72-
]
73-
]
51+
'fields' => function () {
52+
$schema = graphql_api();
53+
return $schema->getInterfaceType('file')->getFields() + [
54+
'description' => [
55+
'type' => Type::string(),
56+
'description' => t('Alt')
57+
],
58+
'display' => [
59+
'type' => Type::string(),
60+
'description' => t('Display')
61+
],
62+
'alt' => [
63+
'type' => Type::string(),
64+
'description' => t('Alt')
65+
],
66+
'title' => [
67+
'type' => Type::string(),
68+
'description' => t('Title')
69+
]
70+
];
71+
}
7472
]),
75-
'socialfield' => new ObjectType([
73+
'socialfield' => Type::listOf(new ObjectType([
7674
'name' => 'socialfield',
7775
'fields' => [
7876
'service' => [
@@ -88,7 +86,7 @@ function graphql_api_graphql_api_info() {
8886
'description' => t('Weight')
8987
]
9088
]
91-
]),
89+
])),
9290
'field_item_name' => new ObjectType([
9391
'name' => 'field_item_name',
9492
'fields' => [

graphql_api.module

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ function graphql_api_entity_get_query($entity_type, $conditions = []) {
121121
*/
122122
function graphql_api_query_file($file, $args = []) {
123123
$query_task = file_get_contents($file);
124-
$shema = new Schema();
124+
$shema = graphql_api();
125125
$schema_build = $shema->build();
126126
$data = GraphQL::execute(
127127
$schema_build,
@@ -140,4 +140,14 @@ if (!function_exists('dump')) {
140140
function dump() {
141141
return call_user_func_array('var_dump', func_get_args());
142142
}
143+
}
144+
145+
/**
146+
* Get Schema builder
147+
*/
148+
function graphql_api() {
149+
if (!$obj = &drupal_static(__FUNCTION__)) {
150+
$obj = new Schema;
151+
}
152+
return $obj;
143153
}

includes/graphql_api_page_callback.inc

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function graphql_api_page_callback() {
3636
$result = $cache->data;
3737
} else {
3838
// Define your schema:
39-
$schemaBuilder = new Schema;
39+
$schemaBuilder = graphql_api();
4040
$schema = $schemaBuilder->build();
4141

4242
$result = GraphQL::execute(
@@ -54,7 +54,6 @@ function graphql_api_page_callback() {
5454
$result['errors'] = array_unique($result['errors']);
5555
}
5656

57-
5857
if ($is_introspec) {
5958
cache_set('graphql_api_introspec', $result);
6059
}

0 commit comments

Comments
 (0)