|
| 1 | +import { HttpException, HttpStatus } from '@nestjs/common'; |
| 2 | + |
| 3 | +import type { ApolloServerPlugin, GraphQLServerContext, GraphQLServerListener } from '@apollo/server'; |
| 4 | + |
| 5 | +/** The initial query displayed in the Apollo sandbox */ |
| 6 | +const initialDocument = `query ExampleQuery { |
| 7 | + notifications { |
| 8 | + id |
| 9 | + overview { |
| 10 | + unread { |
| 11 | + info |
| 12 | + warning |
| 13 | + alert |
| 14 | + total |
| 15 | + } |
| 16 | + archive { |
| 17 | + info |
| 18 | + warning |
| 19 | + alert |
| 20 | + total |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + }`; |
| 25 | + |
| 26 | +/** helper for raising precondition failure errors during an http request. */ |
| 27 | +const preconditionFailed = (preconditionName: string) => { |
| 28 | + throw new HttpException(`Precondition failed: ${preconditionName} `, HttpStatus.PRECONDITION_FAILED); |
| 29 | +}; |
| 30 | + |
| 31 | +/** |
| 32 | + * Renders the sandbox page for the GraphQL server with Apollo Server landing page configuration. |
| 33 | + * |
| 34 | + * @param service - The GraphQL server context object |
| 35 | + * @returns Promise that resolves to an Apollo `LandingPage`, or throws a precondition failed error |
| 36 | + * @throws {Error} When downstream plugin components from apollo are unavailable. This should never happen. |
| 37 | + * |
| 38 | + * @remarks |
| 39 | + * This function configures and renders the Apollo Server landing page with: |
| 40 | + * - Disabled footer |
| 41 | + * - Enabled cookies |
| 42 | + * - Initial document state |
| 43 | + * - Shared headers containing CSRF token |
| 44 | + */ |
| 45 | +async function renderSandboxPage(service: GraphQLServerContext) { |
| 46 | + const { getters } = await import('@app/store'); |
| 47 | + const { ApolloServerPluginLandingPageLocalDefault } = await import( |
| 48 | + '@apollo/server/plugin/landingPage/default' |
| 49 | + ); |
| 50 | + const plugin = ApolloServerPluginLandingPageLocalDefault({ |
| 51 | + footer: false, |
| 52 | + includeCookies: true, |
| 53 | + document: initialDocument, |
| 54 | + embed: { |
| 55 | + initialState: { |
| 56 | + sharedHeaders: { |
| 57 | + 'x-csrf-token': getters.emhttp().var.csrfToken, |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }); |
| 62 | + if (!plugin.serverWillStart) return preconditionFailed('serverWillStart'); |
| 63 | + const serverListener = await plugin.serverWillStart(service); |
| 64 | + |
| 65 | + if (!serverListener) return preconditionFailed('serverListener'); |
| 66 | + if (!serverListener.renderLandingPage) return preconditionFailed('renderLandingPage'); |
| 67 | + return serverListener.renderLandingPage(); |
| 68 | +} |
| 69 | + |
| 70 | +/** |
| 71 | + * Apollo plugin to render the GraphQL Sandbox page on-demand based on current server state. |
| 72 | + * |
| 73 | + * Usually, the `ApolloServerPluginLandingPageLocalDefault` plugin configures its |
| 74 | + * parameters once, during server startup. This plugin defers the configuration |
| 75 | + * and rendering to request-time instead of server startup. |
| 76 | + */ |
| 77 | +export const sandboxPlugin: ApolloServerPlugin = { |
| 78 | + serverWillStart: async (service) => |
| 79 | + ({ |
| 80 | + renderLandingPage: () => renderSandboxPage(service), |
| 81 | + }) satisfies GraphQLServerListener, |
| 82 | +}; |
0 commit comments