Skip to content

Commit

Permalink
test usage reporting
Browse files Browse the repository at this point in the history
  • Loading branch information
glasser committed Sep 23, 2022
1 parent 1dcd2e4 commit 6d7ccf6
Showing 1 changed file with 63 additions and 3 deletions.
66 changes: 63 additions & 3 deletions packages/integration-testsuite/src/apolloServerTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,7 @@ export function defineIntegrationTestSuiteApolloServerTests(
describe('traces', () => {
let throwError: Mock;
let apolloFetch: ApolloFetch;
let uri: string;

beforeEach(async () => {
throwError = jest.fn();
Expand All @@ -884,8 +885,13 @@ export function defineIntegrationTestSuiteApolloServerTests(
constructorOptions: Partial<CreateServerForIntegrationTests> = {},
plugins: ApolloServerPlugin<BaseContext>[] = [],
) => {
const uri = await createServerAndGetUrl({
uri = await createServerAndGetUrl({
typeDefs: gql`
directive @defer(
if: Boolean! = true
label: String
) on FRAGMENT_SPREAD | INLINE_FRAGMENT
enum CacheControlScope {
PUBLIC
PRIVATE
Expand All @@ -898,14 +904,23 @@ export function defineIntegrationTestSuiteApolloServerTests(
type Query {
fieldWhichWillError: String
delayedFoo: Foo
justAField: String @cacheControl(maxAge: 5, scope: PRIVATE)
}
type Foo {
bar: String
}
`,
resolvers: {
Query: {
fieldWhichWillError: () => {
throwError();
},
delayedFoo: async () => {
await new Promise<void>((r) => setTimeout(r, 10));
return { bar: 'hi' };
},
justAField: () => 'a string',
},
},
Expand Down Expand Up @@ -1073,6 +1088,53 @@ export function defineIntegrationTestSuiteApolloServerTests(
expect(Object.keys(reports[0].tracesPerQuery)[0]).toMatch(/^# -\n/);
});

(process.env.INCREMENTAL_DELIVERY_TESTS_ENABLED ? it : it.skip)(
'includes all fields with defer',
async () => {
await setupApolloServerAndFetchPair();
const response = await fetch(uri, {
method: 'POST',
headers: {
'content-type': 'application/json',
accept: 'multipart/mixed; deferSpec=20220824',
},
body: JSON.stringify({
query: '{ justAField ...@defer { delayedFoo { bar} } }',
}),
});
expect(response.status).toBe(200);
expect(
response.headers.get('content-type'),
).toMatchInlineSnapshot(
`"multipart/mixed; boundary="-"; deferSpec=20220824"`,
);
expect(await response.text()).toMatchInlineSnapshot(`
"
---
content-type: application/json
{"hasNext":true,"data":{"justAField":"a string"}}
---
content-type: application/json
{"hasNext":false,"incremental":[{"path":[],"data":{"delayedFoo":{"bar":"hi"}}}]}
-----
"
`);
const reports = await reportIngress.promiseOfReports;
expect(reports.length).toBe(1);
expect(Object.keys(reports[0].tracesPerQuery)).toHaveLength(1);
const trace = Object.values(reports[0].tracesPerQuery)[0]
.trace?.[0] as Trace;
expect(trace).toBeDefined();
expect(trace?.root?.child?.[0].responseName).toBe('justAField');
expect(trace?.root?.child?.[1].responseName).toBe('delayedFoo');
expect(trace?.root?.child?.[1].child?.[0].responseName).toBe(
'bar',
);
},
);

it("doesn't resort to query body signature on `didResolveOperation` error", async () => {
await setupApolloServerAndFetchPair({}, {}, [
{
Expand Down Expand Up @@ -2154,8 +2216,6 @@ export function defineIntegrationTestSuiteApolloServerTests(
});

describe('usage reporting', () => {
// FIXME ensure deferred fields get usage-reported

async function makeFakeUsageReportingServer({
status,
waitWriteResponse = false,
Expand Down

0 comments on commit 6d7ccf6

Please sign in to comment.