Skip to content

Commit

Permalink
Support for graphql@16 (#5857)
Browse files Browse the repository at this point in the history
The only change required is in `apollo-server-errors`.
Alternative to #5844

Co-authored-by: David Glasser <glasser@apollographql.com>
  • Loading branch information
IvanGoncharov and glasser committed Nov 5, 2021
1 parent 3640286 commit 13bf29e
Show file tree
Hide file tree
Showing 18 changed files with 80 additions and 17 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The version headers in this history reflect the versions of Apollo Server itself

## vNEXT

- Apollo Server now supports `graphql@16`. (There is a very small backwards incompatibility: `ApolloError.originalError` can no longer be `null`, matching the type of `GraphQLError.originalError`. Use `undefined` instead. If this causes challenges, let us know and we can try to adapt.) [PR #5857](https://github.com/apollographql/apollo-server/pull/5857)
`apollo-server-core`: Fix build error when building with `@rollup/plugin-commonjs`. [PR #5797](https://github.com/apollographql/apollo-server/pull/5797)
- `apollo-server-plugin-response-cache`: Add missing dependency on `apollo-server-types` (broken since v3.0.0). [Issue #5804](https://github.com/apollographql/apollo-server/issues/5804) [PR #5816](https://github.com/apollographql/apollo-server/pull/5816)
- `apollo-server-core`: The default landing page plugins now take `document`, `variables`, and `headers` arguments which fill in default values if you click through to Explorer. [PR #5711](https://github.com/apollographql/apollo-server/pull/5711)
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-azure-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@
"apollo-server-integration-testsuite": "file:../apollo-server-integration-testsuite"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-cloud-functions/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@
"apollo-server-integration-testsuite": "file:../apollo-server-integration-testsuite"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-cloudflare/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@
"apollo-server-types": "file:../apollo-server-types"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,6 @@
"uuid": "^8.0.0"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-errors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"node": ">=12.0"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
22 changes: 22 additions & 0 deletions packages/apollo-server-errors/src/__tests__/ApolloError.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,28 @@ describe('ApolloError', () => {
}),
).toThrow(/Pass extensions directly/);
});

it('provides toJSON method', () => {
const error = new ApolloError('My original message', 'A_CODE', {
arbitrary: 'user_data',
});

expect(error.toJSON()).toEqual({
message: 'My original message',
extensions: {
code: 'A_CODE',
arbitrary: 'user_data',
},
});
});

it('provides toString method', () => {
const error = new ApolloError('My original message', 'A_CODE', {
arbitrary: 'user_data',
});

expect(error.toString()).toEqual('My original message');
});
});

describe('ForbiddenError', () => {
Expand Down
44 changes: 42 additions & 2 deletions packages/apollo-server-errors/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ import {
GraphQLFormattedError,
Source,
SourceLocation,
printError,
formatError,
} from 'graphql';

declare module 'graphql' {
export interface GraphQLErrorExtensions {
exception?: {
code?: string;
stacktrace?: ReadonlyArray<string>;
};
}
}

// Note: We'd like to switch to `extends GraphQLError` and look forward to doing so
// as soon as we drop support for `graphql` bellow `v15.7.0`.
export class ApolloError extends Error implements GraphQLError {
public extensions: Record<string, any>;
override readonly name!: string;
Expand All @@ -14,7 +27,7 @@ export class ApolloError extends Error implements GraphQLError {
readonly source: Source | undefined;
readonly positions: ReadonlyArray<number> | undefined;
readonly nodes: ReadonlyArray<ASTNode> | undefined;
public originalError: Error | null | undefined;
public originalError: Error | undefined;

[key: string]: any;

Expand All @@ -40,6 +53,30 @@ export class ApolloError extends Error implements GraphQLError {

this.extensions = { ...extensions, code };
}

toJSON(): GraphQLFormattedError {
return formatError(toGraphQLError(this));
}

override toString(): string {
return printError(toGraphQLError(this));
}

get [Symbol.toStringTag](): string {
return this.name;
}
}

function toGraphQLError(error: ApolloError): GraphQLError {
return new GraphQLError(
error.message,
error.nodes,
error.source,
error.positions,
error.path,
error.originalError,
error.extensions,
);
}

function enrichError(error: Partial<GraphQLError>, debug: boolean = false) {
Expand Down Expand Up @@ -129,10 +166,13 @@ export function fromGraphQLError(error: GraphQLError, options?: ErrorOptions) {

// copy enumerable keys
Object.entries(error).forEach(([key, value]) => {
if (key === 'extensions') {
return; // extensions are handled bellow
}
copy[key] = value;
});

// extensions are non enumerable, so copy them directly
// merge extensions instead of just copying them
copy.extensions = {
...copy.extensions,
...error.extensions,
Expand Down
2 changes: 1 addition & 1 deletion packages/apollo-server-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@
},
"peerDependencies": {
"express": "^4.17.1",
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-fastify/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,6 @@
},
"peerDependencies": {
"fastify": "^3.17.0",
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-hapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
},
"peerDependencies": {
"@hapi/hapi": "^20.1.2",
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"apollo-server-integration-testsuite": "file:../apollo-server-integration-testsuite"
},
"peerDependencies": {
"graphql": "^15.3.0",
"graphql": "^15.3.0 || ^16.0.0",
"koa": "^2.13.1"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-lambda/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,6 @@
"apollo-server-integration-testsuite": "file:../apollo-server-integration-testsuite"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-plugin-base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@
"apollo-server-types": "file:../apollo-server-types"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@
"make-fetch-happen": "^8.0.9"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-plugin-response-cache/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@
"apollo-server-types": "file:../apollo-server-types"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"apollo-server-env": "file:../apollo-server-env"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}
2 changes: 1 addition & 1 deletion packages/apollo-server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@
"apollo-server-integration-testsuite": "file:../apollo-server-integration-testsuite"
},
"peerDependencies": {
"graphql": "^15.3.0"
"graphql": "^15.3.0 || ^16.0.0"
}
}

0 comments on commit 13bf29e

Please sign in to comment.