Skip to main content

Get Started with GraphQL

Create an Account and Database#

If you do not have a database already, follow these steps to create one.

Check the Query Browser#

In Upstash Console, click the GraphQL Explorer from the left sidebar. You will see the browser page. Select your database and select Redis Strings example. Click to the play/run button to execute the query. You will see the response as below:

You can check other examples and write your own queries in the browser.

Get the API URL and Access Keys#

Now, in the Query Browser, click to the Configure button at the top. You will see the page where the GraphQL API URL and access keys are shared as below. You can copy the url end point, access keys. You can download the schema. Also you can revoke the keys and disable the GraphQL API for this database completely. Note that, everthing you see in this page is specific to the selected database.

You can access the same page clicking GraphQL API button in your database details page.

Create a Basic Node.js Application#

Create a new NodeJS app with npm init and then edit index.js as below:

const fetch = require("node-fetch");
const query = `
query { redisGet(key : "key") }
`;
const url = "YOUR_API_URL_HERE";
const opts = {
method: "POST",
headers: {"Content-Type": "application/json",
Authorization: "Bearer YOUR_ACCESS_KEY_HERE"},
body: JSON.stringify({query})
};
fetch(url, opts)
.then(res => res.json())
.then(console.log)
.catch(console.error);

Now, run the app by node index.js. You will see the output like:

{ data: { redisGet: 'value' } }

You can use the above code in client side javascript removing require("node-fetch"). But be careful, your access key will be exposed publicly. Using a serverless function to call the GraphQL API can be a better solution.

The above approach should be enough for simple application. If you have more sophisticated needs then you may want to use GraphQL clients like Apollo or Relay.

Check here for clients and tools in other languages.