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#


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#

import { buildSchema } from '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#

import express from "express"import { graphqlHTTP } from 'express-graphql';import { verifySession } from "supertokens-node/recipe/session/framework/express";import { buildSchema } from 'graphql';
var GraphQLSchema = buildSchema(`...`); // Refer to step cvar resolvers = {/* ... */ }; // Refer to step c// ...
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: any, 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