mirror of
https://github.com/vale981/apollo-server
synced 2025-03-05 09:41:40 -05:00
* test: ensure "total duration" > "duration of resolvers" Add test for https://github.com/apollographql/apollo-server/pull/2298 * Update tracing duration test to be more comprehensive Previously we were only guaranteeing that individual resolvers didn't have a duration longer than the total duration. The updated test guarantees that the duration (from when the first resolver started to when the last resolver ended) should be less than total duration * Add another resolver to tracing test * Prefer `const` over `let` for variables which aren't mutated.
This commit is contained in:
parent
4d017c0447
commit
5918e188d9
1 changed files with 63 additions and 0 deletions
|
@ -37,6 +37,7 @@ import {
|
||||||
ApolloServerBase,
|
ApolloServerBase,
|
||||||
} from 'apollo-server-core';
|
} from 'apollo-server-core';
|
||||||
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
|
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
|
||||||
|
import { TracingFormat } from 'apollo-tracing';
|
||||||
|
|
||||||
export function createServerInfo<AS extends ApolloServerBase>(
|
export function createServerInfo<AS extends ApolloServerBase>(
|
||||||
server: AS,
|
server: AS,
|
||||||
|
@ -1260,5 +1261,67 @@ export function testApolloServer<AS extends ApolloServerBase>(
|
||||||
}, done.fail);
|
}, done.fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('Tracing', () => {
|
||||||
|
const typeDefs = gql`
|
||||||
|
type Book {
|
||||||
|
title: String
|
||||||
|
author: String
|
||||||
|
}
|
||||||
|
|
||||||
|
type Movie {
|
||||||
|
title: String
|
||||||
|
}
|
||||||
|
|
||||||
|
type Query {
|
||||||
|
books: [Book]
|
||||||
|
movies: [Movie]
|
||||||
|
}
|
||||||
|
`;
|
||||||
|
|
||||||
|
const resolvers = {
|
||||||
|
Query: {
|
||||||
|
books: () =>
|
||||||
|
new Promise(resolve =>
|
||||||
|
setTimeout(() => resolve([{ title: 'H', author: 'J' }]), 10),
|
||||||
|
),
|
||||||
|
movies: () =>
|
||||||
|
new Promise(resolve =>
|
||||||
|
setTimeout(() => resolve([{ title: 'H' }]), 12),
|
||||||
|
),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
it('reports a total duration that is longer than the duration of its resolvers', async () => {
|
||||||
|
const { url: uri } = await createApolloServer({
|
||||||
|
typeDefs,
|
||||||
|
resolvers,
|
||||||
|
tracing: true,
|
||||||
|
});
|
||||||
|
|
||||||
|
const apolloFetch = createApolloFetch({ uri });
|
||||||
|
const result = await apolloFetch({
|
||||||
|
query: `{ books { title author } }`,
|
||||||
|
});
|
||||||
|
|
||||||
|
const tracing: TracingFormat = result.extensions.tracing;
|
||||||
|
|
||||||
|
const earliestStartOffset = tracing.execution.resolvers
|
||||||
|
.map(resolver => resolver.startOffset)
|
||||||
|
.reduce((currentEarliestOffset, nextOffset) =>
|
||||||
|
Math.min(currentEarliestOffset, nextOffset),
|
||||||
|
);
|
||||||
|
|
||||||
|
const latestEndOffset = tracing.execution.resolvers
|
||||||
|
.map(resolver => resolver.startOffset + resolver.duration)
|
||||||
|
.reduce((currentLatestEndOffset, nextEndOffset) =>
|
||||||
|
Math.min(currentLatestEndOffset, nextEndOffset),
|
||||||
|
);
|
||||||
|
|
||||||
|
const resolverDuration = latestEndOffset - earliestStartOffset;
|
||||||
|
|
||||||
|
expect(resolverDuration).not.toBeGreaterThan(tracing.duration);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue