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

apollo-server-testing createTestClient types #2172

Closed
ksabry opened this issue Jan 10, 2019 · 2 comments · Fixed by #2307
Closed

apollo-server-testing createTestClient types #2172

ksabry opened this issue Jan 10, 2019 · 2 comments · Fixed by #2307

Comments

@ksabry
Copy link

ksabry commented Jan 10, 2019

Writing the below in typescript complains with 'variables' does not exist in type 'Query | Mutation'

let client = createTestClient(server);
let result = await client.query({
    query: SOME_QUERY,
    variables: { var: "var" }
});

Looking in createTestClient.ts shows that client.query is written

({ query, mutation, ...args }: Query | Mutation) => { ... }

with

type Query = { query: StringOrAst; mutation?: undefined };
type Mutation = { mutation: StringOrAst; query?: undefined };

As it is currently written args is later passed through correctly to server.executeOperation, however the typings imply that args can only ever be an empty object when in fact it should allow the following additional fields (from the interface GraphQLRequest in requestPipelineAPI.ts)

{
    operationName?: string;
    variables?: { [name: string]: any };
    extensions?: Record<string, any>;
    http?: Pick<Request, 'url' | 'method' | 'headers'>;
}

It may be a good idea to implement it like this

type RequestNoQuery = Pick<GraphQLRequest, Exclude<keyof GraphQLRequest, "query">>;

type Query = { query: StringOrAst; mutation?: undefined } & RequestNoQuery;
type Mutation = { mutation: StringOrAst; query?: undefined } & RequestNoQuery;

So as to be less coupled to any changes to GraphQLRequest

@lirbank
Copy link

lirbank commented Jan 15, 2019

It would be great if the variables type was a generic (as well as the return type), just like apollo-client is doing it.

query<T, TVariables = OperationVariables>(options: QueryOptions<TVariables>): Promise<ApolloQueryResult<T>>;

@lirbank
Copy link

lirbank commented Jan 15, 2019

As a workaround, you can add a apollo-server-testing.d.ts file to your project with the following:

declare module 'apollo-server-testing' {
  import { ApolloServerBase } from 'apollo-server-core';
  import { print, DocumentNode } from 'graphql';
  import { GraphQLResponse } from 'graphql-extensions';

  type StringOrAst = string | DocumentNode;

  // A query must not come with a mutation (and vice versa).
  type Query<TVariables> = {
    query: StringOrAst;
    mutation?: undefined;
    variables?: TVariables;
  };

  type Mutation<TVariables> = {
    mutation: StringOrAst;
    query?: undefined;
    variables?: TVariables;
  };

  export const createTestClient: <TVariables>(
    server: ApolloServerBase,
  ) => {
    query: (query: Query<TVariables>) => Promise<GraphQLResponse>;
    mutate: (mutation: Mutation<TVariables>) => Promise<GraphQLResponse>;
  };
}

feinoujc added a commit to feinoujc/apollo-server that referenced this issue Feb 12, 2019
feinoujc added a commit to feinoujc/apollo-server that referenced this issue Feb 12, 2019
feinoujc added a commit to feinoujc/apollo-server that referenced this issue Feb 16, 2019
feinoujc added a commit to feinoujc/apollo-server that referenced this issue Feb 20, 2019
feinoujc added a commit to feinoujc/apollo-server that referenced this issue Apr 13, 2019
feinoujc added a commit to feinoujc/apollo-server that referenced this issue Apr 17, 2019
abernix pushed a commit that referenced this issue Apr 25, 2019
* [apollo-server-testing] add complete typing for ...rest args

fixes #2172

* simpler type definition

* Update CHANGELOG.md

* Update CHANGELOG.md
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 21, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants