From a19b809315cb3bc62e086797bb0decc068905eca Mon Sep 17 00:00:00 2001 From: Chris James <1523286+cajames@users.noreply.github.com> Date: Mon, 10 Aug 2020 18:31:07 +1000 Subject: [PATCH] fix: change use of `instanceof Promise` to use `is-promise` library (#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 c59c9c4ebf1def2f864c8b19e74f7bce4a0ffcfb. * fix: replace all `instanceof Promise` check with is-promise library based on feedback: https://github.com/ardatan/graphql-tools/pull/1837#issuecomment-664947208 https://github.com/ardatan/graphql-tools/pull/1837#issuecomment-664942062 --- packages/delegate/package.json | 3 ++- packages/delegate/src/delegateToSchema.ts | 4 +++- packages/delegate/src/results/mergeFields.ts | 3 ++- packages/links/package.json | 5 +++-- packages/links/src/GraphQLUpload.ts | 5 +++-- packages/stitch/package.json | 3 ++- packages/stitch/tests/fixtures/schemas.ts | 5 +++-- packages/wrap/package.json | 3 ++- packages/wrap/tests/fixtures/schemas.ts | 5 +++-- yarn.lock | 5 +++++ 10 files changed, 28 insertions(+), 13 deletions(-) diff --git a/packages/delegate/package.json b/packages/delegate/package.json index 805a9afda83..cbec4ac71fc 100644 --- a/packages/delegate/package.json +++ b/packages/delegate/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/packages/delegate/src/delegateToSchema.ts b/packages/delegate/src/delegateToSchema.ts index 5051e7b3abf..0fd0908e807 100644 --- a/packages/delegate/src/delegateToSchema.ts +++ b/packages/delegate/src/delegateToSchema.ts @@ -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'; @@ -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); diff --git a/packages/delegate/src/results/mergeFields.ts b/packages/delegate/src/results/mergeFields.ts index d36e8d5eb50..a84773dd47c 100644 --- a/packages/delegate/src/results/mergeFields.ts +++ b/packages/delegate/src/results/mergeFields.ts @@ -1,4 +1,5 @@ import { FieldNode, SelectionNode, Kind, GraphQLResolveInfo, SelectionSetNode } from 'graphql'; +import isPromise from 'is-promise'; import { MergedTypeInfo, SubschemaConfig } from '../types'; @@ -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; } }); diff --git a/packages/links/package.json b/packages/links/package.json index 65f5f2d8fb5..b6630ce0884 100644 --- a/packages/links/package.json +++ b/packages/links/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/packages/links/src/GraphQLUpload.ts b/packages/links/src/GraphQLUpload.ts index 62493f4bac4..a33701c9cb8 100644 --- a/packages/links/src/GraphQLUpload.ts +++ b/packages/links/src/GraphQLUpload.ts @@ -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; } diff --git a/packages/stitch/package.json b/packages/stitch/package.json index 7dea9755e8d..331ba253a90 100644 --- a/packages/stitch/package.json +++ b/packages/stitch/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/packages/stitch/tests/fixtures/schemas.ts b/packages/stitch/tests/fixtures/schemas.ts index 09a5365d3e0..cf0798c64c9 100644 --- a/packages/stitch/tests/fixtures/schemas.ts +++ b/packages/stitch/tests/fixtures/schemas.ts @@ -11,6 +11,7 @@ import { GraphQLError, GraphQLInterfaceType, } from 'graphql'; +import isPromise from 'is-promise'; import { introspectSchema } from '@graphql-tools/wrap'; import { @@ -689,7 +690,7 @@ function makeExecutorFromSchema(schema: GraphQLSchema) { context, variables, ) as PromiseOrValue>; - if (result instanceof Promise) { + if (isPromise(result)) { return result.then(originalResult => JSON.parse(JSON.stringify(originalResult))); } return JSON.parse(JSON.stringify(result)); @@ -705,7 +706,7 @@ function makeSubscriberFromSchema(schema: GraphQLSchema) { context, variables, ) as Promise> | ExecutionResult>; - if (result instanceof Promise) { + if (isPromise(result)) { return result.then(asyncIterator => mapAsyncIterator(asyncIterator as AsyncIterator, (originalResult: ExecutionResult) => JSON.parse(JSON.stringify(originalResult)))); } diff --git a/packages/wrap/package.json b/packages/wrap/package.json index 53130d28bf8..e8d0a45b3e0 100644 --- a/packages/wrap/package.json +++ b/packages/wrap/package.json @@ -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" } -} \ No newline at end of file +} diff --git a/packages/wrap/tests/fixtures/schemas.ts b/packages/wrap/tests/fixtures/schemas.ts index bf2fe7df7e0..a13a9099499 100644 --- a/packages/wrap/tests/fixtures/schemas.ts +++ b/packages/wrap/tests/fixtures/schemas.ts @@ -11,6 +11,7 @@ import { GraphQLError, GraphQLInterfaceType, } from 'graphql'; +import isPromise from 'is-promise'; import { introspectSchema } from '../../src/introspect'; import { @@ -689,7 +690,7 @@ function makeExecutorFromSchema(schema: GraphQLSchema) { context, variables, ) as PromiseOrValue>; - if (result instanceof Promise) { + if (isPromise(result)) { return result.then(originalResult => JSON.parse(JSON.stringify(originalResult))); } return JSON.parse(JSON.stringify(result)); @@ -705,7 +706,7 @@ function makeSubscriberFromSchema(schema: GraphQLSchema) { context, variables, ) as Promise> | ExecutionResult>; - if (result instanceof Promise) { + if (isPromise(result)) { return result.then(asyncIterator => mapAsyncIterator(asyncIterator as AsyncIterator, (originalResult: ExecutionResult) => JSON.parse(JSON.stringify(originalResult)))); } diff --git a/yarn.lock b/yarn.lock index 92164b5b0a2..8002ea72e11 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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= +is-promise@4.0.0: + 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"