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

✨ execute operation with external context #4166

Merged
merged 2 commits into from Mar 25, 2021

Conversation

chengcyber
Copy link
Contributor

Use case

  1. create server via apollo-server-koa, context is a function which injects Koa.Context
import { ApolloServer } from 'apollo-server-koa';

const server = new ApolloServer({
  schema // ...
  context(ctx) {
    return {
      service: ctx.service
    };
  },
 // ...
})
  1. execute operation with the server, lose Koa.Context...
server.executeOperation({
  query: `` // ...
})

How to solve

server.executeOperation(
  {
    query: `` // ...
  }, 
  /* provide koa context manually */
  ctx
)

Why

integration apollo server like apollo-server-koa extends ApolloServerBase, and leverage graphQLServerOptions to inject Koa context

  async createGraphQLServerOptions(ctx: Koa.Context): Promise<GraphQLOptions> {
    return super.graphQLServerOptions({ ctx });
  }

while, executeOperation is a instance method of ApolloServerBase

  public async executeOperation(request: GraphQLRequest) {
    const options = await this.graphQLServerOptions(); // no way to provide koa context here...

After this PR merged, there is a way for executeOperation to provide integration context (koa context here).

Related issue

#2886

@apollo-cla
Copy link

@chengcyber: Thank you for submitting a pull request! Before we can merge it, you'll need to sign the Apollo Contributor License Agreement here: https://contribute.apollographql.com/

@abernix abernix closed this Jun 24, 2020
@abernix abernix reopened this Jun 25, 2020
@abernix abernix changed the base branch from master to main June 25, 2020 09:02
@glasser glasser merged commit a5c6bcd into apollographql:main Mar 25, 2021
@glasser
Copy link
Member

glasser commented Mar 25, 2021

Thanks for the PR! Sorry for the delay. I added a comment on the method and a CHANGELOG entry.

@glasser
Copy link
Member

glasser commented Apr 9, 2021

I've released a prerelease with this fix, version 2.23.0-alpha.0. Try out the alpha and see if it works for you! Please provide any feedback in #5094.

@glasser
Copy link
Member

glasser commented Apr 14, 2021

This is released in Apollo Server 2.23.0.

glasser added a commit that referenced this pull request 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 pull request 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 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 this pull request may close these issues.

None yet

4 participants