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

Lint error for createTestClient #4724

Closed
extjbhlego opened this issue Nov 9, 2020 · 3 comments
Closed

Lint error for createTestClient #4724

extjbhlego opened this issue Nov 9, 2020 · 3 comments

Comments

@extjbhlego
Copy link

Hello 👋

Using the createTestClient method upsets our linting rule eslint@typescript-eslint/unbound-method, documented here

Looking at the implementation, I suspect it has to do with
const executeOperation = server.executeOperation.bind(server); on line 31

"apollo-server-testing": "2.19.0"

  • The expected behavior
    No warnings

  • The actual behavior
    Warning

@lauriharpf
Copy link

Have also encountered this issue with apollo-server-testing 2.24.0. Repository with a reproduction at https://github.com/lauriharpf/reproduce-create-test-client-linting-issue .

It looks like adjusting the typing of ApolloServerTestClient would remove the linter error. Could that be enough since the createTestClient function is not part of a class?

@glasser
Copy link
Member

glasser commented May 18, 2021

apollo-server-testing is merely a very thin wrapper around ApolloServer.executeOperation that introduces its own new API to learn for no apparent (to me) benefit. I'm currently planning to (in the next few days) update our docs to remove references to apollo-server-testing and just document executeOperation instead, and then remove apollo-server-testing from the forthcoming AS 3.0. See #4952 for details.

(There is one minor advantage that apollo-server-testing has over executeOperation: you can pass in either a string or a parsed document, whereas executeOperation only accepts a string. I will probably change executeOperation to have that property as well.)

I'd suggest as a "workaround" you just stop using apollo-server-testing today. Replace this:

import { createTestClient } from 'apollo-server-testing';

const { query } = createTestClient(server);
query({ query: SOME_QUERY });

with

server.executeOperation({query: SOME_QUERY});

(If SOME_QUERY was a literal gql document, change it to a string, or if you want to keep it as a document in your code, pass it to print from graphql first. As mentioned above I will hopefully make this unnecessary soon.)

Let me know if this isn't an acceptable alternative and it may feed into the plans described above!

@glasser glasser closed this as completed May 18, 2021
lauriharpf added a commit to lauriharpf/type-graphql-starter that referenced this issue May 22, 2021
lauriharpf added a commit to lauriharpf/type-graphql-starter that referenced this issue May 22, 2021
@lauriharpf
Copy link

Thank you for the explanation and the instructions, the suggested approach works well for me. Looking forward to the upcoming updates! 👍

glasser added a commit that referenced this issue May 25, 2021
The `apollo-server-testing` package exports one small function which is
just a tiny wrapper around `server.executeOperation`. The one main
advantage it provides is that you can pass in operations as ASTs rather
than only as strings.

This extra layer doesn't add much value but does require us to update
things in two places (which cross a package barrier and thus can be
installed at skewed versions). So for example when adding the second
argument to `executeOperation` in #4166 I did not bother to add it to
`apollo-server-testing` too. We've also found that users have been
confused by the `createTestClient` API (eg #5111) and that some linters
get confused by the unbound methods it returns (#4724).

So the simplest thing is to just teach people how to use the real
`ApolloServer` method instead of an unrelated API.

This PR allows you to pass an AST to `server.executeOperation` (just
like with the `apollo-server-testing` API), and changes the docs to
recommend `executeOperation` instead of `apollo-server-testing`. It also
makes some other suggestions about how to test Apollo Server code in a
more end-to-end fashion, and adds some basic tests for
`executeOperation`.

Fixes #4952.
glasser added a commit that referenced this issue May 25, 2021
The `apollo-server-testing` package exports one small function which is
just a tiny wrapper around `server.executeOperation`. The one main
advantage it provides is that you can pass in operations as ASTs rather
than only as strings.

This extra layer doesn't add much value but does require us to update
things in two places (which cross a package barrier and thus can be
installed at skewed versions). So for example when adding the second
argument to `executeOperation` in #4166 I did not bother to add it to
`apollo-server-testing` too. We've also found that users have been
confused by the `createTestClient` API (eg #5111) and that some linters
get confused by the unbound methods it returns (#4724).

So the simplest thing is to just teach people how to use the real
`ApolloServer` method instead of an unrelated API.

This PR allows you to pass an AST to `server.executeOperation` (just
like with the `apollo-server-testing` API), and changes the docs to
recommend `executeOperation` instead of `apollo-server-testing`. It also
makes some other suggestions about how to test Apollo Server code in a
more end-to-end fashion, and adds some basic tests for
`executeOperation`.

Fixes #4952.
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Apr 20, 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.

3 participants