Skip to content

Commit

Permalink
feat(core): Serve GraphiQL interface from Apollo plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
toBeOfUse committed Nov 30, 2024
1 parent 5b26714 commit f801e22
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions packages/core/src/api/config/configure-graphql-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import { DynamicModule } from '@nestjs/common';
import { GraphQLModule, GraphQLTypesLoader } from '@nestjs/graphql';
import { notNullOrUndefined } from '@vendure/common/lib/shared-utils';
import { readFileSync } from 'fs';
import { buildSchema, extendSchema, GraphQLSchema, printSchema, ValidationContext } from 'graphql';
import path from 'path';

Expand Down Expand Up @@ -115,6 +116,20 @@ async function createGraphQLOptions(
apolloServerPlugins.unshift(new IdCodecPlugin(idCodecService));
}

const GraphiQLPlugin = {
async serverWillStart() {
return {
async renderLandingPage() {
return {
html: readFileSync(path.resolve(__dirname, '../../graphiql/output/index.html'), {
encoding: 'utf-8',
}).replace('__API_URL__', '/' + options.apiPath),
};
},
};
},
};

return {
path: '/' + options.apiPath,
typeDefs: printSchema(builtSchema),
Expand All @@ -125,13 +140,16 @@ async function createGraphQLOptions(
// We no longer rely on the upload facility bundled with Apollo Server, and instead
// manually configure the graphql-upload package. See https://github.com/vendure-ecommerce/vendure/issues/396
uploads: false,
playground: options.playground,
// similarly, the built-in playground is outdated and buggy, so we
// replace it with `GraphiQLPlugin` in the `plugins` array
// https://github.com/vendure-ecommerce/vendure/issues/3244
playground: false,
csrfPrevention: false,
debug: options.debug || false,
context: (req: any) => req,
// This is handled by the Express cors plugin
cors: false,
plugins: apolloServerPlugins,
plugins: apolloServerPlugins.concat(options.playground ? [GraphiQLPlugin] : []),
validationRules: options.validationRules,
introspection: configService.apiOptions.introspection ?? true,
} as ApolloDriverConfig;
Expand Down

0 comments on commit f801e22

Please sign in to comment.