Skip to content

Commit

Permalink
Add more accurate typings to formatError (apollographql#2343)
Browse files Browse the repository at this point in the history
* Type formatErrors function to specify that it accepts and returns ApolloError

* Update changelog

* type(formatErrors): accept GraphQLError, return GraphQLFormattedError

* update changelog
  • Loading branch information
cheapsteak committed Feb 21, 2019
1 parent f6cdc94 commit 8850e0f
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 6 deletions.
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

0 comments on commit 8850e0f

Please sign in to comment.