diff --git a/packages/load/src/load-typedefs.ts b/packages/load/src/load-typedefs.ts index f7e1cba63ce..17c1a004770 100644 --- a/packages/load/src/load-typedefs.ts +++ b/packages/load/src/load-typedefs.ts @@ -14,8 +14,6 @@ export type LoadTypedefsOptions = SingleFi filterKinds?: string[]; ignore?: string | string[]; sort?: boolean; - skipGraphQLImport?: boolean; - forceGraphQLImport?: boolean; }; export type UnnormalizedTypeDefPointer = { [key: string]: any } | string; diff --git a/packages/loaders/code-file/src/index.ts b/packages/loaders/code-file/src/index.ts index 65125a5e658..8e9f6557d27 100644 --- a/packages/loaders/code-file/src/index.ts +++ b/packages/loaders/code-file/src/index.ts @@ -18,13 +18,14 @@ import { } from '@graphql-tools/graphql-tag-pluck'; import { tryToLoadFromExport, tryToLoadFromExportSync } from './load-from-module'; import { isAbsolute, resolve } from 'path'; -import { exists, existsSync, readFileSync, readFile } from 'fs-extra'; +import { readFileSync, readFile, pathExists, pathExistsSync } from 'fs-extra'; import { cwd } from 'process'; export type CodeFileLoaderOptions = { require?: string | string[]; pluckConfig?: GraphQLTagPluckOptions; noPluck?: boolean; + noRequire?: boolean; } & SingleFileOptions; const FILE_EXTENSIONS = ['.ts', '.tsx', '.js', '.jsx', '.vue']; @@ -41,7 +42,7 @@ export class CodeFileLoader implements UniversalLoader { if (isValidPath(pointer)) { if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); - return new Promise(resolve => exists(normalizedFilePath, resolve)); + return pathExists(normalizedFilePath); } } @@ -52,9 +53,7 @@ export class CodeFileLoader implements UniversalLoader { if (isValidPath(pointer)) { if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); - if (existsSync(normalizedFilePath)) { - return true; - } + return pathExistsSync(normalizedFilePath); } } diff --git a/packages/loaders/graphql-file/src/index.ts b/packages/loaders/graphql-file/src/index.ts index f574b0f7df1..d2ce34312ec 100644 --- a/packages/loaders/graphql-file/src/index.ts +++ b/packages/loaders/graphql-file/src/index.ts @@ -8,13 +8,15 @@ import { SingleFileOptions, } from '@graphql-tools/utils'; import { isAbsolute, resolve } from 'path'; -import { exists, existsSync, readFile, readFileSync } from 'fs-extra'; +import { readFile, readFileSync, pathExists, pathExistsSync } from 'fs-extra'; import { cwd as processCwd } from 'process'; import { processImport } from '@graphql-tools/import'; const FILE_EXTENSIONS = ['.gql', '.gqls', '.graphql', '.graphqls']; -export interface GraphQLFileLoaderOptions extends SingleFileOptions {} +export interface GraphQLFileLoaderOptions extends SingleFileOptions { + skipGraphQLImport?: boolean; +} function isGraphQLImportFile(rawSDL: string) { const trimmedRawSDL = rawSDL.trim(); @@ -33,7 +35,7 @@ export class GraphQLFileLoader implements UniversalLoader pointer.endsWith(extension))) { const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer); - return new Promise(resolve => exists(normalizedFilePath, resolve)); + return pathExists(normalizedFilePath); } } @@ -44,7 +46,7 @@ export class GraphQLFileLoader implements UniversalLoader pointer.endsWith(extension))) { const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer); - return existsSync(normalizedFilePath); + return pathExistsSync(normalizedFilePath); } } @@ -55,21 +57,18 @@ export class GraphQLFileLoader implements UniversalLoader pointer.endsWith(extension))) { const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); - return new Promise(resolve => exists(normalizedFilePath, resolve)); + return pathExists(normalizedFilePath); } } @@ -35,9 +35,7 @@ export class JsonFileLoader implements DocumentLoader { if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) { const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd || cwd(), pointer); - if (existsSync(normalizedFilePath)) { - return true; - } + return pathExistsSync(normalizedFilePath); } } diff --git a/packages/loaders/prisma/src/index.ts b/packages/loaders/prisma/src/index.ts index 005db105b4c..862079d8248 100644 --- a/packages/loaders/prisma/src/index.ts +++ b/packages/loaders/prisma/src/index.ts @@ -1,7 +1,7 @@ import { UrlLoader, LoadFromUrlOptions } from '@graphql-tools/url-loader'; import { PrismaDefinitionClass, Environment } from 'prisma-yml'; import { join } from 'path'; -import { exists } from 'fs-extra'; +import { pathExists } from 'fs-extra'; import { homedir } from 'os'; import { cwd } from 'process'; @@ -19,7 +19,7 @@ export class PrismaLoader extends UrlLoader { async canLoad(prismaConfigFilePath: string, options: PrismaLoaderOptions): Promise { if (typeof prismaConfigFilePath === 'string' && prismaConfigFilePath.endsWith('prisma.yml')) { const joinedYmlPath = join(options.cwd || cwd(), prismaConfigFilePath); - return new Promise(resolve => exists(joinedYmlPath, resolve)); + return pathExists(joinedYmlPath); } return false; } diff --git a/packages/utils/src/loaders.ts b/packages/utils/src/loaders.ts index 6ab43eb3b2b..24ba845a6d1 100644 --- a/packages/utils/src/loaders.ts +++ b/packages/utils/src/loaders.ts @@ -11,7 +11,6 @@ export interface Source { export type SingleFileOptions = ParseOptions & GraphQLSchemaValidationOptions & BuildSchemaOptions & { - noRequire?: boolean; cwd?: string; }; diff --git a/website/docs/merge-typedefs.md b/website/docs/merge-typedefs.md index e5b59502f51..6870cf9ca2f 100644 --- a/website/docs/merge-typedefs.md +++ b/website/docs/merge-typedefs.md @@ -102,21 +102,21 @@ module.exports = mergeTypeDefs(types, { all: true }); ### Import everything from a specified folder -In this way we use the `loadFiles` function from `@graphql-tools/load-files` to import all files from the specified folder. +In this way we use the `loadFilesSync` function from `@graphql-tools/load-files` to import all files from the specified folder. ```js // ./graphql/typeDefs.js const path = require('path'); -const { loadFiles } = require('@graphql-tools/load-files'); +const { loadFilesSync } = require('@graphql-tools/load-files'); const { mergeTypeDefs } = require('@graphql-tools/merge'); -const typesArray = loadFiles(path.join(__dirname, './types')); +const typesArray = loadFilesSync(path.join(__dirname, './types')); module.exports = mergeTypeDefs(typesArray, { all: true }); ``` -When using the `loadFiles` function you can also implement your type definitions using `.graphql` or `.gql` or `.graphqls` files. +When using the `loadFilesSync` function you can also implement your type definitions using `.graphql` or `.gql` or `.graphqls` files. -> The `loadFiles` function will by default ignore files named `index.js` or `index.ts` (use `{ignoreIndex: false}` option to change this behavior). This allows you to create your index file inside the actual types folder if desired. +> The `loadFilesSync` function will by default ignore files named `index.js` or `index.ts` (use `{ignoreIndex: false}` option to change this behavior). This allows you to create your index file inside the actual types folder if desired. ```graphql # ./graphql/types/clientType.graphql @@ -170,15 +170,15 @@ Here's how your `index` file could look like: ```js const path = require('path'); -const { loadFiles } = require('@graphql-tools/load-files'); +const { loadFilesSync } = require('@graphql-tools/load-files'); const { mergeTypeDefs } = require('@graphql-tools/merge'); -const typesArray = loadFiles(path.join(__dirname, '.'), { recursive: true }) +const typesArray = loadFilesSync(path.join(__dirname, '.'), { recursive: true }) module.exports = mergeTypeDefs(typesArray, { all: true }) ``` -You can also load files in different folders by passing a glob pattern in `loadFiles`. +You can also load files in different folders by passing a glob pattern in `loadFilesSync`. Given the file structure below: ``` @@ -196,10 +196,10 @@ Here's how your `index` file could look like: ```js const path = require('path'); -const { loadFiles } = require('@graphql-tools/load-files'); +const { loadFilesSync } = require('@graphql-tools/load-files'); const { mergeTypeDefs } = require('@graphql-tools/merge'); -const typesArray = loadFiles(path.join(__dirname, 'graphql/**/*.graphql')) +const typesArray = loadFilesSync(path.join(__dirname, 'graphql/**/*.graphql')) module.exports = mergeTypeDefs(typesArray, { all: true }) ``` @@ -209,12 +209,12 @@ module.exports = mergeTypeDefs(typesArray, { all: true }) Since the output of `mergeTypeDefs` is `DocumentNode`, after you merge your types, you can save it to a file to be passed around to other systems. Here is an example using ES6 modules: ```js -const { loadFiles } = require('@graphql-tools/load-files'); +const { loadFilesSync } = require('@graphql-tools/load-files'); const { mergeTypeDefs } = require('@graphql-tools/merge'); const { print } = require('graphql'); const fs = require('fs'); -const loadedFiles = loadFiles(`${__dirname}/schema/**/*.graphql`); +const loadedFiles = loadFilesSync(`${__dirname}/schema/**/*.graphql`); const typeDefs = mergeTypeDefs(loadedFiles, { all: true }); const printedTypeDefs = print(typeDefs); fs.writeFileSync('joined.graphql', printedTypeDefs);