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

Add more accurate typings to formatError #2343

Merged
merged 4 commits into from
Feb 21, 2019
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 @@ -7,6 +7,7 @@
- `apollo-server-lambda`: Fix typings which triggered "Module has no default export" errors. [PR #2230](https://github.com/apollographql/apollo-server/pull/2230)
- `apollo-server-koa`: Support OPTIONS requests [PR #2288](https://github.com/apollographql/apollo-server/pull/2288)
- Add `req` and `res` typings to the `ContextFunction` argument for apollo-server and apollo-server-express. Update `ContextFunction` return type to allow returning a value syncronously. [PR #2330](https://github.com/apollographql/apollo-server/pull/2330)
- Type the `formatError` function to accept an GraphQLError as an argument and return a GraphQLFormattedError [PR #2343](https://github.com/apollographql/apollo-server/pull/2343)

### v2.4.2

Expand Down
8 changes: 6 additions & 2 deletions packages/apollo-server-core/src/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { GraphQLExtension, GraphQLResponse } from 'graphql-extensions';
import { formatApolloErrors } from 'apollo-server-errors';
import { GraphQLError, GraphQLFormattedError } from 'graphql';

export class FormatErrorExtension<TContext = any> extends GraphQLExtension {
private formatError?: Function;
private formatError?: (error: GraphQLError) => GraphQLFormattedError;
private debug: boolean;

public constructor(formatError?: Function, debug: boolean = false) {
public constructor(
formatError?: (error: GraphQLError) => GraphQLFormattedError,
debug: boolean = false,
) {
super();
this.formatError = formatError;
this.debug = debug;
Expand Down
4 changes: 3 additions & 1 deletion packages/apollo-server-core/src/graphqlOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
ValidationContext,
GraphQLFieldResolver,
DocumentNode,
GraphQLError,
GraphQLFormattedError,
} from 'graphql';
import { GraphQLExtension } from 'graphql-extensions';
import { CacheControlExtensionOptions } from 'apollo-cache-control';
Expand Down Expand Up @@ -31,7 +33,7 @@ export interface GraphQLServerOptions<
TRootValue = any
> {
schema: GraphQLSchema;
formatError?: Function;
formatError?: (error: GraphQLError) => GraphQLFormattedError;
rootValue?: ((parsedQuery: DocumentNode) => TRootValue) | TRootValue;
context?: TContext | (() => never);
validationRules?: Array<(context: ValidationContext) => any>;
Expand Down
3 changes: 2 additions & 1 deletion packages/apollo-server-core/src/requestPipeline.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
ExecutionArgs,
ExecutionResult,
GraphQLError,
GraphQLFormattedError,
} from 'graphql';
import * as graphql from 'graphql';
import {
Expand Down Expand Up @@ -75,7 +76,7 @@ export interface GraphQLRequestPipelineConfig<TContext> {
persistedQueries?: PersistedQueryOptions;
cacheControl?: CacheControlExtensionOptions;

formatError?: Function;
formatError?: (error: GraphQLError) => GraphQLFormattedError;
formatResponse?: Function;

plugins?: ApolloServerPlugin[];
Expand Down
4 changes: 2 additions & 2 deletions packages/apollo-server-errors/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GraphQLError } from 'graphql';
import { GraphQLError, GraphQLFormattedError } from 'graphql';

export class ApolloError extends Error implements GraphQLError {
public extensions: Record<string, any>;
Expand Down Expand Up @@ -213,7 +213,7 @@ export class UserInputError extends ApolloError {
export function formatApolloErrors(
errors: Array<Error>,
options?: {
formatter?: Function;
formatter?: (error: GraphQLError) => GraphQLFormattedError;
debug?: boolean;
},
): Array<ApolloError> {
Expand Down