import express from "express"
import { buildSchema } from 'graphql';
import { graphqlHTTP } from 'express-graphql';
import { verifySession } from "supertokens-node/recipe/session/framework/express";
import { SessionRequest } from "supertokens-node/framework/express";
let app = express();
var GraphQLSchema = buildSchema(`
  type Mutation {
      signOut: String
  }
`);
function getResolvers(req: any, res: any) {
    return {
        signOut: async (_: any, context: any) => {
            return new Promise(async (resolve, reject) => {
                try {
                    if (context.session !== undefined) {
                        await context.session.revokeSession();
                        resolve("Logged out");
                    } else {
                        resolve("Session does not exist");
                    }
                } catch (e) {
                    reject(e);
                }
            });
        },
    };
}
app.use("/graphQL", verifySession({ sessionRequired: false }), graphqlHTTP(async (req, res) => {
    return {
        schema: GraphQLSchema,
        rootValue: getResolvers(req, res),
        context: {
            session: (req as SessionRequest).session,
        }
    };
}))