UNPKG

uzen

Version:

General-purpose GraphQL subscription server library

61 lines (60 loc) 2.29 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.createGraphQLServer = createGraphQLServer; const schema_1 = require("@graphql-tools/schema"); const load_1 = require("@graphql-tools/load"); const graphql_file_loader_1 = require("@graphql-tools/graphql-file-loader"); const graphql_yoga_1 = require("graphql-yoga"); const graphql_scalars_1 = require("graphql-scalars"); const path_1 = require("path"); const model_notify_resolver_1 = __importDefault(require("./modules/subscription/model-notify.resolver")); const scalars_1 = require("./scalars"); function createGraphQLServer(config) { const typeDefs = (0, load_1.loadSchemaSync)((0, path_1.join)(__dirname, '**/*.gql'), { loaders: [new graphql_file_loader_1.GraphQLFileLoader()] }); const mergeResolvers = (...resolverArrays) => { return resolverArrays.reduce((acc, resolvers) => { Object.keys(resolvers).forEach((type) => { if (!acc[type]) { acc[type] = {}; } Object.assign(acc[type], resolvers[type]); }); return acc; }, {}); }; const resolvers = mergeResolvers(model_notify_resolver_1.default); const schema = (0, schema_1.makeExecutableSchema)({ typeDefs, resolvers: { ...resolvers, JSONString: scalars_1.JSONString, JSON: graphql_scalars_1.GraphQLJSON, JSONOb: graphql_scalars_1.GraphQLJSONObject, DateTime: graphql_scalars_1.GraphQLDateTime, }, }); const yoga = (0, graphql_yoga_1.createYoga)({ cors: config.cors, graphqlEndpoint: `/${config.endpoint || 'uzen'}`, schema, logging: config.logging, graphiql: config.graphiql || false, context: { pubsub: config.pubsub, }, landingPage: ({ fetchAPI }) => { return new fetchAPI.Response(config.landingPage, { status: 404, headers: { 'Content-Type': 'text/html' } }); }, }); return yoga; }