To search job vacancies in the official NHS website can sometimes be some kind of a hassle. This small project proposes a different user interface so it is easier to search NHS postings, while waiting for the new official website due to be online by the end of October 2022.
This project is above all a means to run through some of the abilities the Nhost stack is offering to developers.
The official NHS website does not expose a standard API. As a result, it is not possible to make direct search calls to fetch a JSON payload and render it in the frontend. In this project, the official HTML website is entirely scraped once with Cheerio into the database. Then, three times a day, vacancies from the past two days are scraped and updated into the database.
In this project, we connect a Hasura cron trigger with a Nhost Serverless function.
To do so, we need:
- to define a
fetch
serverless function - to configure the cron job trigger with the right path:
{{NHOST_BACKEND_URL}}/v1/functions/fetch
- To secure the function call in sending the Nhost webhook secret in the cron job's headers
The scrapping of the initial HTML website takes time and causes a function timeout. To prevent this, the fetch
function scraps only a single result page from the original website, then inserts a new row in the fetch_request
table with the number of the next page to scrap.
We then define a Hasura event trigger on the fetch_request
table that calls the fetch
function again for the next page, and so on, until no result is available anymore.
A common pattern in Nhost functions is to connect to the database to get some context about the current processing. As we secured the function call with the Nhost webhook secret, it is then safe to connect through GraphQL with the x-hasura-admin-secret
attached as a header. This secret is available in Nhost functions as the NHOST_ADMIN_SECRET environment variable.
VS Code supports GraphQL through this nice extension. To activate if for your schema, you need to create a graphql.config.yml
file in the root directory and declare a schema that points to the Nhost local project (when using the CLI) or the Hasura endpoint of your cloud project. In this repository, we are using the CLI to develop locally.
If no headers are defined, the backend will assume the schema introspection is made from a public
user. You need to set the x-hasura-admin-secret
header to get access to the full introspection (defaults to nhost-admin-secret
when using the Nhost CLI).
graphql-request is a simple and effective GraphQL client that can make GraphQL call much simpler in serverless functions.
This repository is configured to generate code for graphql-request
operations using the GraphQL Code Generator:
- Follow the graphql-request GraphQL Codegen plugin installation instructions
- Configure the generator in
graphql.config.yml
- Define GraphQL operations in the
functions
directory, for instance an upsert mutation. GraphQL files are prefixed with_
so we are sure there won't be any attempt to deploy them as a function endpoint. - See the generated sdk
- Use the operation in a function
In a similar way, we can generate GraphQL operations for Vue Apollo. It works fine with @nhost/vue
and @nhost/apollo
:
- Configure the generator in
graphql.config.yml
. Note that we added ax-hasura-role
header set touser
to the schema will be generated given the permissions of theusers
role. - Define GraphQL operations in the
src/graphql
directory, for instancevacanciesTable
- See the generated composable
- Use the
useVacanciesTableQuery
in a Vue composant
To use the Nhost JS SDK in a client-side rendered Quasar application is quite straight forward.
As Quasar is based on Vue, we are using the standard @nhost/vue library, in addition to @nhost/apollo.
Please have a look at the the Vue main file and the Vite configuration file.
Quite easy in using Hasura's limit, offset, where and order_by clauses as GraphQL variables in a Vue Apollo composable - see source.
Small composable to start the application in dark/light mode according to user's preferences, and to persist its change to localStorage
.
It is also a bit tricky to set dark/light mode to sticky headers in Quasar's QTable
component as it is based on CSS' position: sticky
. Here's how sass can be configured to make it work.