Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export default function createApolloServer(options = {}) {
const { context: contextFromOptions, expressMiddleware, resolvers } = options;
const path = options.path || DEFAULT_GRAPHQL_PATH;
// We support passing in either a typeDefs string or an already executable schema,
// for the case where a plugin is stitching in a schema from an external API.
const schemas = options.schemas || [];
const schemasToMerge = schemas.filter((td) => typeof td !== "string");
const typeDefs = schemas.filter((td) => typeof td === "string");
// Create a custom Express server so that we can add our own middleware and HTTP routes
const app = express();
let schema = makeExecutableSchema({ typeDefs, resolvers, resolverValidationOptions });
if (schemasToMerge.length) {
schema = mergeSchemas({ schemas: [schema, ...schemasToMerge] });
}
const apolloServer = new ApolloServer({
async context({ connection, req }) {
const context = { ...contextFromOptions };
// For a GraphQL subscription WebSocket request, there is no `req`
if (connection) return context;
// Express middleware should have already set req.user if there is one
await buildContext(context, req);
await createDataLoaders(context);
return context;
},
// We support passing in either a typeDefs string or an already executable schema,
// for the case where a plugin is stitching in a schema from an external API.
const schemas = options.schemas || [];
const schemasToMerge = schemas.filter((td) => typeof td !== "string");
const typeDefs = schemas.filter((td) => typeof td === "string");
if (typeDefs.length === 0 && schemasToMerge.length === 0) {
throw new Error("No type definitions (schemas) provided for GraphQL");
}
// Create a custom Express server so that we can add our own middleware and HTTP routes
const app = express();
let schema = makeExecutableSchema({ typeDefs, resolvers, resolverValidationOptions });
if (schemasToMerge.length) {
schema = mergeSchemas({ schemas: [schema, ...schemasToMerge] });
}
const apolloServer = new ApolloServer({
async context({ connection, req }) {
const context = { ...contextFromOptions };
// For a GraphQL subscription WebSocket request, there is no `req`
if (connection) return context;
// Express middleware should have already set req.user if there is one
await buildContext(context, req);
await createDataLoaders(context);
return context;
},
export async function createAndMergeRemoteSchemas() {
const schemas = await Promise.all(["cms/drupal/web/graphql"].map(createRemoteSchema));
const filteredSchemas = schemas.filter((schema) => schema !== undefined);
if (filteredSchemas.length) {
return mergeSchemas({schemas: filteredSchemas});
} else {
throw new NoRemoteSchemasFound("Couldn't generate remote schemas from any service.");
}
}