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

fix(client): graphql module is not required for runtime #102

Merged
merged 1 commit into from
Jul 8, 2023
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
41 changes: 33 additions & 8 deletions src/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,7 @@ import {
GraphQLError,
} from 'graphql';
import { RequestParams } from './common';
import {
areGraphQLErrors,
isAsyncIterable,
isExecutionResult,
isGraphQLError,
isObject,
jsonErrorReplacer,
} from './utils';
import { isAsyncIterable, isExecutionResult, isObject } from './utils';

/**
* The incoming request headers the implementing server should provide.
Expand Down Expand Up @@ -854,3 +847,35 @@ function getHeader(
}
return Object(req.headers)[key];
}

function areGraphQLErrors(obj: unknown): obj is readonly GraphQLError[] {
return (
Array.isArray(obj) &&
obj.length > 0 &&
// if one item in the array is a GraphQLError, we're good
obj.some(isGraphQLError)
);
}

function isGraphQLError(obj: unknown): obj is GraphQLError {
return obj instanceof GraphQLError;
}

function jsonErrorReplacer(
_key: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
val: any,
) {
if (
val instanceof Error &&
// GraphQL errors implement their own stringer
!isGraphQLError(val)
) {
return {
// name: val.name, name is included in message
message: val.message,
// stack: val.stack, can leak sensitive details
};
}
return val;
}
36 changes: 0 additions & 36 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import type { ExecutionResult } from 'graphql';
import { GraphQLError } from 'graphql';

/** @private */
export function extendedTypeof(
Expand Down Expand Up @@ -39,21 +38,6 @@ export function isObject(val: unknown): val is Record<
return typeof val === 'object' && val !== null;
}

/** @private */
export function areGraphQLErrors(obj: unknown): obj is readonly GraphQLError[] {
return (
Array.isArray(obj) &&
obj.length > 0 &&
// if one item in the array is a GraphQLError, we're good
obj.some(isGraphQLError)
);
}

/** @private */
export function isGraphQLError(obj: unknown): obj is GraphQLError {
return obj instanceof GraphQLError;
}

/** @private */
export function isExecutionResult(val: unknown): val is ExecutionResult {
return (
Expand All @@ -68,23 +52,3 @@ export function isAsyncIterable<T = unknown>(
): val is AsyncIterable<T> {
return typeof Object(val)[Symbol.asyncIterator] === 'function';
}

/** @private */
export function jsonErrorReplacer(
_key: string,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
val: any,
) {
if (
val instanceof Error &&
// GraphQL errors implement their own stringer
!isGraphQLError(val)
) {
return {
// name: val.name, name is included in message
message: val.message,
// stack: val.stack, can leak sensitive details
};
}
return val;
}