Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(apollo-server-testing): improve types #4383

1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ The version headers in this history reflect the versions of Apollo Server itself

## v2.19.0

- `apollo-server-testing`: types: Allow generic `variables` usage of `query` and `mutate` functions. [PR #4383](https://github.com/apollograpqh/apollo-server/pull/4383)
- `apollo-server-core`: Explicitly include `lru-cache` dependency in `apollo-server-core`'s dependencies. [PR #4600](https://github.com/apollographql/apollo-server/pull/4600)
- `apollo-server-core`: Update dependency for `@apollographql/graphql-playground-react` to `v1.7.34` to incorporate [`33c48183`](https://github.com/apollographql/graphql-playground/commit/33c48183229e59f0d21f7f4fdd57bed5a973a5cf) which, as an upstream change, should improve support for `graphql@15` features, like interfaces implementing interfaces.
- `apollo-server-express`: Export the `GetMiddlewareOptions` type. [PR #4599](https://github.com/apollograpqh/apollo-server/pull/4599)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,17 @@ describe('createTestClient', () => {
expect(res.data).toEqual({ test: 'wow' });
});

it('works with generic types', async () => {
const query = `query test($echo: String){ test(echo: $echo) }`;
type Data = { test?: string | null; };
type Variables = { echo: string; };
const client = createTestClient(myTestServer);
const res1 = await client.query<Data>({ query, variables: { echo: 'onlydata' } });
expect(res1.data).toEqual({ test: 'onlydata' });
const res2 = await client.query<Data, Variables>({ query, variables: { echo: 'data and variables' } });
expect(res2.data).toEqual({ test: 'data and variables' });
})

it('resolves with context', async () => {
const query = `{ hello }`;
const client = createTestClient(myTestServer);
Expand Down
24 changes: 12 additions & 12 deletions packages/apollo-server-testing/src/createTestClient.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import { ApolloServerBase } from 'apollo-server-core';
import { GraphQLResponse } from 'apollo-server-types';
import { GraphQLResponse as GraphQLResponseType } from 'apollo-server-types';
import { print, DocumentNode } from 'graphql';

type StringOrAst = string | DocumentNode;

// A query must not come with a mutation (and vice versa).
type Query = {
type Query<TVariables = Record<string, any>> = {
query: StringOrAst;
mutation?: undefined;
variables?: {
[name: string]: any;
};
variables?: TVariables;
operationName?: string;
};
type Mutation = {
type Mutation<TVariables = Record<string, any>> = {
mutation: StringOrAst;
query?: undefined;
variables?: {
[name: string]: any;
};
variables?: TVariables;
operationName?: string;
};

type GraphQLResponse<TData> = Omit<GraphQLResponseType, 'data'> & {
data?: TData;
};

export interface ApolloServerTestClient {
query: (query: Query) => Promise<GraphQLResponse>;
mutate: (mutation: Mutation) => Promise<GraphQLResponse>;
query<TData = any, TVariables = Record<string, any>>(query: Query<TVariables>): Promise<GraphQLResponse<TData>>;
mutate<TData = any, TVariables = Record<string, any>>(mutation: Mutation<TVariables>): Promise<GraphQLResponse<TData>>;
}

export default (server: ApolloServerBase): ApolloServerTestClient => {
Expand All @@ -46,5 +46,5 @@ export default (server: ApolloServerBase): ApolloServerTestClient => {
});
};

return { query: test, mutate: test };
return { query: test, mutate: test } as ApolloServerTestClient;
};