UNPKG

fuse

Version:

The magical GraphQL framework

135 lines (131 loc) 4.72 kB
"use strict"; var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); // src/next/index.ts var next_exports = {}; __export(next_exports, { createAPIRouteHandler: () => createAPIRouteHandler, createPagesRouteHandler: () => createPagesRouteHandler }); module.exports = __toCommonJS(next_exports); var import_fuse = require("fuse"); var import_graphql_yoga2 = require("graphql-yoga"); var import_promises = require("fs/promises"); var import_graphql = require("graphql"); // src/utils/yoga-helpers.ts var import_graphql_armor_block_field_suggestions = require("@escape.tech/graphql-armor-block-field-suggestions"); var import_plugin_defer_stream = require("@graphql-yoga/plugin-defer-stream"); var import_plugin_disable_introspection = require("@graphql-yoga/plugin-disable-introspection"); var import_graphql_yoga = require("stellate/graphql-yoga"); var getYogaPlugins = (stellate) => { return [ (0, import_plugin_defer_stream.useDeferStream)(), process.env.NODE_ENV === "production" && (0, import_plugin_disable_introspection.useDisableIntrospection)(), process.env.NODE_ENV === "production" && (0, import_graphql_armor_block_field_suggestions.blockFieldSuggestionsPlugin)(), Boolean(process.env.NODE_ENV === "production" && stellate) && (0, import_graphql_yoga.createStellateLoggerPlugin)({ serviceName: stellate.serviceName, token: stellate.loggingToken, fetch }) ].filter(Boolean); }; var wrappedContext = (context) => { return async (ct) => { const baseContext = { request: ct.request, headers: ct.request.headers, params: ct.params }; if (typeof context === "function") { const userCtx = context(baseContext); if (userCtx.then) { const result = await userCtx; return { ...baseContext, ...result }; } return { ...baseContext, ...userCtx }; } else if (typeof context === "object") { return { ...baseContext, ...context }; } return baseContext; }; }; // src/next/index.ts var defaultQuery = ( /* GraphQL */ `query { _version } ` ); function createAPIRouteHandler(options) { return (request, context) => { const completedSchema = import_fuse.builder.toSchema({}); if (process.env.NODE_ENV === "development") { (0, import_promises.writeFile)("./schema.graphql", (0, import_graphql.printSchema)(completedSchema), "utf-8"); } const { handleRequest } = (0, import_graphql_yoga2.createYoga)({ maskedErrors: process.env.NODE_ENV === "production", graphiql: process.env.NODE_ENV !== "production" ? { title: "Fuse GraphiQL", defaultQuery } : false, schema: completedSchema, // We allow batching by default batching: true, context: wrappedContext(options?.context), // While using Next.js file convention for routing, we need to configure Yoga to use the correct endpoint graphqlEndpoint: "/api/fuse", // Yoga needs to know how to create a valid Next response fetchAPI: { Response }, plugins: getYogaPlugins(options?.stellate) }); return handleRequest(request, context); }; } function createPagesRouteHandler(options) { const schema = import_fuse.builder.toSchema({}); if (process.env.NODE_ENV === "development") { (0, import_promises.writeFile)("./schema.graphql", (0, import_graphql.printSchema)(schema), "utf-8"); } return (0, import_graphql_yoga2.createYoga)({ schema, graphiql: process.env.NODE_ENV !== "production" ? { title: "Fuse GraphiQL", defaultQuery } : false, maskedErrors: process.env.NODE_ENV === "production", batching: true, context: wrappedContext(options?.context), graphqlEndpoint: "/api/fuse", plugins: getYogaPlugins(options?.stellate) }); } // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { createAPIRouteHandler, createPagesRouteHandler });