Skip to content

Commit

Permalink
fix(fs): fix fs handling issues for non Node environments
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jan 20, 2021
1 parent 610fdf6 commit c767df0
Show file tree
Hide file tree
Showing 15 changed files with 49 additions and 22 deletions.
13 changes: 13 additions & 0 deletions .changeset/fast-pets-dance.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions examples/openapi-react-weatherbit/.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SKIP_PREFLIGHT_CHECK=true
17 changes: 8 additions & 9 deletions examples/openapi-react-weatherbit/src/mesh/getMeshInstance.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
});
}
1 change: 1 addition & 0 deletions packages/cache/file/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/commands/serve/playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/cli/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/handlers/json-schema/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from 'graphql-scalars';
import { promises as fsPromises } from 'fs';

const { stat } = fsPromises;
const { stat } = fsPromises || {};

type CachedSchema = {
timestamp: number;
Expand Down
2 changes: 1 addition & 1 deletion packages/handlers/postgraphile/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions packages/runtime/src/get-mesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export async function getMesh(
const context: any = {
...initialContextValue,
pubsub,
cache,
[MESH_CONTEXT_SYMBOL]: true,
};

Expand Down
2 changes: 1 addition & 1 deletion packages/runtime/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ export type APIContext = {
export type MeshContext = {
[MESH_CONTEXT_SYMBOL]: true;
[key: string]: APIContext;
} & { pubsub: MeshPubSub };
} & { pubsub: MeshPubSub; cache: KeyValueCache };
2 changes: 1 addition & 1 deletion packages/transforms/snapshot/test/snapshot.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand Down
11 changes: 7 additions & 4 deletions packages/types/src/config-schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
},
Expand Down
11 changes: 10 additions & 1 deletion packages/types/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
/**
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/fs-operations.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion packages/utils/src/read-file-or-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };

Expand Down

0 comments on commit c767df0

Please sign in to comment.