Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
process.env.GRAPHQL_MOCK !== undefined &&
process.env.GRAPHQL_MOCK === 'true'
? mocks
: false;
const myStarwarsAPI: any = new StarwarsAPI();
return {
typeDefs,
resolvers,
dataSources: () => {
return {
starwarsAPI: myStarwarsAPI
};
},
cache: new InMemoryLRUCache({ maxSize: 100 }),
schemaDirectives,
mocks: serverMocks,
// formatError, // Error Handler
tracing,
playground,
introspection: true,
context: async ({ req, connection }) => {
if (connection) {
// check connection for metadata
return {};
} else {
const user = getuser(req);
return {
// Setup the user context as well as the dataload context
user,
peopleLoader,
private initializeDocumentStore(): InMemoryLRUCache {
return new InMemoryLRUCache({
// Create ~about~ a 30MiB InMemoryLRUCache. This is less than precise
// since the technique to calculate the size of a DocumentNode is
// only using JSON.stringify on the DocumentNode (and thus doesn't account
// for unicode characters, etc.), but it should do a reasonable job at
// providing a caching document store for most operations.
maxSize: Math.pow(2, 20) * 30,
sizeCalculator: approximateObjectSize,
});
}
private initializeQueryPlanStore(): void {
this.queryPlanStore = new InMemoryLRUCache({
// Create ~about~ a 30MiB InMemoryLRUCache. This is less than precise
// since the technique to calculate the size of a DocumentNode is
// only using JSON.stringify on the DocumentNode (and thus doesn't account
// for unicode characters, etc.), but it should do a reasonable job at
// providing a caching document store for most operations.
maxSize: Math.pow(2, 20) * 30,
sizeCalculator: approximateObjectSize,
});
}
defaultMaxAge: 0,
};
} else {
// Default behavior is to run default header calculation and return
// no cacheControl extensions
requestOptions.cacheControl = {
stripFormattedExtensions: true,
calculateHttpHeaders: true,
defaultMaxAge: 0,
...requestOptions.cacheControl,
};
}
}
if (!requestOptions.cache) {
requestOptions.cache = new InMemoryLRUCache();
}
if (requestOptions.persistedQueries !== false) {
const {
cache: apqCache = requestOptions.cache!,
...apqOtherOptions
} = requestOptions.persistedQueries || Object.create(null);
requestOptions.persistedQueries = {
cache: new PrefixingKeyValueCache(apqCache, APQ_CACHE_PREFIX),
...apqOtherOptions,
};
} else {
// the user does not want to use persisted queries, so we remove the field
delete requestOptions.persistedQueries;
}
// if this is local dev, introspection should turned on
// in production, we can manually turn introspection on by passing {
// introspection: true } to the constructor of ApolloServer
if (
(typeof introspection === 'boolean' && !introspection) ||
(introspection === undefined && !isDev)
) {
const noIntro = [NoIntrospection];
requestOptions.validationRules = requestOptions.validationRules
? requestOptions.validationRules.concat(noIntro)
: noIntro;
}
if (!requestOptions.cache) {
requestOptions.cache = new InMemoryLRUCache();
}
if (requestOptions.persistedQueries !== false) {
if (!requestOptions.persistedQueries) {
requestOptions.persistedQueries = {
cache: requestOptions.cache!,
};
}
} else {
// the user does not want to use persisted queries, so we remove the field
delete requestOptions.persistedQueries;
}
this.requestOptions = requestOptions as GraphQLOptions;
this.context = context;
} else if (extensions.persistedQuery.version !== 1) {
throw new InvalidGraphQLRequestError(
'Unsupported persisted query version',
);
}
// We'll store a reference to the persisted query cache so we can actually
// do the write at a later point in the request pipeline processing.
persistedQueryCache = config.persistedQueries.cache;
// This is a bit hacky, but if `config` came from direct use of the old
// apollo-server 1.0-style middleware (graphqlExpress etc, not via the
// ApolloServer class), it won't have been converted to
// PrefixingKeyValueCache yet.
if (!(persistedQueryCache instanceof PrefixingKeyValueCache)) {
persistedQueryCache = new PrefixingKeyValueCache(
persistedQueryCache,
APQ_CACHE_PREFIX,
);
}
queryHash = extensions.persistedQuery.sha256Hash;
if (query === undefined) {
query = await persistedQueryCache.get(queryHash);
if (query) {
metrics.persistedQueryHit = true;
} else {
throw new PersistedQueryNotFoundError();
}
} else {
const computedQueryHash = computeQueryHash(query);
};
}
}
if (!requestOptions.cache) {
requestOptions.cache = new InMemoryLRUCache();
}
if (requestOptions.persistedQueries !== false) {
const {
cache: apqCache = requestOptions.cache!,
...apqOtherOptions
} = requestOptions.persistedQueries || Object.create(null);
requestOptions.persistedQueries = {
cache: new PrefixingKeyValueCache(apqCache, APQ_CACHE_PREFIX),
...apqOtherOptions,
};
} else {
// the user does not want to use persisted queries, so we remove the field
delete requestOptions.persistedQueries;
}
this.requestOptions = requestOptions as GraphQLServerOptions;
if (uploads !== false && !forbidUploadsForTesting) {
if (this.supportsUploads()) {
if (!runtimeSupportsUploads) {
printNodeFileUploadsMessage();
throw new Error(
'`graphql-upload` is no longer supported on Node.js < v8.5.0. ' +
'See https://bit.ly/gql-upload-node-6.',
constructor(cache = new InMemoryLRUCache(), knex) {
this.cache = cache;
this.loader = new DataLoader(rawQueries =>
Promise.all(rawQueries.map(rawQuery => knex.raw(rawQuery)))
);
}
initialize({ context, cache } = {}) {
this.context = context
const methods = createCachingMethods({
collection: this.collection,
cache: cache || new InMemoryLRUCache()
})
Object.assign(this, methods)
}
}
constructor(config) {
this.graphqlPath = '/graphql';
if (!config)
throw new Error('ApolloServer requires options.');
const { context, resolvers, schema, schemaDirectives, typeDefs, introspection, mocks, extensions, engine, subscriptions, uploads, playground } = config, requestOptions = __rest(config, ["context", "resolvers", "schema", "schemaDirectives", "typeDefs", "introspection", "mocks", "extensions", "engine", "subscriptions", "uploads", "playground"]);
const isDev = process.env.NODE_ENV !== 'production';
if ((typeof introspection === 'boolean' && !introspection) ||
(introspection === undefined && !isDev)) {
const noIntro = [NoIntrospection];
requestOptions.validationRules = requestOptions.validationRules
? requestOptions.validationRules.concat(noIntro)
: noIntro;
}
if (!requestOptions.cache) {
requestOptions.cache = new apollo_server_caching_1.InMemoryLRUCache();
}
if (requestOptions.persistedQueries !== false) {
if (!requestOptions.persistedQueries) {
requestOptions.persistedQueries = {
cache: requestOptions.cache,
};
}
}
else {
delete requestOptions.persistedQueries;
}
this.requestOptions = requestOptions;
this.context = context;
if (uploads !== false) {
if (this.supportsUploads()) {
if (uploads === true || typeof uploads === 'undefined') {