Skip to content

Commit

Permalink
Synchronous version + support legacy config (#437)
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela authored Mar 25, 2020
1 parent c1f8dcf commit f71630f
Show file tree
Hide file tree
Showing 44 changed files with 958 additions and 6,029 deletions.
16 changes: 14 additions & 2 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,34 @@ on:
- '**.yml'
jobs:
test:
name: Test on node ${{ matrix.node_version }}
runs-on: ubuntu-latest
name: Test on node ${{ matrix.node_version }} and ${{matrix.os}}
runs-on: ${{matrix.os}}
strategy:
matrix:
os: [ubuntu-latest, windows-latest]
node_version: [10, 12, 13]
steps:
- uses: actions/checkout@master
- name: Use Node.js ${{ matrix.node_version }}
uses: actions/setup-node@master
with:
node-version: ${{ matrix.node_version }}

- uses: actions/cache@v1
id: yarn-cache
with:
path: node_modules
key: ${{ runner.os }}-yarn-${{ hashFiles('yarn.lock') }}

- name: Install
run: yarn
if: steps.yarn-cache.outputs.cache-hit != 'true'

- name: Test
run: yarn test

- name: Lint
run: yarn lint

- name: Build
run: yarn build
16 changes: 16 additions & 0 deletions docs/api-graphql-project-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,28 @@ _type: `getSchema(out: 'GraphQLSchema'): Promise<GraphQLSchema>`_

Allows to access `GraphQLSchema` object based on provided information (in `schema` property of project's configuration).

### `getSchemaSync()`

_type: `getSchemaSync(): GraphQLSchema`_

_type: `getSchemaSync(out: 'DocumentNode'): DocumentNode`_

_type: `getSchemaSync(out: 'GraphQLSchema'): GraphQLSchema`_

Allows to access `GraphQLSchema` object based on provided information (in `schema` property of project's configuration).

### `getDocuments()`

_type: `getDocuments(): Promise<Source[]>`_

Access Operations and Fragments wrapped with `Source` class based on provided information (in `documents` property of project's configuration).

### `getDocumentsSync()`

_type: `getDocumentsSync(): Source[]`_

Access Operations and Fragments wrapped with `Source` class based on provided information (in `documents` property of project's configuration).

### `match()`

_type: `match(filepath: string): boolean`_
Expand Down
2 changes: 2 additions & 0 deletions docs/author-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ async function main() {
}
```

> Synchronous version: `loadConfigSync`
Now that everything is ready, GraphQL Config understands there's the Inspector extension.

In order to access information stored in the config file, do the following:
Expand Down
12 changes: 11 additions & 1 deletion docs/author-load-config.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ sidebar_label: Loading Config

This function is the starting point for using GraphQL Config. It looks for a config file in [predefined search places](./user-usage.md#config-search-places) in the currently working directory.

A basic usage example:
A basic usage example (async):

```typescript
import {loadConfig} from 'graphql-config';
Expand All @@ -18,6 +18,16 @@ async function main() {
}
```

Synchronous version:

```typescript
import {loadConfigSync} from 'graphql-config';

function main() {
const config = loadConfigSync({...}); // an instance of GraphQLConfig
}
```

## Options

### `filepath`
Expand Down
7 changes: 6 additions & 1 deletion docs/user-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,17 @@ sidebar_label: Usage

## Config search places

- `graphql.config.json`
- `graphql.config.js`
- `graphql.config.yaml`
- `graphql.config.yml`

- `.graphqlrc` _(YAML and JSON)_
- `.graphqlrc.json`
- `.graphqlrc.yaml`
- `.graphqlrc.yml`
- `.graphqlrc.js`
- `graphql.config.js`

- `graphql` property in `package.json`

## Schema
Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "graphql-config",
"version": "3.0.0-alpha.18",
"version": "3.0.0-alpha.22",
"description": "The easiest way to configure your development environment with your GraphQL schema (supported by most tools, editors & IDEs)",
"sideEffects": false,
"main": "dist/index.cjs.js",
Expand All @@ -18,8 +18,8 @@
"prebuild": "yarn clean",
"build": "bob",
"prepack": "bob-update-version",
"format": "prettier --write '{src,test}/**/*.{ts,graphql}'",
"lint": "eslint '{src,test}/**/*.ts'",
"format": "prettier --write \"{src,test}/**/*.{ts,graphql}\"",
"lint": "eslint \"{src,test}/**/*.ts\"",
"test": "jest",
"now-build": "(cd website && yarn && yarn build && mv build/graphql-config ../public)",
"precommit": "lint-staged",
Expand All @@ -33,12 +33,12 @@
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
},
"dependencies": {
"@graphql-toolkit/common": "0.9.9",
"@graphql-toolkit/core": "0.9.9",
"@graphql-toolkit/graphql-file-loader": "0.9.7",
"@graphql-toolkit/json-file-loader": "0.9.7",
"@graphql-toolkit/schema-merging": "0.9.9",
"@graphql-toolkit/url-loader": "0.9.7",
"@graphql-toolkit/common": "0.9.10-alpha-b7764d5.0+b7764d5",
"@graphql-toolkit/core": "0.9.10-alpha-b7764d5.0+b7764d5",
"@graphql-toolkit/graphql-file-loader": "0.9.10-alpha-b7764d5.0+b7764d5",
"@graphql-toolkit/json-file-loader": "0.9.10-alpha-b7764d5.0+b7764d5",
"@graphql-toolkit/schema-merging": "0.9.10-alpha-b7764d5.0+b7764d5",
"@graphql-toolkit/url-loader": "0.9.10-alpha-b7764d5.0+b7764d5",
"cosmiconfig": "6.0.0",
"globby": "11.0.0",
"minimatch": "3.0.4"
Expand Down
107 changes: 91 additions & 16 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import {
isSingleProjectConfig,
findConfig,
getConfig,
getConfigSync,
findConfigSync,
isLegacyProjectConfig,
} from './helpers';
import {
ProjectNotFoundError,
Expand All @@ -17,6 +20,7 @@ import {
GraphQLExtensionsRegistry,
} from './extension';
import {EndpointsExtension} from './extensions/endpoints';
import {isLegacyConfig} from './helpers/cosmiconfig';

const cwd = typeof process !== 'undefined' ? process.cwd() : undefined;
const defaultConfigName = 'graphql';
Expand All @@ -28,38 +32,98 @@ interface LoadConfigOptions {
throwOnMissing?: boolean;
throwOnEmpty?: boolean;
configName?: string;
legacy?: boolean;
}

export async function loadConfig({
filepath,
rootDir = cwd,
extensions = [],
throwOnMissing = true,
throwOnEmpty = true,
configName = defaultConfigName,
}: LoadConfigOptions) {
const defaultLoadConfigOptions: LoadConfigOptions = {
rootDir: cwd,
extensions: [],
throwOnMissing: true,
throwOnEmpty: true,
configName: defaultConfigName,
legacy: true,
};

export async function loadConfig(
options: LoadConfigOptions,
): Promise<GraphQLConfig | undefined> {
const {
filepath,
configName,
rootDir,
extensions,
throwOnEmpty,
throwOnMissing,
legacy,
} = {
...defaultLoadConfigOptions,
...options,
};

try {
const found = filepath
? await getConfig({
filepath,
configName,
legacy,
})
: await findConfig({
rootDir,
configName,
legacy,
});

return new GraphQLConfig(found, extensions);
} catch (error) {
if (
(!throwOnMissing && error instanceof ConfigNotFoundError) ||
(!throwOnEmpty && error instanceof ConfigEmptyError)
) {
return;
}
return handleError(error, {throwOnMissing, throwOnEmpty});
}
}

export function loadConfigSync(options: LoadConfigOptions) {
const {
filepath,
configName,
rootDir,
extensions,
throwOnEmpty,
throwOnMissing,
legacy,
} = {
...defaultLoadConfigOptions,
...options,
};

try {
const found = filepath
? getConfigSync({
filepath,
configName,
legacy,
})
: findConfigSync({
rootDir,
configName,
legacy,
});

throw error;
return new GraphQLConfig(found, extensions);
} catch (error) {
return handleError(error, {throwOnMissing, throwOnEmpty});
}
}

function handleError(
error: any,
options: Pick<LoadConfigOptions, 'throwOnMissing' | 'throwOnEmpty'>,
): never | undefined {
if (
(!options.throwOnMissing && error instanceof ConfigNotFoundError) ||
(!options.throwOnEmpty && error instanceof ConfigEmptyError)
) {
return;
}

throw error;
}

export class GraphQLConfig {
Expand All @@ -84,7 +148,7 @@ export class GraphQLConfig {
this.extensions = new GraphQLExtensionsRegistry({cwd: this.dirpath});

// Register Endpoints
this.extensions.register(EndpointsExtension)
this.extensions.register(EndpointsExtension);

extensions.forEach(extension => {
this.extensions.register(extension);
Expand All @@ -110,6 +174,13 @@ export class GraphQLConfig {
config: this._rawConfig,
extensionsRegistry: this.extensions,
});
} else if (isLegacyProjectConfig(this._rawConfig)) {
this.projects['default'] = new GraphQLProjectConfig({
filepath: this.filepath,
name: 'default',
config: this._rawConfig,
extensionsRegistry: this.extensions,
});
}
}

Expand Down Expand Up @@ -159,4 +230,8 @@ export class GraphQLConfig {
getDefault(): GraphQLProjectConfig | never {
return this.getProject('default');
}

isLegacy(): boolean {
return isLegacyConfig(this.filepath);
}
}
Loading

0 comments on commit f71630f

Please sign in to comment.