From c767df013b9e5abb30165759eebaefcaf1a89639 Mon Sep 17 00:00:00 2001 From: Arda TANRIKULU Date: Thu, 21 Jan 2021 00:17:33 +0300 Subject: [PATCH] fix(fs): fix fs handling issues for non Node environments --- .changeset/fast-pets-dance.md | 13 +++++++++++++ examples/openapi-react-weatherbit/.env | 1 + .../src/mesh/getMeshInstance.ts | 17 ++++++++--------- packages/cache/file/package.json | 1 + packages/cli/src/commands/serve/playground.ts | 2 +- packages/cli/src/index.ts | 2 +- packages/handlers/json-schema/src/index.ts | 2 +- packages/handlers/postgraphile/src/index.ts | 2 +- packages/runtime/src/get-mesh.ts | 1 + packages/runtime/src/types.ts | 2 +- .../transforms/snapshot/test/snapshot.spec.ts | 2 +- packages/types/src/config-schema.json | 11 +++++++---- packages/types/src/config.ts | 11 ++++++++++- packages/utils/src/fs-operations.ts | 2 +- packages/utils/src/read-file-or-url.ts | 2 +- 15 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 .changeset/fast-pets-dance.md create mode 100644 examples/openapi-react-weatherbit/.env diff --git a/.changeset/fast-pets-dance.md b/.changeset/fast-pets-dance.md new file mode 100644 index 0000000000000..27474392486b0 --- /dev/null +++ b/.changeset/fast-pets-dance.md @@ -0,0 +1,13 @@ +--- +'openapi-react-weatherbit': patch +'@graphql-mesh/cache-file': patch +'@graphql-mesh/cli': patch +'@graphql-mesh/json-schema': patch +'@graphql-mesh/postgraphile': patch +'@graphql-mesh/runtime': patch +'@graphql-mesh/transform-snapshot': patch +'@graphql-mesh/types': patch +'@graphql-mesh/utils': patch +--- + +fix(fs): fix fs handling issues for non Node environments diff --git a/examples/openapi-react-weatherbit/.env b/examples/openapi-react-weatherbit/.env new file mode 100644 index 0000000000000..6f809cc2540d7 --- /dev/null +++ b/examples/openapi-react-weatherbit/.env @@ -0,0 +1 @@ +SKIP_PREFLIGHT_CHECK=true diff --git a/examples/openapi-react-weatherbit/src/mesh/getMeshInstance.ts b/examples/openapi-react-weatherbit/src/mesh/getMeshInstance.ts index 823271c390e95..242639b035e72 100644 --- a/examples/openapi-react-weatherbit/src/mesh/getMeshInstance.ts +++ b/examples/openapi-react-weatherbit/src/mesh/getMeshInstance.ts @@ -1,27 +1,29 @@ import { getMesh } from '@graphql-mesh/runtime'; import OpenAPIHandler from '@graphql-mesh/openapi'; import BareMerger from '@graphql-mesh/merger-bare'; -import { MeshPubSub } from '@graphql-mesh/types'; import { KeyValueCache } from '@graphql-mesh/types'; import { PubSub } from 'graphql-subscriptions'; import CacheTransform from '@graphql-mesh/transform-cache'; export function getMeshInstance({ cache }: { cache: KeyValueCache }) { - const pubsub = new PubSub() as MeshPubSub; + const meshContext = { + cache, + pubsub: new PubSub(), + }; return getMesh({ sources: [ { name: 'Weatherbit', handler: new OpenAPIHandler({ name: 'Weatherbit', - cache, - pubsub, config: { source: 'https://www.weatherbit.io/static/swagger.json', }, + ...meshContext, }), transforms: [ new CacheTransform({ + apiName: 'Weatherbit', config: [ { field: 'Query.getForecastDailyLatequalToLatLonLon', @@ -30,15 +32,12 @@ export function getMeshInstance({ cache }: { cache: KeyValueCache }) { }, }, ], - cache, - pubsub, - apiName: 'Weatherbit', + ...meshContext, }), ], }, ], - cache, - pubsub, merger: BareMerger, // we can use BareMerger since we don't need a real merger at all + ...meshContext, }); } diff --git a/packages/cache/file/package.json b/packages/cache/file/package.json index 76eacfdbbc2ef..53c5a1f189406 100644 --- a/packages/cache/file/package.json +++ b/packages/cache/file/package.json @@ -14,6 +14,7 @@ }, "dependencies": { "@graphql-mesh/types": "0.28.0", + "@graphql-mesh/utils": "0.8.3", "dataloader": "2.0.0", "lodash": "4.17.20" }, diff --git a/packages/cli/src/commands/serve/playground.ts b/packages/cli/src/commands/serve/playground.ts index 661ca231e6358..37644d177b528 100644 --- a/packages/cli/src/commands/serve/playground.ts +++ b/packages/cli/src/commands/serve/playground.ts @@ -5,7 +5,7 @@ import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'; import { basename, resolve } from 'path'; import { promises as fsPromises } from 'fs'; -const { readFile } = fsPromises; +const { readFile } = fsPromises || {}; export function playground(exampleQuery: string, graphqlPath: string): RequestHandler { return async (req: Request, res: Response, next) => { diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 6b447b8dec277..268ab5a3428e5 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -11,7 +11,7 @@ import { introspectionFromSchema } from 'graphql'; import { printSchemaWithDirectives } from '@graphql-tools/utils'; export { generateSdk, serveMesh }; -const { writeFile } = fsPromises; +const { writeFile } = fsPromises || {}; export async function graphqlMesh() { return yargs diff --git a/packages/handlers/json-schema/src/index.ts b/packages/handlers/json-schema/src/index.ts index 3927ddcdc408b..44f1f4600e63c 100644 --- a/packages/handlers/json-schema/src/index.ts +++ b/packages/handlers/json-schema/src/index.ts @@ -29,7 +29,7 @@ import { } from 'graphql-scalars'; import { promises as fsPromises } from 'fs'; -const { stat } = fsPromises; +const { stat } = fsPromises || {}; type CachedSchema = { timestamp: number; diff --git a/packages/handlers/postgraphile/src/index.ts b/packages/handlers/postgraphile/src/index.ts index 17130cb29e946..816e78fabcc44 100644 --- a/packages/handlers/postgraphile/src/index.ts +++ b/packages/handlers/postgraphile/src/index.ts @@ -16,7 +16,7 @@ import { tmpdir } from 'os'; import { promises as fsPromises } from 'fs'; import { loadFromModuleExportExpression, readFileOrUrlWithCache, readJSON } from '@graphql-mesh/utils'; -const { unlink } = fsPromises; +const { unlink } = fsPromises || {}; export default class PostGraphileHandler implements MeshHandler { private name: string; diff --git a/packages/runtime/src/get-mesh.ts b/packages/runtime/src/get-mesh.ts index b6c3c6163c867..fee6ae5d8fd0f 100644 --- a/packages/runtime/src/get-mesh.ts +++ b/packages/runtime/src/get-mesh.ts @@ -72,6 +72,7 @@ export async function getMesh( const context: any = { ...initialContextValue, pubsub, + cache, [MESH_CONTEXT_SYMBOL]: true, }; diff --git a/packages/runtime/src/types.ts b/packages/runtime/src/types.ts index c771549e2db9d..7864859b82f2f 100644 --- a/packages/runtime/src/types.ts +++ b/packages/runtime/src/types.ts @@ -72,4 +72,4 @@ export type APIContext = { export type MeshContext = { [MESH_CONTEXT_SYMBOL]: true; [key: string]: APIContext; -} & { pubsub: MeshPubSub }; +} & { pubsub: MeshPubSub; cache: KeyValueCache }; diff --git a/packages/transforms/snapshot/test/snapshot.spec.ts b/packages/transforms/snapshot/test/snapshot.spec.ts index e07b6f95b84a2..36c293e559b7c 100644 --- a/packages/transforms/snapshot/test/snapshot.spec.ts +++ b/packages/transforms/snapshot/test/snapshot.spec.ts @@ -13,7 +13,7 @@ import { pick } from 'lodash'; import graphqlFields from 'graphql-fields'; import { readJSON, mkdir } from '@graphql-mesh/utils'; -const { readdir, unlink, rmdir } = fsPromises; +const { readdir, unlink, rmdir } = fsPromises || {}; async function rmdirs(dir: string) { let entries = await readdir(dir, { withFileTypes: true }); diff --git a/packages/types/src/config-schema.json b/packages/types/src/config-schema.json index f1273fa919f31..f577bf6fce2d1 100644 --- a/packages/types/src/config-schema.json +++ b/packages/types/src/config-schema.json @@ -1385,18 +1385,21 @@ "title": "SelectQueryOrMutationFieldConfig", "properties": { "title": { - "type": "string" + "type": "string", + "description": "OAS Title" }, "path": { - "type": "string" + "type": "string", + "description": "Operation Path" }, "type": { "type": "string", "enum": ["Query", "Mutation"], - "description": "Allowed values: Query, Mutation" + "description": "Target Root Type for this operation (Allowed values: Query, Mutation)" }, "method": { - "type": "string" + "type": "string", + "description": "Which method is used for this operation" } } }, diff --git a/packages/types/src/config.ts b/packages/types/src/config.ts index be2922c7a6a94..fcd9e15b77f0c 100644 --- a/packages/types/src/config.ts +++ b/packages/types/src/config.ts @@ -621,12 +621,21 @@ export interface OpenapiHandler { selectQueryOrMutationField?: SelectQueryOrMutationFieldConfig[]; } export interface SelectQueryOrMutationFieldConfig { + /** + * OAS Title + */ title?: string; + /** + * Operation Path + */ path?: string; /** - * Allowed values: Query, Mutation + * Target Root Type for this operation (Allowed values: Query, Mutation) */ type?: 'Query' | 'Mutation'; + /** + * Which method is used for this operation + */ method?: string; } /** diff --git a/packages/utils/src/fs-operations.ts b/packages/utils/src/fs-operations.ts index 3de6a45cfc8df..db76c90ad2a56 100644 --- a/packages/utils/src/fs-operations.ts +++ b/packages/utils/src/fs-operations.ts @@ -1,6 +1,6 @@ import { MakeDirectoryOptions, promises as fsPromises, readFileSync } from 'fs'; -const { stat, writeFile, readFile, mkdir: fsMkdir } = fsPromises; +const { stat, writeFile, readFile, mkdir: fsMkdir } = fsPromises || {}; export async function pathExists(path: string) { try { diff --git a/packages/utils/src/read-file-or-url.ts b/packages/utils/src/read-file-or-url.ts index 672634a0146be..9dda147924f6f 100644 --- a/packages/utils/src/read-file-or-url.ts +++ b/packages/utils/src/read-file-or-url.ts @@ -4,7 +4,7 @@ import { load as loadYaml } from 'js-yaml'; import { isAbsolute, resolve } from 'path'; import { promises as fsPromises } from 'fs'; -const { readFile, stat } = fsPromises; +const { readFile, stat } = fsPromises || {}; export { isUrl };