Skip to content

Commit

Permalink
Improvements on FS operations and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Jun 2, 2020
1 parent b6941f6 commit 7e06966
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 39 deletions.
2 changes: 0 additions & 2 deletions packages/load/src/load-typedefs.ts
Expand Up @@ -14,8 +14,6 @@ export type LoadTypedefsOptions<ExtraConfig = { [key: string]: any }> = SingleFi
filterKinds?: string[];
ignore?: string | string[];
sort?: boolean;
skipGraphQLImport?: boolean;
forceGraphQLImport?: boolean;
};

export type UnnormalizedTypeDefPointer = { [key: string]: any } | string;
Expand Down
9 changes: 4 additions & 5 deletions packages/loaders/code-file/src/index.ts
Expand Up @@ -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'];
Expand All @@ -41,7 +42,7 @@ export class CodeFileLoader implements UniversalLoader<CodeFileLoaderOptions> {
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);
}
}

Expand All @@ -52,9 +53,7 @@ export class CodeFileLoader implements UniversalLoader<CodeFileLoaderOptions> {
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);
}
}

Expand Down
23 changes: 11 additions & 12 deletions packages/loaders/graphql-file/src/index.ts
Expand Up @@ -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();
Expand All @@ -33,7 +35,7 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
if (isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer);
return new Promise(resolve => exists(normalizedFilePath, resolve));
return pathExists(normalizedFilePath);
}
}

Expand All @@ -44,7 +46,7 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
if (isValidPath(pointer)) {
if (FILE_EXTENSIONS.find(extension => pointer.endsWith(extension))) {
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer);
return existsSync(normalizedFilePath);
return pathExistsSync(normalizedFilePath);
}
}

Expand All @@ -55,21 +57,18 @@ export class GraphQLFileLoader implements UniversalLoader<GraphQLFileLoaderOptio
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(options.cwd, pointer);
const rawSDL: string = await readFile(normalizedFilePath, { encoding: 'utf8' });

if (isGraphQLImportFile(rawSDL)) {
return {
location: pointer,
document: processImport(pointer, options.cwd),
};
}
return parseGraphQLSDL(pointer, rawSDL.trim(), options);
return this.handleFileContent(rawSDL, pointer, options);
}

loadSync(pointer: SchemaPointerSingle | DocumentPointerSingle, options: GraphQLFileLoaderOptions): Source {
const cwd = options.cwd || processCwd();
const normalizedFilePath = isAbsolute(pointer) ? pointer : resolve(cwd, pointer);
const rawSDL = readFileSync(normalizedFilePath, { encoding: 'utf8' });
return this.handleFileContent(rawSDL, pointer, options);
}

if (isGraphQLImportFile(rawSDL)) {
handleFileContent(rawSDL: string, pointer: string, options: GraphQLFileLoaderOptions) {
if (!options.skipGraphQLImport && isGraphQLImportFile(rawSDL)) {
return {
location: pointer,
document: processImport(pointer, options.cwd),
Expand Down
8 changes: 3 additions & 5 deletions packages/loaders/json-file/src/index.ts
Expand Up @@ -7,7 +7,7 @@ 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 } from 'process';

const FILE_EXTENSIONS = ['.json'];
Expand All @@ -23,7 +23,7 @@ export class JsonFileLoader implements DocumentLoader {
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);
}
}

Expand All @@ -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);
}
}

Expand Down
4 changes: 2 additions & 2 deletions 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';

Expand All @@ -19,7 +19,7 @@ export class PrismaLoader extends UrlLoader {
async canLoad(prismaConfigFilePath: string, options: PrismaLoaderOptions): Promise<boolean> {
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;
}
Expand Down
1 change: 0 additions & 1 deletion packages/utils/src/loaders.ts
Expand Up @@ -11,7 +11,6 @@ export interface Source {
export type SingleFileOptions = ParseOptions &
GraphQLSchemaValidationOptions &
BuildSchemaOptions & {
noRequire?: boolean;
cwd?: string;
};

Expand Down
24 changes: 12 additions & 12 deletions website/docs/merge-typedefs.md
Expand Up @@ -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
Expand Down Expand Up @@ -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:
```
Expand All @@ -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 })
```
Expand All @@ -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);
Expand Down

0 comments on commit 7e06966

Please sign in to comment.