var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { jwt } from '../jwt.js';
import { generateApolloClient } from '@deep-foundation/hasura/client.js';
import gql from 'graphql-tag';
import { ApolloServer } from 'apollo-server-express';
import { DeepClient } from '../client.js';
import { ApolloServerPluginDrainHttpServer, ApolloServerPluginLandingPageGraphQLPlayground } from 'apollo-server-core';
const JWT_SECRET = process.env.JWT_SECRET;
const jwt_secret = JSON.parse(JWT_SECRET);
export const typeDefsString = `
  type Query {
    guest: GuestOutput
  }
  type GuestOutput {
    token: String
    linkId: Int
  }
`;
export const typeDefs = gql `${typeDefsString}`;
const client = generateApolloClient({
    path: `${process.env.DEEPLINKS_HASURA_PATH}/v1/graphql`,
    ssl: !!+process.env.DEEPLINKS_HASURA_SSL,
    secret: process.env.DEEPLINKS_HASURA_SECRET,
}, { ApolloClient: { defaultOptions: { query: { fetchPolicy: 'no-cache' }, watchQuery: { fetchPolicy: 'no-cache' } } } });
const deep = new DeepClient({
    apolloClient: client,
});
const resolvers = {
    Query: {
        guest: (source, args, context, info) => __awaiter(void 0, void 0, void 0, function* () {
            const { data: [{ id }] } = yield deep.insert({
                type_id: deep.idLocal('@deep-foundation/core', 'User'),
                out: { data: [
                        {
                            type_id: deep.idLocal('@deep-foundation/core', 'Join'),
                            to_id: yield deep.id('deep', 'users')
                        },
                    ] },
            });
            yield deep.insert({
                type_id: deep.idLocal('@deep-foundation/core', 'Rule'),
                out: { data: [
                        {
                            type_id: deep.idLocal('@deep-foundation/core', 'RuleSubject'),
                            to: { data: {
                                    type_id: deep.idLocal('@deep-foundation/core', 'Selector'),
                                    out: { data: {
                                            type_id: deep.idLocal('@deep-foundation/core', 'SelectorInclude'),
                                            to_id: id,
                                            out: { data: {
                                                    type_id: deep.idLocal('@deep-foundation/core', 'SelectorTree'),
                                                    to_id: deep.idLocal('@deep-foundation/core', 'joinTree'),
                                                } },
                                        } }
                                } }
                        },
                        {
                            type_id: deep.idLocal('@deep-foundation/core', 'RuleObject'),
                            to: { data: {
                                    type_id: deep.idLocal('@deep-foundation/core', 'Selector'),
                                    out: { data: {
                                            type_id: deep.idLocal('@deep-foundation/core', 'SelectorInclude'),
                                            to_id: id,
                                            out: { data: {
                                                    type_id: deep.idLocal('@deep-foundation/core', 'SelectorTree'),
                                                    to_id: deep.idLocal('@deep-foundation/core', 'containTree'),
                                                } },
                                        } }
                                } }
                        },
                        {
                            type_id: deep.idLocal('@deep-foundation/core', 'RuleAction'),
                            to: { data: {
                                    type_id: deep.idLocal('@deep-foundation/core', 'Selector'),
                                    out: { data: {
                                            type_id: deep.idLocal('@deep-foundation/core', 'SelectorInclude'),
                                            to_id: deep.idLocal('@deep-foundation/core', 'AllowSelect'),
                                            out: { data: {
                                                    type_id: deep.idLocal('@deep-foundation/core', 'SelectorTree'),
                                                    to_id: deep.idLocal('@deep-foundation/core', 'containTree'),
                                                } },
                                        } }
                                } }
                        },
                    ] },
            });
            const token = jwt({
                secret: jwt_secret.key,
                linkId: id,
            });
            return { token, linkId: id };
        }),
    }
};
const context = ({ req }) => {
    return { headers: req.headers };
};
const generateApolloServer = (httpServer) => {
    return new ApolloServer({
        introspection: true,
        typeDefs,
        resolvers,
        context,
        plugins: [
            ApolloServerPluginDrainHttpServer({ httpServer }),
            ApolloServerPluginLandingPageGraphQLPlayground()
        ]
    });
};
export default generateApolloServer;
//# sourceMappingURL=guest.js.map