Skip to main content

Backend Setup

a) Complete Quick Setup#

Complete both steps from the quick setup guide for SuperTokens:

b) Install GraphQL dependencies on your backend#

info

We use express-graphql as an example, SuperTokens can be used with other GraphQL libraries.

npm i express-graphql graphql

c) Setup GraphQL#

Create a schema#

var { buildSchema } = require('graphql');
var schema = buildSchema(`  type Query {    hello: String  }`);

Create resolvers#

var resolvers = {    hello: () => "Hello World!",}

Refer to the official guide to know more.

d) Add a GraphQL endpoint with optional session verification#

let express = require("express")let { graphqlHTTP } = require('express-graphql');let { verifySession } = require("supertokens-node/recipe/session/framework/express");
...
let app = express();
/** The verifySession middleware adds the session object to req* { sessionRequired: false } allows access to the endpoint even * when a session does not exist*/app.use("/graphQL", verifySession({sessionRequired: false}), graphqlHTTP(async (req, res) => {    return {        schema: GraphQLSchema,        rootValue: resolvers,        // req.session will be undefined if a session does not exist        context: {            session: req.session,        }    };}))

Refer to this page to know more about the session object