You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: doc/producers/custom.md
+18-7Lines changed: 18 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,9 @@ Custom data producers allow you essentially hook into any data of Drupal, becaus
6
6
7
7
Lets look at a custom Data producer that loads the current user (similar to the 3.x version of currentUser query).
8
8
9
-
The first step as seen before is to add our query to the schema :
9
+
The first step as seen before is to add our query to the schema :
10
10
11
-
```
11
+
```
12
12
type Query {
13
13
...
14
14
currentUser: User
@@ -21,7 +21,7 @@ type User {
21
21
}
22
22
```
23
23
24
-
Now that we have this we need to make a resolver that actually loads this user, but for that first we need our own custom data producer "CurrentUser" :
24
+
Now that we have this we need to make a resolver that actually loads this user, but for that first we need our own custom data producer "CurrentUser" :
25
25
26
26
```php
27
27
<?php
@@ -99,7 +99,7 @@ class CurrentUser extends DataProducerPluginBase implements ContainerFactoryPlug
99
99
100
100
We are defining a custom data producer `current_user` that we can now use to resolve our query that we previously added to the schema. Notice that our data producer returns only the user id and not the actual user object. However we can combine it with an entity_load which is already made very efficient with in the module (taking advantage of caching strategies using buffering) so we don't have to actually load the user here.
101
101
102
-
Lets see how we can consume our newly created data producer :
102
+
Lets see how we can consume our newly created data producer :
Notice how we combine our custom data producer with a built-in `entity_load` to make querying more performance and standardized across. We will look at `compose` in more detail in the next section.
114
114
115
-
In the end when we do a query like this :
115
+
In the end when we do a query like this :
116
116
117
117
```graphql
118
118
{
@@ -123,7 +123,7 @@ In the end when we do a query like this :
123
123
}
124
124
```
125
125
126
-
we get a result like this :
126
+
we get a result like this :
127
127
128
128
```json
129
129
{
@@ -136,4 +136,15 @@ we get a result like this :
136
136
}
137
137
```
138
138
139
-
(For this to actually work we would need to add resolvers to the User object to resolve the `id` and `name` properties).
139
+
For this to actually work we would need to add resolvers to the User object to resolve the `id` and `name` properties like so:
0 commit comments