Skip to content

Commit

Permalink
fix: change use of instanceof Promise to use is-promise library (#…
Browse files Browse the repository at this point in the history
…1837)

* fix: wait for promise resolution before returning

* fix: update tests to reflect async result

* fix linting

* Revert "fix: wait for promise resolution before returning"

This reverts commit c59c9c4.

* fix: replace all `instanceof Promise` check with is-promise library

based on feedback:
#1837 (comment)
#1837 (comment)
  • Loading branch information
cajames authored Aug 10, 2020
1 parent f4bafe0 commit a19b809
Show file tree
Hide file tree
Showing 10 changed files with 28 additions and 13 deletions.
3 changes: 2 additions & 1 deletion packages/delegate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@
"@graphql-tools/schema": "6.0.16",
"@graphql-tools/utils": "6.0.16",
"@ardatan/aggregate-error": "0.0.1",
"is-promise": "4.0.0",
"tslib": "~2.0.0"
},
"publishConfig": {
"access": "public",
"directory": "dist"
}
}
}
4 changes: 3 additions & 1 deletion packages/delegate/src/delegateToSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import {
GraphQLObjectType,
} from 'graphql';

import isPromise from 'is-promise';

import { mapAsyncIterator, Transform, ExecutionResult } from '@graphql-tools/utils';

import { IDelegateToSchemaOptions, IDelegateRequestOptions, SubschemaConfig, ExecutionParams } from './types';
Expand Down Expand Up @@ -162,7 +164,7 @@ export function delegateRequest({
info,
});

if (executionResult instanceof Promise) {
if (isPromise(executionResult)) {
return executionResult.then(originalResult => transformer.transformResult(originalResult));
}
return transformer.transformResult(executionResult);
Expand Down
3 changes: 2 additions & 1 deletion packages/delegate/src/results/mergeFields.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FieldNode, SelectionNode, Kind, GraphQLResolveInfo, SelectionSetNode } from 'graphql';
import isPromise from 'is-promise';

import { MergedTypeInfo, SubschemaConfig } from '../types';

Expand Down Expand Up @@ -153,7 +154,7 @@ export function mergeFields(
delegationMap.forEach((selectionSet: SelectionSetNode, s: SubschemaConfig) => {
const maybePromise = s.merge[typeName].resolve(object, context, info, s, selectionSet);
maybePromises.push(maybePromise);
if (!containsPromises && maybePromise instanceof Promise) {
if (!containsPromises && isPromise(maybePromise)) {
containsPromises = true;
}
});
Expand Down
5 changes: 3 additions & 2 deletions packages/links/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@
"@graphql-tools/utils": "6.0.16",
"apollo-link": "1.2.14",
"apollo-upload-client": "14.1.1",
"form-data": "3.0.0",
"cross-fetch": "3.0.5",
"form-data": "3.0.0",
"is-promise": "4.0.0",
"tslib": "~2.0.0"
},
"publishConfig": {
"access": "public",
"directory": "dist"
}
}
}
5 changes: 3 additions & 2 deletions packages/links/src/GraphQLUpload.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { GraphQLScalarType, GraphQLError } from 'graphql';
import isPromise from 'is-promise';

const GraphQLUpload = new GraphQLScalarType({
name: 'Upload',
description: 'The `Upload` scalar type represents a file upload.',
parseValue: value => {
if (value != null && value.promise instanceof Promise) {
if (value != null && isPromise(value.promise)) {
// graphql-upload v10
return value.promise;
} else if (value instanceof Promise) {
} else if (isPromise(value)) {
// graphql-upload v9
return value;
}
Expand Down
3 changes: 2 additions & 1 deletion packages/stitch/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@
"@graphql-tools/schema": "6.0.16",
"@graphql-tools/utils": "6.0.16",
"@graphql-tools/wrap": "6.0.16",
"is-promise": "4.0.0",
"tslib": "~2.0.0"
},
"publishConfig": {
"access": "public",
"directory": "dist"
}
}
}
5 changes: 3 additions & 2 deletions packages/stitch/tests/fixtures/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GraphQLError,
GraphQLInterfaceType,
} from 'graphql';
import isPromise from 'is-promise';

import { introspectSchema } from '@graphql-tools/wrap';
import {
Expand Down Expand Up @@ -689,7 +690,7 @@ function makeExecutorFromSchema(schema: GraphQLSchema) {
context,
variables,
) as PromiseOrValue<ExecutionResult<TReturn>>;
if (result instanceof Promise) {
if (isPromise(result)) {
return result.then(originalResult => JSON.parse(JSON.stringify(originalResult)));
}
return JSON.parse(JSON.stringify(result));
Expand All @@ -705,7 +706,7 @@ function makeSubscriberFromSchema(schema: GraphQLSchema) {
context,
variables,
) as Promise<AsyncIterator<ExecutionResult<TReturn>> | ExecutionResult<TReturn>>;
if (result instanceof Promise) {
if (isPromise(result)) {
return result.then(asyncIterator =>
mapAsyncIterator(asyncIterator as AsyncIterator<ExecutionResult>, (originalResult: ExecutionResult<TReturn>) => JSON.parse(JSON.stringify(originalResult))));
}
Expand Down
3 changes: 2 additions & 1 deletion packages/wrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@
"@graphql-tools/schema": "6.0.16",
"@graphql-tools/utils": "6.0.16",
"aggregate-error": "3.0.1",
"is-promise": "4.0.0",
"tslib": "~2.0.0"
},
"publishConfig": {
"access": "public",
"directory": "dist"
}
}
}
5 changes: 3 additions & 2 deletions packages/wrap/tests/fixtures/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GraphQLError,
GraphQLInterfaceType,
} from 'graphql';
import isPromise from 'is-promise';

import { introspectSchema } from '../../src/introspect';
import {
Expand Down Expand Up @@ -689,7 +690,7 @@ function makeExecutorFromSchema(schema: GraphQLSchema) {
context,
variables,
) as PromiseOrValue<ExecutionResult<TReturn>>;
if (result instanceof Promise) {
if (isPromise(result)) {
return result.then(originalResult => JSON.parse(JSON.stringify(originalResult)));
}
return JSON.parse(JSON.stringify(result));
Expand All @@ -705,7 +706,7 @@ function makeSubscriberFromSchema(schema: GraphQLSchema) {
context,
variables,
) as Promise<AsyncIterator<ExecutionResult<TReturn>> | ExecutionResult<TReturn>>;
if (result instanceof Promise) {
if (isPromise(result)) {
return result.then(asyncIterator =>
mapAsyncIterator(asyncIterator as AsyncIterator<ExecutionResult>, (originalResult: ExecutionResult<TReturn>) => JSON.parse(JSON.stringify(originalResult))));
}
Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7770,6 +7770,11 @@ is-potential-custom-element-name@^1.0.0:
resolved "https://registry.yarnpkg.com/is-potential-custom-element-name/-/is-potential-custom-element-name-1.0.0.tgz#0c52e54bcca391bb2c494b21e8626d7336c6e397"
integrity sha1-DFLlS8yjkbssSUsh6GJtczbG45c=

[email protected]:
version "4.0.0"
resolved "https://registry.yarnpkg.com/is-promise/-/is-promise-4.0.0.tgz#42ff9f84206c1991d26debf520dd5c01042dd2f3"
integrity sha512-hvpoI6korhJMnej285dSg6nu1+e6uxs7zG3BYAm5byqDsgJNWwxzM6z6iZiAgQR4TJ30JmBTOwqZUw3WlyH3AQ==

is-regex@^1.0.4, is-regex@^1.0.5:
version "1.0.5"
resolved "https://registry.yarnpkg.com/is-regex/-/is-regex-1.0.5.tgz#39d589a358bf18967f726967120b8fc1aed74eae"
Expand Down

0 comments on commit a19b809

Please sign in to comment.