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

Support for graphql@16 #5857

Merged
merged 4 commits into from
Nov 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was useful.
I discovered a bug in graphql-js for toString we should follow Error.toString behavior so it should be ApolloError: My original message.

> e = new Error('test')
> e.name = 'TEST_NAME'
> e.toString()
'TEST_NAME: test'

I will fix it in the 16.0.x release, I don't think it would be a breaking change for both graphql-js and AS since I just added GraphQLError.toString in v16.0.0.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. If you're planning to change this in graphql-js, maybe just make this test accept either value? Or we can fix the test in the next graphql-js Renovate PR.

});
});

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 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be nice to throw in a comment noting that we'd like to switch to extends GraphQLError and look forward to doing so as soon as we drop support for <15.7.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done ✅

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;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To check my understanding: this is a safe change (that won't break users of graphql@15) because it's OK for an implementation of an interface to declare that a field has a type that is a subtype of the supertype's type?

Or I guess this is specifically the case because the interface (in v15) declared the field as readonly, which means that while ApolloError allows you to write e.originalError = null, that's unrelated to the GraphQLError declaration. (Because readonly just means "this interface doesn't provide the ability for you to change the field", not "the implementing type must forbid you from changing the field".)

Right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glasser Basically, we need to do this change to be compatible with v16.
From a purely theoretical position, it's a breaking change but I just can't figure out how to work around it.
In practical terms, I don't think it will break anyone especially since runtime behavior is the same.
Worst-case scenario library author (don't expect end-users to mess with originalError) need to change e.originalError = null to e.originalError = undefined.

Happy to replace it with some workaround that is less breaking I just can't come up with something right now.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah right, I was focused just on "will this work with graphql 15" and not "is this a backwards incompatible change for users". But I think it's fine. We'll make this a minor update at least.


[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));
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved
}

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
IvanGoncharov marked this conversation as resolved.
Show resolved Hide resolved
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"
}
}