Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for loading the config from "package.json" #693

Merged
merged 2 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/helpers/cosmiconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ function prepareCosmiconfig(moduleName: string, {legacy}: {legacy: boolean}) {
'.#rc.yml',
'.#rc.yaml',
'.#rc.toml',
'package.json',
];

if (legacy) {
Expand Down
71 changes: 65 additions & 6 deletions test/config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {buildSchema, buildASTSchema} from 'graphql';
import {resolve} from 'path';
import {resolve, basename} from 'path';
import {TempDir} from './utils/temp-dir';
import {runTests} from './utils/runner';
import {loadConfig, loadConfigSync} from '../src/config';
Expand Down Expand Up @@ -121,6 +121,65 @@ runTests({async: loadConfig, sync: loadConfigSync})((load, mode) => {
});
});

describe('loading from supported config files', () => {
const moduleName = 'graphql';
const schemaFile = 'schema.graphql';

const tsConfig = `export default { schema: '${schemaFile}' };`;
const jsConfig = `module.exports = { schema: '${schemaFile}' };`;
const yamlConfig = `schema: '${schemaFile}'`;
const tomlConfig = `schema = "${schemaFile}"`;
const jsonConfig = `{"schema": "${schemaFile}"}`;
const packageJsonConfig = `{"${moduleName}": {"schema": "${schemaFile}"}}`;

const configFiles = [
// #.config files
[`${moduleName}.config.ts`, tsConfig],
[`${moduleName}.config.js`, jsConfig],
[`${moduleName}.config.json`, jsonConfig],
[`${moduleName}.config.yaml`, yamlConfig],
[`${moduleName}.config.yml`, yamlConfig],
[`${moduleName}.config.toml`, tomlConfig],
// .#rc files
[`.${moduleName}rc`, yamlConfig],
[`.${moduleName}rc.ts`, tsConfig],
[`.${moduleName}rc.js`, jsConfig],
[`.${moduleName}rc.json`, jsonConfig],
[`.${moduleName}rc.yml`, yamlConfig],
[`.${moduleName}rc.yaml`, yamlConfig],
[`.${moduleName}rc.toml`, tomlConfig],
// other files
['package.json', packageJsonConfig],
];

beforeEach(() => {
temp.clean();
temp.createFile(
schemaFile,
/* GraphQL */ `
type Query {
foo: String
}
`,
);
});

test.each(configFiles)('load config from "%s"', async (name, content) => {
temp.createFile(name, content);

const config = await load({
rootDir: temp.dir,
});

const loadedFileName = basename(config!.filepath);
const loadedSchema = config!.getDefault()!.schema;

expect(config).toBeDefined();
expect(loadedFileName).toEqual(name);
expect(loadedSchema).toEqual(schemaFile);
});
});

describe('environment variables', () => {
test('not defined but with a default value', async () => {
temp.createFile(
Expand Down Expand Up @@ -190,16 +249,16 @@ runTests({async: loadConfig, sync: loadConfigSync})((load, mode) => {
temp.createFile(
'.graphqlrc',
`
projects:
projects:
foo:
schema: ./foo.graphql
bar:
schema:
schema:
- ./bar.graphql:
noop: true
baz:
schema: ./documents/**/*.graphql

qux:
schema:
- ./schemas/foo.graphql
Expand Down Expand Up @@ -234,7 +293,7 @@ runTests({async: loadConfig, sync: loadConfigSync})((load, mode) => {
temp.createFile(
'.graphqlrc',
`
projects:
projects:
foo:
schema: ./foo.graphql
include: ./foo/*.ts
Expand All @@ -258,7 +317,7 @@ runTests({async: loadConfig, sync: loadConfigSync})((load, mode) => {
temp.createFile(
'.graphqlrc',
`
projects:
projects:
foo:
schema: ./foo.graphql
include: ./foo/*.ts
Expand Down