Skip to content

Commit

Permalink
Merge pull request #3996 abernix/add-schema-and-schemaHash-to-request…
Browse files Browse the repository at this point in the history
…Context
  • Loading branch information
abernix authored May 12, 2020
2 parents b2073e8 + 87b403c commit 13fa77d
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/apollo-server-core/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,12 @@ export class ApolloServerBase {
protected async graphQLServerOptions(
integrationContextArgument?: Record<string, any>,
): Promise<GraphQLServerOptions> {
const { schema, documentStore, extensions } = await this.schemaDerivedData;
const {
schema,
schemaHash,
documentStore,
extensions,
} = await this.schemaDerivedData;

let context: Context = this.context ? this.context : {};

Expand All @@ -814,6 +819,7 @@ export class ApolloServerBase {

return {
schema,
schemaHash,
logger: this.logger,
plugins: this.plugins,
documentStore,
Expand Down Expand Up @@ -843,6 +849,8 @@ export class ApolloServerBase {

const requestCtx: GraphQLRequestContext = {
logger: this.logger,
schema: options.schema,
schemaHash: options.schemaHash,
request,
context: options.context || Object.create(null),
cache: options.cache!,
Expand Down
24 changes: 24 additions & 0 deletions packages/apollo-server-core/src/__tests__/runQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
} from 'apollo-server-plugin-base';
import { GraphQLRequestListener } from 'apollo-server-plugin-base';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { generateSchemaHash } from "../utils/schemaHash";

// This is a temporary kludge to ensure we preserve runQuery behavior with the
// GraphQLRequestProcessor refactoring.
Expand All @@ -44,8 +45,13 @@ function runQuery(options: QueryOptions): Promise<GraphQLResponse> {
http: options.request,
};

const schemaHash = generateSchemaHash(schema);

return processGraphQLRequest(options, {
request,
schema: options.schema,
schemaHash,
metrics: {},
logger: console,
context: options.context || {},
debug: options.debug,
Expand Down Expand Up @@ -477,6 +483,24 @@ describe('runQuery', () => {
await runOnce();
expect(requestDidStart.mock.calls.length).toBe(2);
});

it('is called with the schema and schemaHash', async () => {
await runQuery({
schema,
queryString: '{ testString }',
plugins: [
{
requestDidStart,
},
],
request: new MockReq(),
});

const invocation = requestDidStart.mock.calls[0][0];
expect(invocation).toHaveProperty('schema', schema);
expect(invocation).toHaveProperty( /* Shorter as a RegExp */
'schemaHash', expect.stringMatching(/^8ff87f3e0/));
});
});

/**
Expand Down
2 changes: 2 additions & 0 deletions packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
GraphQLResponse,
GraphQLRequestContext,
Logger,
SchemaHash,
} from 'apollo-server-types';

/*
Expand All @@ -42,6 +43,7 @@ export interface GraphQLServerOptions<
TRootValue = any
> {
schema: GraphQLSchema;
schemaHash: SchemaHash;
logger?: Logger;
formatError?: (error: GraphQLError) => GraphQLFormattedError;
rootValue?: ((parsedQuery: DocumentNode) => TRootValue) | TRootValue;
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-server-core/src/runHttpQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export async function runHttpQuery(

const config = {
schema: options.schema,
schemaHash: options.schemaHash,
logger: options.logger,
rootValue: options.rootValue,
context: options.context || {},
Expand Down Expand Up @@ -256,6 +257,8 @@ export async function processHTTPRequest<TContext>(
// exported since perhaps as far back as Apollo Server 1.x. Therefore,
// for compatibility reasons, we'll default to `console`.
logger: options.logger || console,
schema: options.schema,
schemaHash: options.schemaHash,
request,
response: {
http: {
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-server-core/src/utils/pluginTestHarness.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from 'apollo-server-plugin-base';
import { InMemoryLRUCache } from 'apollo-server-caching';
import { Dispatcher } from './dispatcher';
import { generateSchemaHash } from "./schemaHash";

// This test harness guarantees the presence of `query`.
type IPluginTestHarnessGraphqlRequest = WithRequired<GraphQLRequest, 'query'>;
Expand Down Expand Up @@ -99,6 +100,8 @@ export default async function pluginTestHarness<TContext>({

const requestContext: GraphQLRequestContext<TContext> = {
logger: logger || console,
schema,
schemaHash: generateSchemaHash(schema),
request: graphqlRequest,
metrics: Object.create(null),
source: graphqlRequest.query,
Expand Down
3 changes: 3 additions & 0 deletions packages/apollo-server-types/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,9 @@ export interface GraphQLRequestContext<TContext = Record<string, any>> {

logger: Logger;

readonly schema: GraphQLSchema;
readonly schemaHash: SchemaHash;

readonly context: TContext;
readonly cache: KeyValueCache;

Expand Down

0 comments on commit 13fa77d

Please sign in to comment.