docs: update subscriptions use cases + securing action handler section

GITHUB_PR_NUMBER: 7945
GITHUB_PR_URL: https://github.com/hasura/graphql-engine/pull/7945

PR-URL: https://github.com/hasura/graphql-engine-mono/pull/3162
Co-authored-by: Divya Bhushan <11659160+divyabhushan@users.noreply.github.com>
Co-authored-by: Rikin Kachhia <54616969+rikinsk@users.noreply.github.com>
GitOrigin-RevId: de67d188920e7de06575409e9455af488ce49362
This commit is contained in:
hasura-bot 2022-01-03 19:22:19 +05:30
parent 6fdd433d5b
commit bb2b1e2a45
2 changed files with 70 additions and 49 deletions

View File

@ -288,7 +288,7 @@ First, load the action secret as an environment variable in your action handler
Second, you need to write some code in your action handler to check that the action secret
passed as a header equals to the one you stored as an environment variable.
The following is an example of a simple authorization middleware with Express:
The following is an example of a simple authorization middleware with Express which can be included before the request handler logic:
.. code-block:: javascript
@ -305,8 +305,13 @@ The following is an example of a simple authorization middleware with Express:
function correctSecretProvided(req) {
const requiredSecret = process.env.ACTION_SECRET_ENV;
const providedSecret = req.headers['ACTION_SECRET'];
return requiredSecret == providedSecret;
return requiredSecret === providedSecret;
}
// Request handler
app.post('/actionHandler', async (req, res) => {
// handler logic
});
.. admonition:: Additional Resources

View File

@ -30,7 +30,7 @@ Example: Live location tracking
Use subscriptions to show the current location of a vehicle on a map.
Let's say we have the following database schema:
Let's say we have the following database schema:
.. code-block:: sql
@ -42,7 +42,7 @@ Let's say we have the following database schema:
vehicle_location (
id INT PRIMARY KEY,
location TEXT,
timestamp TIMESTAMP,
time_stamp TIMESTAMP with time zone DEFAULT now(),
/* used to create relationship 'locations' for vehicle */
vehicle_id INT FOREIGN KEY REFERENCES vehicle(id)
)
@ -57,9 +57,9 @@ Now we can use the following subscription to fetch the latest location of a vehi
vehicle(where: {id: {_eq: $vehicleId}}) {
id
vehicle_number
locations(order_by: {timestamp: desc}, limit: 1) {
locations(order_by: {time_stamp: desc}, limit: 1) {
location
timestamp
time_stamp
}
}
}
@ -73,17 +73,17 @@ Now we can use the following subscription to fetch the latest location of a vehi
"locations": [
{
"location": "(12.93623,77.61701)",
"timestamp": "2018-09-05T06:52:44.383588+00:00"
"time_stamp": "2018-09-05T06:52:44.383588+00:00"
}
]
}
]
}
}
Check this `sample app <https://realtime-location-tracking.demo.hasura.app/>`__ for a working demo
(`source code <https://github.com/hasura/graphql-engine/tree/master/community/sample-apps/realtime-location-tracking>`__).
:variables:
{
"vehicleId": 3
}
.. _subscribe_table:
@ -109,9 +109,9 @@ Let's say we have the following database schema:
message (
id INT PRIMARY KEY,
text TEXT,
timestamp TIMESTAMP,
/* used to create relationship 'author' for message */
texts TEXT,
time_stamp TIMESTAMP default now(),
/* used to create relationship 'author' with the 'user' table */
user_id INT FOREIGN KEY REFERENCES user(id)
)
@ -121,9 +121,9 @@ Now we can use the following subscription to display the latest messages in a ch
:view_only:
:query:
subscription getMessages {
message(order_by: {timestamp: desc}) {
text
timestamp
message(order_by: {time_stamp: desc}) {
texts
time_stamp
author {
username
}
@ -134,22 +134,22 @@ Now we can use the following subscription to display the latest messages in a ch
"data": {
"message": [
{
"text": "I am fine.",
"timestamp": "2018-09-05T10:52:23.522223+00:00",
"texts": "I am fine, and you?",
"time_stamp": "2021-11-29T07:42:56.689135",
"author": {
"username": "Jane"
}
},
{
"text": "Hi! How are you?",
"timestamp": "2018-09-05T10:52:04.75283+00:00",
"texts": "Hi! How are you?",
"time_stamp": "2021-11-29T07:42:19.506049",
"author": {
"username": "Jose"
"username": "Musk"
},
},
{
"text": "Hi!",
"timestamp": "2018-09-05T10:51:43.622839+00:00",
"texts": "Hi!",
"time_stamp": "2021-11-29T07:38:52.347136",
"author": {
"username": "Jane"
}
@ -158,9 +158,6 @@ Now we can use the following subscription to display the latest messages in a ch
}
}
Check this `sample app <https://realtime-chat.demo.hasura.app/>`__ for a working demo
(`source code <https://github.com/hasura/graphql-engine/tree/master/community/sample-apps/realtime-chat>`__).
.. _subscribe_derived:
Subscribe to the latest value of some derived data
@ -187,7 +184,7 @@ Let's say we have the following database schema:
option (
id INT PRIMARY KEY
poll_id INT FOREIGN KEY REFERENCES poll(id)
text TEXT
texts TEXT
)
user (
@ -199,7 +196,7 @@ Let's say we have the following database schema:
id INT PRIMARY KEY,
option_id INT FOREIGN KEY REFERENCES option(id),
user_id INT FOREIGN KEY REFERENCES user(id),
timestamp TIMESTAMP
time_stamp TIMESTAMP
)
First, create a view ``poll_results`` to give the result of the poll:
@ -214,7 +211,7 @@ First, create a view ``poll_results`` to give the result of the poll:
(
SELECT vote.option_id,
option.poll_id,
option.text
option.texts
FROM (
vote
LEFT JOIN option ON ((option.id = vote.option_id))
@ -241,8 +238,11 @@ Now we can use the following subscription to display the latest poll result:
where: { poll_id: {_eq: $pollId} }
) {
poll_id
poll {
question
}
option {
text
texts
}
votes
}
@ -253,42 +253,58 @@ Now we can use the following subscription to display the latest poll result:
"poll_results": [
{
"poll_id": 1,
"votes": 1,
"poll":{
"question": "What's your favourite food?"
},
"option": {
"text": "Pizza"
}
"texts": "Pizza"
},
"votes": 1
},
{
"poll_id": 1,
"votes": 1,
"poll":{
"question": "What's your favourite food?"
},
"option": {
"text": "Salad"
}
"texts": "Salad"
},
"votes": 1
},
{
"poll_id": 1,
"votes": 2,
"poll":{
"question": "What's your favourite food?"
},
"option": {
"text": "Sandwich"
}
"texts": "Sandwich"
},
"votes": 2
},
{
"poll_id": 1,
"votes": 3,
"poll":{
"question": "What's your favourite food?"
},
"option": {
"text": "Burger"
}
"texts": "Burger"
},
"votes": 3
},
{
"poll_id": 1,
"votes": 1,
"poll":{
"question": "What's your favourite food?"
},
"option": {
"text": "Lasagna"
}
"texts": "Lasagna"
},
"votes": 1
}
]
}
}
Check this `sample app <https://realtime-poll.demo.hasura.app/>`__ for a working demo
(`source code <https://github.com/hasura/graphql-engine/tree/master/community/sample-apps/realtime-poll>`__).
:variables:
{
"pollId": 1
}