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

TestClient doesn't receive Request object #3158

Closed
lnmedrano opened this issue Aug 14, 2019 · 2 comments
Closed

TestClient doesn't receive Request object #3158

lnmedrano opened this issue Aug 14, 2019 · 2 comments

Comments

@lnmedrano
Copy link

Hello!

Use case

My API receives an authorization token in a header in the request (an ExpressRequest). When writing tests I'm creating a test-client with an Apollo Server.
The problem is that, since no Express -or any other- server is created, the executeOperation method from ApolloServer is only receiving the GraphqlRequest. The issue has been discussed here: #2277 and here: #2478.
I think it would be very useful to have the request available on the tests.

Workarounds

There are some workarounds here in other issues (like setting the context inside of tests - #2277 - , or getting the headers from the GraphqlRequest - #2478 -) but none of them solves the problem. Maybe, if the request is needed in tests, it is better to create an Apollo server with an Express -or any other- server, instead of using a testClient, but I think it would be better to have this option available in the testClient, so that is possible to do something like

createTestClient(
  new ApolloServer({
    schema,
    context: ({req}) => {
      doSomethingWithTheRequest(req);
      }
  })
);

Some possible solutions:

  • Create a server in every test and pass the query object to the executeOperation method. This should be user's responsibility.
  • Mock a request object in executeOperation with the information of the GraphqlRequest, but this mocked request should be different for each server.
  • Make 'context' protected and implement a new class ApolloServerBaseTesting with a setContext method.

I would like to know if this is intentional or if is there a possibility to improve it. In that case what do you think it would be a good solution?

apollo-server version: 2.4.8

@abernix
Copy link
Member

abernix commented Aug 26, 2019

I think you're right, but I think this still would be a duplicate of #2277. Please provide this input on that issue, rather than opening a new one!

@abernix
Copy link
Member

abernix commented Aug 26, 2019

I see you have left feedback there. I'll close this one, as I believe the other issue will suffice for tracking this.

@abernix abernix closed this as completed Aug 26, 2019
@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

No branches or pull requests

2 participants