Skip to content

Commit

Permalink
Synchronous version
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Mar 17, 2020
1 parent 500a879 commit 52768fc
Show file tree
Hide file tree
Showing 15 changed files with 555 additions and 314 deletions.
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
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
"graphql": "^0.11.0 || ^0.12.0 || ^0.13.0 || ^14.0.0"
},
"dependencies": {
"@graphql-toolkit/common": "0.9.8",
"@graphql-toolkit/core": "0.9.8",
"@graphql-toolkit/graphql-file-loader": "0.9.7",
"@graphql-toolkit/json-file-loader": "0.9.7",
"@graphql-toolkit/schema-merging": "0.9.8",
"@graphql-toolkit/url-loader": "0.9.7",
"@graphql-toolkit/common": "0.9.8-alpha-6be3bfe.44+6be3bfe",
"@graphql-toolkit/core": "0.9.8-alpha-6be3bfe.44+6be3bfe",
"@graphql-toolkit/graphql-file-loader": "0.9.8-alpha-6be3bfe.44+6be3bfe",
"@graphql-toolkit/json-file-loader": "0.9.8-alpha-6be3bfe.44+6be3bfe",
"@graphql-toolkit/schema-merging": "0.9.8-alpha-6be3bfe.44+6be3bfe",
"@graphql-toolkit/url-loader": "0.9.8-alpha-6be3bfe.44+6be3bfe",
"cosmiconfig": "6.0.0",
"globby": "11.0.0",
"minimatch": "3.0.4"
Expand Down
84 changes: 68 additions & 16 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
isSingleProjectConfig,
findConfig,
getConfig,
getConfigSync,
findConfigSync,
} from './helpers';
import {
ProjectNotFoundError,
Expand All @@ -30,14 +32,27 @@ interface LoadConfigOptions {
configName?: string;
}

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,
};

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

try {
const found = filepath
? await getConfig({
Expand All @@ -51,17 +66,54 @@ export async function loadConfig({

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

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

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

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

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

throw error;
}

export class GraphQLConfig {
private readonly _rawConfig: IGraphQLConfig;

Expand All @@ -84,7 +136,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 Down
171 changes: 0 additions & 171 deletions src/helpers.ts

This file was deleted.

Loading

0 comments on commit 52768fc

Please sign in to comment.