From 12d76c5cad69b114ab385dd62bdee65e1032c22d Mon Sep 17 00:00:00 2001 From: Ivaylo Bratoev Date: Tue, 23 Feb 2021 11:46:33 +0200 Subject: [PATCH 1/2] fix: executeOperation passes the debug option Fix the ApolloServerBase.executeOperation to pass the debug option in the request context. This change resolves an issue with `apollo-server-testing` that does not return stacktraces with debug option enabled. Add 2 unit tests with and without the debug option. fixes #4107 --- .../apollo-server-core/src/ApolloServer.ts | 1 + .../src/__tests__/ApolloServerBase.test.ts | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/packages/apollo-server-core/src/ApolloServer.ts b/packages/apollo-server-core/src/ApolloServer.ts index b9aec5fd2d0..49ae83deb6f 100644 --- a/packages/apollo-server-core/src/ApolloServer.ts +++ b/packages/apollo-server-core/src/ApolloServer.ts @@ -1015,6 +1015,7 @@ export class ApolloServerBase { headers: new Headers(), }, }, + debug: options.debug, }; return processGraphQLRequest(options, requestCtx); diff --git a/packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts b/packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts index e71e42ef9d4..ecd00afd2d9 100644 --- a/packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts +++ b/packages/apollo-server-core/src/__tests__/ApolloServerBase.test.ts @@ -6,6 +6,7 @@ import { Logger } from 'apollo-server-types'; const typeDefs = gql` type Query { hello: String + error: Boolean } `; @@ -14,6 +15,9 @@ const resolvers = { hello() { return 'world'; }, + error() { + throw new Error('A test error'); + }, }, }; @@ -97,6 +101,36 @@ describe('ApolloServerBase construction', () => { }); }); +describe('ApolloServerBase executeOperation', () => { + it('returns error information without details by default', async () => { + const server = new ApolloServerBase({ + typeDefs, + resolvers, + }); + + const result = await server.executeOperation({ query: 'query { error }' }); + + expect(result.errors).toHaveLength(1); + expect(result.errors?.[0].extensions).toStrictEqual({ + code: 'INTERNAL_SERVER_ERROR', + }); + }); + + it('returns error information with details when debug is enabled', async () => { + const server = new ApolloServerBase({ + typeDefs, + resolvers, + debug: true, + }); + + const result = await server.executeOperation({ query: 'query { error }' }); + + expect(result.errors).toHaveLength(1); + expect(result.errors?.[0].extensions?.code).toBe('INTERNAL_SERVER_ERROR'); + expect(result.errors?.[0].extensions?.exception?.stacktrace).toBeDefined(); + }); +}); + describe('environment variables', () => { const OLD_ENV = process.env; From 2ef1180fae23f92fac5e2fc6217f5f427753e2fd Mon Sep 17 00:00:00 2001 From: David Glasser Date: Tue, 23 Feb 2021 15:39:02 -0800 Subject: [PATCH 2/2] Update CHANGELOG.md --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1bb3925928d..a1c519f7db2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,8 @@ The version headers in this history reflect the versions of Apollo Server itself > The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. With few exceptions, the format of the entry should follow convention (i.e., prefix with package name, use markdown `backtick formatting` for package names and code, suffix with a link to the change-set à la `[PR #YYY](https://link/pull/YYY)`, etc.). When a release is being prepared, a new header will be (manually) created below and the appropriate changes within that release will be moved into the new section. +- The `debug` option to `new ApolloServer` (which adds stack traces to errors) now affects errors that come from requests executed with `server.executeOperation` (and its wrapper `apollo-server-testing`), instead of just errors that come from requests executed over HTTP. [Issue #4107](https://github.com/apollographql/apollo-server/issues/4107) [PR #4948](https://github.com/apollographql/apollo-server/pull/4948) + ## v2.21.0 - Apollo Server can now be installed with `graphql@15` without causing peer dependency errors or warnings. (Apollo Server has a [file upload](https://www.apollographql.com/docs/apollo-server/data/file-uploads/) feature which was implemented as a wrapper around the `graphql-upload` package. We have been unable to upgrade our dependency on that package due to backwards-incompatible changes in later versions, and the version we were stuck on did not allow `graphql@15` as a peer dependency. We have now switched to a fork of that old version called `@apollographql/graphql-upload-8-fork` that allows `graphql@15`.) Also bump the `graphql-tools` dependency from 4.0.0 to 4.0.8 for `graphql@15` support. [Issue #4865](https://github.com/apollographql/apollo-server/issues/4865)