1

I am trying to setup testing for a node.js / typescript / apollo-server / typegrapql / typeorm project using jest, ts-jest and apollo-server-testing. My test server and client are setup like this:

import 'dotenv/config';
import { ApolloServer } from 'apollo-server-express';
import connectToDatabase from '../database/createConnection';
import buildApolloSchema from './buildApolloSchema';
import { createTestClient } from 'apollo-server-testing';


let server: ApolloServer;
let query: any; // <- Type here??
let mutate: any; // <- Type here??

export { server, query, mutate };

const setupTestEnvironment = async () => {

  await connectToDatabase();
  const schema = await buildApolloSchema();

  if (schema) {
    const server = new ApolloServer({
      schema,
      context: ({ req, res }) => ({
        req,
        res,
      }),
    });

    const testClient = createTestClient(server);
    query = testClient.query; // <- Error here: Avoid referencing unbound methods...
    mutate = testClient.mutate; // <- Error here: Avoid referencing unbound methods...
  }
};

Any idea of how can I type variables query and mutate to avoid implicit any errors? There seems to be some type inference but I don't seem to be able to import types from the apollo-server-testing package and so far I have left them as any (quickfix suggests never).

Right now the assignments at the end are also throwing the error Avoid referencing unbound methods which may cause unintentional scoping of this.

enter image description here

I have tried all sorts of destructuring assignments (including const {query, mutate} = createTestClient(server) as it appears in the docs) and I get the same error.

Any clues?

1 Answer 1

2

You should be able to type query and mutate with the types from apollo-server-testing like this:

import {
  ApolloServerTestClient,
  createTestClient,
} from "apollo-server-testing";

let server: ApolloServer;
let query: ApolloServerTestClient["query"];
let mutate: ApolloServerTestClient["mutate"];

The Avoid referencing unbound methods which may cause unintentional scoping of this. error is related to the way the types of createTestClient are defined. Since that's apollo-server-testing code, that's hard for you to resolve. Consider disabling the rule for these lines:

    const testClient = createTestClient(server);
    // eslint-disable-next-line @typescript-eslint/unbound-method
    query = testClient.query;
    // eslint-disable-next-line  @typescript-eslint/unbound-method
    mutate = testClient.mutate;

The root of the issue seems to be this definition in apollo-server-testing:

export interface ApolloServerTestClient {
    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>>;
}

If the definition would be like this, the rule shouldn't trigger:

export interface ApolloServerTestClient {
  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>>;
}

Edit: Related to issue 4724 in apollo-server-testing

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.