Skip to content

Commit

Permalink
Update some integration tests to pass new arguments
Browse files Browse the repository at this point in the history
Delete maskErrorDetails integration test and replace it with a unit test.
  • Loading branch information
glasser committed Sep 18, 2020
1 parent 0679a02 commit d852220
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
GraphQLRequestContextDidResolveOperation,
GraphQLRequestContextDidEncounterErrors,
} from 'apollo-server-types';
import { GraphQLError } from 'graphql';

describe('doubly-legacy privateVariables and privateHeaders options', () => {
it('privateVariables/privateHeaders == false; same as all', () => {
Expand Down Expand Up @@ -84,3 +85,11 @@ it('reportTiming', () => {
includeRequest: f,
});
});

it('maskErrorDetails', () => {
const newOptions = legacyOptionsToPluginOptions({ maskErrorDetails: true });
expect(newOptions.rewriteError).toBeTruthy();
expect(newOptions.rewriteError!(new GraphQLError('foo'))?.message).toBe(
'<masked>',
);
});
137 changes: 53 additions & 84 deletions packages/apollo-server-integration-testsuite/src/ApolloServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ import {
GraphQLService,
GraphQLExecutor,
ApolloServerPluginInlineTrace,
EngineReportingOptions,
ApolloServerPluginUsageReporting,
ApolloServerPluginUsageReportingOptions,
} from 'apollo-server-core';
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
import { TracingFormat } from 'apollo-tracing';
Expand Down Expand Up @@ -907,13 +908,7 @@ export function testApolloServer<AS extends ApolloServerBase>(
});
}

public engineOptions(): Partial<EngineReportingOptions<any>> {
return {
tracesEndpointUrl: this.getUrl(),
};
}

private getUrl(): string {
getUrl(): string {
if (!this.server) {
throw new Error('must listen before getting URL');
}
Expand Down Expand Up @@ -999,17 +994,21 @@ export function testApolloServer<AS extends ApolloServerBase>(
},
validationRules: [validationRule],
extensions: [() => new Extension()],
engine: {
apollo: {
key: 'service:my-app:secret',
graphVariant: 'current',
...reportIngress.engineOptions(),
apiKey: 'service:my-app:secret',
maxUncompressedReportSize: 1,
generateClientInfo: () => ({
clientName: 'testing',
clientReferenceId: '1234',
clientVersion: 'v1.0.1',
}),
},
plugins: [
ApolloServerPluginUsageReporting({
endpointUrl: reportIngress.getUrl(),
maxUncompressedReportSize: 1,
generateClientInfo: () => ({
clientName: 'testing',
clientReferenceId: '1234',
clientVersion: 'v1.0.1',
}),
}),
],
formatError,
debug: true,
stopOnTerminationSignals: false,
Expand Down Expand Up @@ -1057,8 +1056,9 @@ export function testApolloServer<AS extends ApolloServerBase>(
});

const setupApolloServerAndFetchPair = async (
engineOptions: Partial<EngineReportingOptions<any>> = {},
usageReportingOptions: Partial<ApolloServerPluginUsageReportingOptions<any>> = {},
constructorOptions: Partial<CreateServerFunc<AS>> = {},
plugins: PluginDefinition[] = [],
) => {
const { url: uri } = await createApolloServer({
typeDefs: gql`
Expand All @@ -1075,13 +1075,18 @@ export function testApolloServer<AS extends ApolloServerBase>(
justAField: () => 'a string',
},
},
engine: {
apollo: {
key: 'service:my-app:secret',
graphVariant: 'current',
...reportIngress.engineOptions(),
apiKey: 'service:my-app:secret',
maxUncompressedReportSize: 1,
...engineOptions,
},
plugins: [
ApolloServerPluginUsageReporting({
endpointUrl: reportIngress.getUrl(),
maxUncompressedReportSize: 1,
...usageReportingOptions,
}),
...plugins,
],
debug: true,
stopOnTerminationSignals: false,
...constructorOptions,
Expand Down Expand Up @@ -1163,19 +1168,17 @@ export function testApolloServer<AS extends ApolloServerBase>(
});

it("doesn't resort to query body signature on `didResolveOperation` error", async () => {
await setupApolloServerAndFetchPair(Object.create(null), {
plugins: [
{
requestDidStart() {
return {
didResolveOperation() {
throw new Error('known_error');
},
};
},
await setupApolloServerAndFetchPair({}, {}, [
{
requestDidStart() {
return {
didResolveOperation() {
throw new Error('known_error');
},
};
},
],
});
},
]);

const result = await apolloFetch({
query: `{ aliasedField: justAField }`,
Expand Down Expand Up @@ -1414,44 +1417,6 @@ export function testApolloServer<AS extends ApolloServerBase>(
},
]);
});

// This is deprecated, but we'll test it until it's removed in
// Apollo Server 3.x.
it('maskErrorDetails (legacy)', async () => {
throwError.mockImplementationOnce(() => {
throw new Error('maskErrorDetails nope');
});

await setupApolloServerAndFetchPair({
maskErrorDetails: true,
});

const result = await apolloFetch({
query: `{fieldWhichWillError}`,
});

expect(result.data).toEqual({
fieldWhichWillError: null,
});
expect(result.errors).toBeDefined();
expect(result.errors[0].message).toEqual('maskErrorDetails nope');

expect(throwError).toHaveBeenCalledTimes(1);

const reports = await reportIngress.promiseOfReports;
expect(reports.length).toBe(1);
const trace = Object.values(reports[0].tracesPerQuery)[0]
.trace[0];

expect(trace.root.child[0].error).toMatchObject([
{
json:
'{"message":"<masked>","locations":[{"line":1,"column":2}],"path":["fieldWhichWillError"]}',
message: '<masked>',
location: [{ line: 1, column: 2 }],
},
]);
});
});
});
});
Expand Down Expand Up @@ -2459,17 +2424,21 @@ export function testApolloServer<AS extends ApolloServerBase>(
}
`,
resolvers: { Query: { something: () => 'hello' } },
engine: {
apiKey: 'service:my-app:secret',
apollo: {
key: 'service:my-app:secret',
graphVariant: 'current',
tracesEndpointUrl: fakeEngineUrl,
reportIntervalMs: 1,
maxAttempts: 3,
requestAgent,
reportErrorFunction(error: Error) {
reportErrorPromiseResolve(error);
},
},
plugins: [
ApolloServerPluginUsageReporting({
endpointUrl: fakeEngineUrl,
reportIntervalMs: 1,
maxAttempts: 3,
requestAgent,
reportErrorFunction(error: Error) {
reportErrorPromiseResolve(error);
},
}),
],
});

const apolloFetch = createApolloFetch({ uri });
Expand Down Expand Up @@ -3203,15 +3172,15 @@ export function testApolloServer<AS extends ApolloServerBase>(
expect(result2.errors).toBeUndefined();
});

it('passes engine data to the gateway', async () => {
it('passes apollo and engine data to the gateway', async () => {
const optionsSpy = jest.fn();

const { gateway, triggers } = makeGatewayMock({ optionsSpy });
triggers.resolveLoad({ schema, executor: () => {} });
await createApolloServer({
gateway,
subscriptions: false,
engine: { apiKey: 'service:tester:1234abc', schemaTag: 'staging' },
apollo: { key: 'service:tester:1234abc', graphVariant: 'staging' },
});

expect(optionsSpy).toHaveBeenLastCalledWith({
Expand Down

0 comments on commit d852220

Please sign in to comment.