@@ -28,13 +28,43 @@ const preconditionFailed = (preconditionName: string) => {
2828 throw new HttpException ( `Precondition failed: ${ preconditionName } ` , HttpStatus . PRECONDITION_FAILED ) ;
2929} ;
3030
31+ export const getPluginBasedOnSandbox = async ( sandbox : boolean , csrfToken : string ) => {
32+ if ( sandbox ) {
33+ const { ApolloServerPluginLandingPageLocalDefault } = await import (
34+ '@apollo/server/plugin/landingPage/default'
35+ ) ;
36+ const plugin = ApolloServerPluginLandingPageLocalDefault ( {
37+ footer : false ,
38+ includeCookies : true ,
39+ document : initialDocument ,
40+ embed : {
41+ initialState : {
42+ sharedHeaders : {
43+ 'x-csrf-token' : csrfToken ,
44+ } ,
45+ } ,
46+ } ,
47+ } ) ;
48+ return plugin ;
49+ } else {
50+ const { ApolloServerPluginLandingPageProductionDefault } = await import (
51+ '@apollo/server/plugin/landingPage/default'
52+ ) ;
53+
54+ const plugin = ApolloServerPluginLandingPageProductionDefault ( {
55+ footer : false
56+ } ) ;
57+ return plugin ;
58+ }
59+ } ;
60+
3161/**
3262 * Renders the sandbox page for the GraphQL server with Apollo Server landing page configuration.
33- *
63+ *
3464 * @param service - The GraphQL server context object
3565 * @returns Promise that resolves to an Apollo `LandingPage`, or throws a precondition failed error
3666 * @throws {Error } When downstream plugin components from apollo are unavailable. This should never happen.
37- *
67+ *
3868 * @remarks
3969 * This function configures and renders the Apollo Server landing page with:
4070 * - Disabled footer
@@ -44,33 +74,22 @@ const preconditionFailed = (preconditionName: string) => {
4474 */
4575async function renderSandboxPage ( service : GraphQLServerContext ) {
4676 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- } ) ;
77+ const sandbox = getters . config ( ) . local . sandbox === 'yes' ;
78+ const csrfToken = getters . emhttp ( ) . var . csrfToken ;
79+ const plugin = await getPluginBasedOnSandbox ( sandbox , csrfToken ) ;
80+
6281 if ( ! plugin . serverWillStart ) return preconditionFailed ( 'serverWillStart' ) ;
6382 const serverListener = await plugin . serverWillStart ( service ) ;
6483
6584 if ( ! serverListener ) return preconditionFailed ( 'serverListener' ) ;
6685 if ( ! serverListener . renderLandingPage ) return preconditionFailed ( 'renderLandingPage' ) ;
67-
86+
6887 return serverListener . renderLandingPage ( ) ;
6988}
7089
7190/**
7291 * Apollo plugin to render the GraphQL Sandbox page on-demand based on current server state.
73- *
92+ *
7493 * Usually, the `ApolloServerPluginLandingPageLocalDefault` plugin configures its
7594 * parameters once, during server startup. This plugin defers the configuration
7695 * and rendering to request-time instead of server startup.
0 commit comments