Skip to content

Commit 3a1e1f0

Browse files
authored
Add Bearer Token Authorization & customHeaders docs (rgstephens#43)
* Cleanup & Docs * scorecard changes * update versions
1 parent 1b612a4 commit 3a1e1f0

18 files changed

+300
-473
lines changed

README.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,16 @@
33
[![Platform](https://img.shields.io/badge/platform-Node--RED-red)](https://nodered.org)
44
![Release](https://img.shields.io/npm/v/node-red-contrib-graphql.svg)
55
![NPM](https://img.shields.io/npm/dm/node-red-contrib-graphql.svg)
6+
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/6cbeb40ab5604b3ab99e6badc9469e8a)](https://www.codacy.com/gh/rgstephens/node-red-contrib-graphql?utm_source=github.com&utm_medium=referral&utm_content=rgstephens/node-red-contrib-graphql&utm_campaign=Badge_Grade)
67

78
A NodeRed node to execute GraphQL Queries.
89

9-
## Install
10-
11-
Run command on Node-RED installation directory.
12-
13-
```
14-
npm install node-red-contrib-graphql
15-
```
16-
1710
## Change Log
1811

1912
| Vers | Changes |
2013
| ----- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
21-
| 2.0.1 | Update dependencies (`axios` & `mustache`), fix node-red scorecard issues |
14+
| 2.1.0 | Bearer Token Authentication |
15+
| 2.0.1 | Update dependencies (`axios` & `mustache`), fix node-red scorecard issues |
2216
| 2.0.0 | GraphQL response is now on `payload.graphql` instead of replacing `payload`. This is a breaking change. Addresses #32 |
2317
| 1.4.1 | Bump `follow-redirects` to 1.14.8 |
2418
| 1.4.0 | improve debug, bump `follow-redirects` |
@@ -30,15 +24,15 @@ npm install node-red-contrib-graphql
3024

3125
## GraphQL Nodes
3226

33-
Provides a `GraphQL` node to support queries and a supporting Configuration node, called `graphql-server` to point to a GraphQL server.
27+
Provides a `GraphQL` node to support queries and a configuration node called `graphql-server`.
3428

3529
### `graphql-server` Configuration Node Fields
3630

37-
| Name | Use |
38-
| ------------- | ------------------- |
39-
| Name | Node Name |
40-
| Endpoint | URL to the endpoint |
41-
| Authorization | Header |
31+
| Name | Use |
32+
| -------- | ------------------- |
33+
| Name | Node Name |
34+
| Endpoint | URL to the endpoint |
35+
| Token | Bearer Token |
4236

4337
### `graphql` Function Node Fields
4438

@@ -48,46 +42,52 @@ Provides a `GraphQL` node to support queries and a supporting Configuration node
4842
| Endpoint | Configuration Node Name |
4943
| Query | Query or Mutation template |
5044
| Syntax | Mustache / plain |
45+
| Token | Bearer Token |
5146
| Show Debug | Enable debug |
5247

53-
## Installing and using the Example Flow
48+
## Countries API Example
5449

55-
This example flow uses the `node-red-contrib-graphql` node to query the Deutsche Bahn GraphQL service and get a station address and details on the next departure.
50+
This example flow uses the `node-red-contrib-graphql` node to query the [Countries API](https://github.com/trevorblades/countries) built by GitHub user [Trevor Blades](https://github.com/trevorblades).
5651

57-
The example flow is in the file `countries.json`. Import this file from the clipboard under the NodeRed menu `Import > Clipboard`. You'll drag the example flow onto NodeRed.
52+
The example flow is in the file `examples/countries.json`. Import this file from the clipboard under the NodeRed menu `Import > Clipboard`. You'll drag the example flow onto NodeRed.
5853

59-
![Example Flow](flow.png)
54+
![Example Flow](images/flow.png)
55+
56+
### Edit graphql node
57+
58+
The GraphQL endpoint for is `https://countries.trevorblades.com/`. You can try it out [here](https://countries.trevorblades.com/). Here's the `graphql-node`:
59+
60+
![Edit GraphQL Node](images/editGraphQL.png)
61+
62+
### GraphQL Output
6063

6164
This is the result sent to the debug window.
6265

63-
![Example Flow Output](flowOutput.png)
66+
![Example Flow Output](images/flowOutput.png)
6467

65-
### Countries API
68+
## Authentication Example
6669

67-
Here is the example using the [Countries API](https://github.com/trevorblades/countries) built by GitHub user [Trevor Blades](https://github.com/trevorblades), who used [Countries List](https://annexare.github.io/Countries/) as a source of data.
70+
A bearer token can be provided for authentication. This is an example using the GitHub GraphQL API which is documented [here](https://docs.github.com/en/graphql/guides/forming-calls-with-graphql#communicating-with-graphql).
6871

69-
The GraphQL endpoint for this API is `https://countries.trevorblades.com/`. You can try it out [here](https://countries.trevorblades.com/).
72+
![Github Flow](images/githubFlow.png)
7073

71-
```json
72-
# Get information on Germany and it's states
73-
{
74-
country(code: "DE") {
75-
name
76-
native
77-
capital
78-
currency
79-
phone
80-
states {
81-
code
82-
name
83-
}
84-
}
85-
}
86-
```
74+
### Config Node Token
75+
76+
If you have an token with a long life, you can provide the token in the `graphql-server` configuration node. For GitHub, user your GitHub PAT.
77+
78+
![Github Config](images/githubGraphqlConfig.png)
79+
80+
You can also provide the token in the `graphql` node. This is useful if a prior node performs the authentication and returns the token to be used for a limited session.
81+
82+
![GitHub GraphQL](images/githubGraphql.png)
83+
84+
## Custom Headers
85+
86+
You can provide custom headers to the GraphQL node by attaching a `customHeaders` key to the `msg` and passing that to the GraphQL node. Here's an example that sets the `content-type` and a bearer token.
8787

88-
![Edit GraphQL Node](editGraphQL.png)
88+
![Custom Headers](images/customHeaders.png)
8989

90-
## Template flavors and uses
90+
## Templates and Variable Use
9191

9292
There are two template flavors:
9393

editGraphQL.png

-110 KB
Binary file not shown.

editGraphQLserver.png

-61.6 KB
Binary file not shown.

examples/countriesFlow.json

Lines changed: 110 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,110 @@
1-
[{"id":"a12eeec2ef7081f3","type":"graphql","z":"e596e2e15cffe546","name":"Get Country","graphql":"5ec8b7409ad68ba9","format":"json","syntax":"mustache","template":"{\n country(code: \"DE\") {\n name\n native\n capital\n currency\n phone\n states {\n code\n name\n }\n }\n}","showDebug":false,"x":290,"y":80,"wires":[["b1909e0fe42ed002","b4a00ddbf587d5db"],["b4a00ddbf587d5db"]]},{"id":"8ce305107f24099c","type":"inject","z":"e596e2e15cffe546","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":100,"y":80,"wires":[["a12eeec2ef7081f3"]]},{"id":"b4a00ddbf587d5db","type":"debug","z":"e596e2e15cffe546","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":100,"wires":[]},{"id":"b1909e0fe42ed002","type":"template","z":"e596e2e15cffe546","name":"Country Details","field":"payload","fieldType":"msg","format":"handlebars","syntax":"mustache","template":"Country Name - {{payload.graphql.country.name}} / {{payload.country.native}}\nCapital: {{payload.graphql.country.capital}}\nCurrency: {{payload.graphql.country.currency}}\nStates:\n {{payload.graphql.country.states.0.name}}\n {{payload.graphql.country.states.1.name}}\n {{payload.graphql.country.states.2.name}}\n {{payload.graphql.country.states.3.name}}\n","output":"str","x":520,"y":60,"wires":[["080fbc2ad4668f85"]]},{"id":"080fbc2ad4668f85","type":"debug","z":"e596e2e15cffe546","name":"","active":true,"tosidebar":true,"console":false,"complete":"false","statusVal":"","statusType":"auto","x":730,"y":60,"wires":[]},{"id":"5ec8b7409ad68ba9","type":"graphql-server","name":"Countries","endpoint":"https://countries.trevorblades.com"}]
1+
[
2+
{
3+
"id": "a12eeec2ef7081f3",
4+
"type": "graphql",
5+
"z": "e596e2e15cffe546",
6+
"name": "Get Country",
7+
"graphql": "5ec8b7409ad68ba9",
8+
"format": "json",
9+
"syntax": "mustache",
10+
"template": "{\n country(code: \"DE\") {\n name\n native\n capital\n currency\n phone\n states {\n code\n name\n }\n }\n}",
11+
"showDebug": false,
12+
"x": 290,
13+
"y": 80,
14+
"wires": [
15+
[
16+
"b1909e0fe42ed002",
17+
"b4a00ddbf587d5db"
18+
],
19+
[
20+
"b4a00ddbf587d5db"
21+
]
22+
]
23+
},
24+
{
25+
"id": "8ce305107f24099c",
26+
"type": "inject",
27+
"z": "e596e2e15cffe546",
28+
"name": "",
29+
"props": [
30+
{
31+
"p": "payload"
32+
},
33+
{
34+
"p": "topic",
35+
"vt": "str"
36+
}
37+
],
38+
"repeat": "",
39+
"crontab": "",
40+
"once": false,
41+
"onceDelay": 0.1,
42+
"topic": "",
43+
"payload": "",
44+
"payloadType": "date",
45+
"x": 100,
46+
"y": 80,
47+
"wires": [
48+
[
49+
"a12eeec2ef7081f3"
50+
]
51+
]
52+
},
53+
{
54+
"id": "b4a00ddbf587d5db",
55+
"type": "debug",
56+
"z": "e596e2e15cffe546",
57+
"name": "",
58+
"active": true,
59+
"tosidebar": true,
60+
"console": false,
61+
"tostatus": false,
62+
"complete": "true",
63+
"targetType": "full",
64+
"statusVal": "",
65+
"statusType": "auto",
66+
"x": 490,
67+
"y": 100,
68+
"wires": []
69+
},
70+
{
71+
"id": "b1909e0fe42ed002",
72+
"type": "template",
73+
"z": "e596e2e15cffe546",
74+
"name": "Country Details",
75+
"field": "payload",
76+
"fieldType": "msg",
77+
"format": "handlebars",
78+
"syntax": "mustache",
79+
"template": "Country Name - {{payload.graphql.country.name}} / {{payload.graphql.country.native}}\nCapital: {{payload.graphql.country.capital}}\nCurrency: {{payload.graphql.country.currency}}\nStates:\n {{payload.graphql.country.states.0.name}}\n {{payload.graphql.country.states.1.name}}\n {{payload.graphql.country.states.2.name}}\n {{payload.graphql.country.states.3.name}}\n",
80+
"output": "str",
81+
"x": 520,
82+
"y": 60,
83+
"wires": [
84+
[
85+
"080fbc2ad4668f85"
86+
]
87+
]
88+
},
89+
{
90+
"id": "080fbc2ad4668f85",
91+
"type": "debug",
92+
"z": "e596e2e15cffe546",
93+
"name": "",
94+
"active": true,
95+
"tosidebar": true,
96+
"console": false,
97+
"complete": "false",
98+
"statusVal": "",
99+
"statusType": "auto",
100+
"x": 730,
101+
"y": 60,
102+
"wires": []
103+
},
104+
{
105+
"id": "5ec8b7409ad68ba9",
106+
"type": "graphql-server",
107+
"name": "Countries",
108+
"endpoint": "https://countries.trevorblades.com"
109+
}
110+
]

examples/deutscheBahnFlow.json

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

0 commit comments

Comments
 (0)