Backend Setup
#
a) Complete Quick SetupComplete both steps from the quick setup guide for SuperTokens:
#
b) Install GraphQL dependencies on your backendinfo
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 schemaimport { buildSchema } from 'graphql';
var schema = buildSchema(`
type Query {
hello: String
}
`);
#
Create resolversvar resolvers = {
hello: () => "Hello World!",
}
Refer to the official guide to know more.
#
d) Add a GraphQL endpoint with optional session verificationimport 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 c
var 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