You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Deprecate apollo-server-testing; allow ASTs for executeOperation (#5238)
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.
Copy file name to clipboardExpand all lines: CHANGELOG.md
+2Lines changed: 2 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -13,6 +13,8 @@ The version headers in this history reflect the versions of Apollo Server itself
13
13
14
14
-`apollo-server-core`: Fix a race condition where schema reporting could lead to a delay at process shutdown. [PR #5222](https://github.com/apollographql/apollo-server/pull/5222)
15
15
-`apollo-server-core`: Allow the Fetch API implementation to be overridden for the schema reporting and usage reporting plugins via a new `fetcher` option. [PR #5179](https://github.com/apollographql/apollo-server/pull/5179)
16
+
-`apollo-server-core`: The `server.executeOperation` method (designed for testing) can now take its `query` as a `DocumentNode` (eg, a `gql`-tagged string) in addition to as a string. (This matches the behavior of the `apollo-server-testing``createTestClient` function which is now deprecated.) We now recommend this method instead of `apollo-server-testing` in our docs. [Issue #4952](https://github.com/apollographql/apollo-server/issues/4952)
17
+
-`apollo-server-testing`: Replace README with a deprecation notice explaining how to use `server.executeOperation` instead. [Issue #4952](https://github.com/apollographql/apollo-server/issues/4952)
Copy file name to clipboardExpand all lines: docs/source/testing/testing.md
+46-21Lines changed: 46 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,33 +3,26 @@ title: Integration testing
3
3
description: Utilities for testing Apollo Server
4
4
---
5
5
6
-
Testing `apollo-server` can be done in many ways. The `apollo-server-testing` package provides tooling to make testing easier and accessible to users of all of the `apollo-server` integrations.
6
+
Testing `apollo-server` can be done in many ways. One simple way is to use ApolloServer's `executeOperation` method to directly execute a GraphQL operation without going through a full HTTP operation.
7
7
8
-
## `createTestClient`
8
+
## `executeOperation`
9
9
10
-
Integration testing a GraphQL server means testing many things. `apollo-server` has a request pipeline that can support many plugins that can affect the way an operation is executed. `createTestClient` provides a single hook to run operations through the request pipeline, enabling the most thorough tests possible without starting up an HTTP server.
10
+
Integration testing a GraphQL server means testing many things. `apollo-server` has a request pipeline that can support many plugins that can affect the way an operation is executed. The `executeOperation` method provides a single hook to run operations through the request pipeline, enabling the most thorough tests possible without starting up an HTTP server.
When passed an instance of the `ApolloServer` class, `createTestClient` returns a `query` and `mutate` function that can be used to run operations against the server instance. Currently, queries and mutations are the only operation types supported by `createTestClient`.
23
+
For example, you can set up a full server with your schema and resolvers and run an operation against it.
This is an example of a full integration test being run against a test instance of `apollo-server`. This test imports the important pieces to test (`typeDefs`, `resolvers`, `dataSources`) and creates a new instance of `apollo-server`. Once an instance is created, it's passed to `createTestClient` which returns `{ query, mutate }`. These methods can then be used to execute operations against the server.
53
+
This is an example of a full integration test being run against a test instance of `apollo-server`. This test imports the important pieces to test (`typeDefs`, `resolvers`, `dataSources`) and creates a new instance of `apollo-server`.
54
+
55
+
The example above shows writing a test-specific [`context` function](../data/resolvers/#the-context-argument) which provides data directly instead of calculating it from the request context. If you'd like to use your server's real `context` function, you can pass a second argument to `executeOperation` which will be passed to your `context` function as its argument. You will need to put to gether an object with the [middleware-specific context fields](../api/apollo-server/#middleware-specific-context-fields) yourself.
56
+
57
+
You can use `executeOperation` to execute queries and mutations. Because the interface matches the GraphQL HTTP protocol, you specify the operation text under the `query` key even if the operation is a mutation. You can specify `query` either as a string or as a `DocumentNode` (an AST created by the `gql`tag).
58
+
59
+
Inadditionto`query`, the first argument to `executeOperation` can take `operationName`, `variables`, `extensions`, and `http` keys.
60
+
61
+
Note that errors in parsing, validating, and executing your operation are returned in the `errors` field of the result (just like in a GraphQL response) rather than thrown.
62
+
63
+
## `createTestClient` and `apollo-server-testing`
64
+
65
+
There is also a package called `apollo-server-testing` which exports a function `createTestClient` which wraps `executeOperation`. This API does not support the second context-function-argument argument, and doesn't provide any real advantages over calling `executeOperation` directly. It is deprecated and will no longer be published with Apollo Server 3.
Instead of bypassing the HTTP layer, you may just want to fully run your server and test it with a real HTTP client.
63
88
64
-
For more examples of this tool in action, check out the [integration tests](https://github.com/apollographql/fullstack-tutorial/blob/master/final/server/src/__tests__/integration.js) in the [Fullstack Tutorial](https://www.apollographql.com/docs/tutorial/introduction.html).
89
+
Apollo Server doesn't have any built-in support for this. You can combine any HTTP or GraphQL client such as [`supertest`](https://www.npmjs.com/package/supertest) or [Apollo Client's HTTP Link](https://www.apollographql.com/docs/react/api/link/apollo-link-http/) to run operations against your server. There are also community packages available such as [`apollo-server-integration-testing`](https://www.npmjs.com/package/apollo-server-integration-testing) which provides an API similar to the deprecated `apollo-server-testing` package which uses mocked Express request and response objects.
0 commit comments