Skip to content

Commit da4eb99

Browse files
joaogarinpmelab
authored andcommitted
docs(graphql): Update structure (#767)
* fix(docs): Fix structure for gitbook * update doc * rename yaml * update snippet types * remove duplicate intro * fix changes * put back upgrade
1 parent 414e5ed commit da4eb99

15 files changed

Lines changed: 129 additions & 112 deletions

File tree

.gitbook.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
root: ./doc/
File renamed without changes.

doc/README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,65 @@
1-
# Introduction
1+
# Drupal GraphQL
2+
3+
## Introduction
4+
5+
The GraphQL Drupal module lets you query or mutate \(update/delete\) any content or configuration using the official GraphQL query language. This is an extremely powerful tool which opens the door for Drupal to be used in a multitude of applications.
6+
7+
## Who is this module for?
8+
9+
Anyone who wants to get JSON data out of Drupal.
10+
11+
A few examples of where the GraphQL module could be used:
12+
13+
* Decoupled Drupal applications with a javascript front-end \(React, Angular, Ember, etc\),
14+
* Twig Templates \(Drupal theming\)
15+
* Mobile applications that need a persistent data store
16+
* IOT data storage
17+
18+
## Hello World \(Quick Start\)
19+
20+
1. Familiarize yourself with the GraphQL language. The official GraphQL docs are very well written.
21+
22+
[http://graphql.org/learn/](http://graphql.org/learn/)
23+
24+
2. Install the module and enable GraphQL, as well as GraphQL Core \(machine names are `graphql` and `graphql_core` respectively\).
25+
3. Login and navigate to `/graphql/explorer` \(Configuration > Web Services > GraphQL > Schemas > Explorer\)
26+
27+
This will bring you to the GraphiQL explorer.
28+
29+
4. **Read the comments** and then enter the following query in the left pane:
30+
31+
```graphql
32+
query {
33+
user: currentUserContext{
34+
...on User {
35+
name
36+
}
37+
}
38+
}
39+
```
40+
41+
5. Press `Ctrl-Space` and you should see something like the following display in the right pane:
42+
43+
```javascript
44+
{
45+
"data": {
46+
"user": {
47+
"name": "admin"
48+
}
49+
}
50+
```
51+
52+
6. Congrats! You just figured out how to execute your first GraphQL query. This query is displaying the current logged in user, you.
53+
54+
**NOTES:**
55+
56+
* The GraphiQL explorer, included with the module, is your friend, it’s amazing. You will most likely use the GraphiQL explorer to build and test more complicated queries.
57+
* GraphQL is introspective, meaning that the entire schema \(data model\) is known up front. This is important as it allows tools like GraphiQL to implement autocompletion.
58+
* You can use GraphiQL to explore your way through the data and configuration, once you know the basic GraphQL syntax. You can use the tab key in the explorer like you would with autocompletion or intellisense in modern IDEs.
59+
* The `... User` in the query above is a fragment which exposes all of the fields on the User entity to us. Inline fragments like this can be a very powerful way to explore the schema.
60+
61+
**Resources**
62+
63+
* [https://github.com/drupal-graphql/graphql](https://github.com/drupal-graphql/graphql)
64+
* [https://drupal.slack.com](https://drupal.slack.com) - The GraphQL Slack Channel is very active
265

doc/SUMMARY.md

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
# Table of contents
22

3-
* [Introduction](README.md)
4-
* [Introduction](...md)
5-
63
## Getting started
74

8-
* [Getting started](getting-started/getting-started.md)
5+
* [Getting started](README.md)
96

107
## Queries
118

12-
* [Introduction](queries/queries.md)
9+
* [Queries](queries/README.md)
1310
* [Querying nodes](queries/querying-nodes.md)
1411
* [Querying taxonomies](queries/querying-taxonomies.md)
1512
* [Querying routes](queries/routes.md)
@@ -19,16 +16,16 @@
1916

2017
## Mutations
2118

22-
* [Introduction](mutations/mutations.md)
19+
* [Mutations](mutations/README.md)
2320
* [Creating mutation plugins](mutations/creating-mutation-plugins.md)
2421
* [Writing custom mutations](mutations/custom-mutations.md)
2522
* [Writing custom validations](mutations/custom-validations.md)
2623

2724
## Authentication
2825

29-
* [Introduction](authentication/introduction.md)
26+
* [Authentication](authentication/README.md)
3027

3128
## Metatag / SEO
3229

33-
* [Metatag support](metatag-seo/metatag.md)
30+
* [Metatag](metatag-seo/README.md)
3431

doc/authentication/README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# Authentication
2+
3+
When it comes to authentication the Drupal GraphQL module is very much independent of what kind of authentication system or technique you use, as long as in the end you are able to send a token via the `Authorization` header.
4+
5+
Drupal has some modules for doing decoupled authentiation using tokens :
6+
7+
* [Simple oauth](https://www.drupal.org/project/simple_oauth)
8+
* [JWT](https://www.drupal.org/project/jwt)
9+
10+
## Bearer token
11+
12+
To authenticated a graphql request with Drupal you need to attach the token you get with those modules as a `Bearer` in the Authorization header. Here is how that can look like when doing a fetch call :
13+
14+
```javascript
15+
...
16+
fetch(url, {
17+
method: 'post',
18+
headers: {
19+
'Authorization': `Bearer ${token}`
20+
},
21+
body: '...'
22+
})
23+
.then(json)
24+
...
25+
```
26+
27+
Once a request goes with that token the user will be authenticated and Drupal's permission system and access checking will work using that user.
28+
29+
## Basic auth
30+
31+
Most recently Drupal's own Basic auth module support was also added so you can aslo use that to authenticated queries to drupal.

doc/authentication/introduction.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

doc/getting-started/getting-started.md

Lines changed: 0 additions & 65 deletions
This file was deleted.
Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Metatag support
1+
# Metatags
22

33
## Metatag
44

55
The easiest way to use the Metatag module together with GraphQL is to use the [GraphQL Metatag](https://www.drupal.org/project/graphql_metatag) module. It comes with built-in support for using metatags with GraphQL.
66

77
## Metatag Queries
88

9-
```text
9+
```graphql
1010
{
1111
nodeById(id: "1") {
1212
entityId
@@ -23,7 +23,7 @@ The easiest way to use the Metatag module together with GraphQL is to use the [G
2323

2424
This will give you something like this as a response :
2525

26-
```text
26+
```json
2727
{
2828
"data": {
2929
"nodeById": {
@@ -37,7 +37,7 @@ This will give you something like this as a response :
3737
"key": "canonical",
3838
"value": "https://websiteurl.com/node/1"
3939
}
40-
],
40+
]
4141
}
4242
}
4343
}
@@ -47,7 +47,7 @@ This way you can easily manipulate SEO information in the Node in Drupal but sti
4747

4848
You can of course use this in any kind of query that would return the entity that holds the metatag information. As an example querying a route and getting from that entity the metatag information would look something like this :
4949

50-
```text
50+
```graphql
5151
{
5252
route(path: "/article-name") {
5353
... on EntityCanonicalUrl {
@@ -65,7 +65,7 @@ You can of course use this in any kind of query that would return the entity tha
6565

6666
This would return any information on this particular route, including the meta information requested.
6767

68-
```text
68+
```json
6969
{
7070
"data": {
7171
"route": {
@@ -95,9 +95,9 @@ There is a currently an [issue](https://github.com/drupal-graphql/graphql/issues
9595

9696
_"If a module \(e.g. metatag\) introduces a new primitive data type, it is not part of the derived types, but any field using it will reference it. That results in a "Missing type metatag." exception."_
9797

98-
So for now you need to include a custom Scalar as a workaround to avoid errors in GraphQL due to this missing type. Create a file inside a custom module of your own, named for example "MetatagScalar.php" where a custom scalar will be defined. In this example the module's name is graphql\_custom as seen from the namespace bellow. Make sure to not conflict with existing namespaces when defining it.
98+
So for now you need to include a custom Scalar as a workaround to avoid errors in GraphQL due to this missing type. Create a file inside a custom module of your own, named for example "MetatagScalar.php" where a custom scalar will be defined. In this example the module's name is graphql_custom as seen from the namespace bellow. Make sure to not conflict with existing namespaces when defining it.
9999

100-
```text
100+
```php
101101
<?php
102102

103103
namespace Drupal\graphql_custom\Plugin\GraphQL\Scalars;
@@ -120,4 +120,3 @@ class MetatagScalar extends StringScalar {
120120

121121
}
122122
```
123-
Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Introduction
1+
# Mutations
22

33
In GraphQL, a mutation is the terminology used whenever you want to add, modify, or delete data stored on the server, in this case Drupal.
44

@@ -13,15 +13,15 @@ The corresponding example code for creating, deleting, updating, and fileuploads
1313
A simple mutation to add an article content type \(node entity / article bundle\) might look the following:
1414

1515
```graphql
16-
mutation{
17-
addArticle(input: {title: "Hey"}){
16+
mutation {
17+
addArticle(input: { title: "Hey" }) {
1818
errors
1919
violations {
2020
message
2121
code
2222
path
23-
},
24-
article: entity{
23+
}
24+
article: entity {
2525
... on NodeArticle {
2626
nid
2727
}
@@ -36,7 +36,7 @@ In the mutation above, we use the inline fragment `... on NodeArticle { nid } to
3636

3737
The result of the above mutation would look something like this:
3838

39-
```javascript
39+
```json
4040
{
4141
"data": {
4242
"addArticle": {
@@ -54,4 +54,3 @@ External Resources:
5454

5555
* [http://graphql.org/learn/queries/\#mutations](http://graphql.org/learn/queries/#mutations)
5656
* [https://www.amazeelabs.com/en/blog/extending-graphql-part-3-mutations](https://www.amazeelabs.com/en/blog/extending-graphql-part-3-mutations)
57-

doc/mutations/creating-mutation-plugins.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ The first step to create a mutation is to make the plugin. The graphql module pr
1818

1919
Let's look at what the code for this plugin looks like :
2020

21-
```text
21+
```php
2222
<?php
2323

2424
namespace Drupal\graphql_examples\Plugin\GraphQL\Mutations;
@@ -92,7 +92,7 @@ We can see we are assigning the `title` that we are passing in the input \(we wi
9292

9393
Let's continue with our article example. In this case we implement a mutation to update a given article. Because we are updating a particular entity and we need to know which entity it is, we will need to provide the plugin annotation with something extra, an Id for the entity.
9494

95-
```text
95+
```php
9696
<?php
9797

9898
namespace Drupal\graphql_examples\Plugin\GraphQL\Mutations;
@@ -143,7 +143,7 @@ The first thing we noticed is we are now using `UpdateEntityBase` instead of "Cr
143143

144144
The only thing left now is really to delete the entity right? This is the simplest type of operation out of the 3, because we only need to give graphql the `id`, it will check if we can access that type of operation and if so delete the entity with the id we give to it. So let's look at how the plugin looks like
145145

146-
```text
146+
```php
147147
<?php
148148

149149
namespace Drupal\graphql_examples\Plugin\GraphQL\Mutations;
@@ -174,7 +174,7 @@ We extend the `DeleteEntityBase` class and only require one argument: the id of
174174

175175
We know from the examples above that we need to define the arguments for mutations. Similar to how a function, mutations receive arguments that can be used to do whatever the mutation needs to do to work. In order for graphql to know information about the arguments we create an `InputType`. The ArticleInput that we used above looks like this :
176176

177-
```text
177+
```php
178178
<?php
179179

180180
namespace Drupal\graphql_examples\Plugin\GraphQL\InputTypes;
@@ -202,4 +202,3 @@ class ArticleInput extends InputTypePluginBase {
202202
We can see again that we namespace this plugin to our module name, in this case `graphql_examples` should be replaced by your own module name. This file should live inside `{{module_name}}/src/Plugin/GraphQL/InputTypes/ArticleInput.php`
203203

204204
We can also see above that we only use annotations here to define the arguments inside the `fields` property. So we know that it receives a `title` and that's a "String" and we also receive a `body` which is also a `String`.
205-

0 commit comments

Comments
 (0)