Skip to content

Commit

Permalink
Use new GraphQL Config
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Mar 18, 2020
1 parent a1a7517 commit d83a091
Show file tree
Hide file tree
Showing 9 changed files with 647 additions and 121 deletions.
684 changes: 598 additions & 86 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -46,7 +46,7 @@
},
"license": "MIT",
"dependencies": {
"graphql-config": "^2.0.1",
"graphql-config": "3.0.0-alpha.22",
"lodash": "^4.11.1"
},
"peerDependencies": {
Expand Down
32 changes: 23 additions & 9 deletions src/index.js
Expand Up @@ -8,7 +8,7 @@ import {

import { flatten, keys, reduce, without, includes } from "lodash";

import { getGraphQLConfig, ConfigNotFoundError } from "graphql-config";
import { loadConfigSync, ConfigNotFoundError, ProjectNotFoundError } from "graphql-config";

import * as customRules from "./customGraphQLValidationRules";
import { internalTag } from "./constants";
Expand Down Expand Up @@ -268,23 +268,34 @@ function parseOptions(optionGroup, context) {
schema = initSchemaFromString(schemaString);
} else {
try {
const config = getGraphQLConfig(path.dirname(context.getFilename()));
const config = loadConfigSync({
rootDir: path.resolve(
process.cwd(),
path.dirname(context.getFilename())
)
});
let projectConfig;
if (projectName) {
projectConfig = config.getProjects()[projectName];
projectConfig = config.getProject(projectName);
if (!projectConfig) {
throw new Error(
`Project with name "${projectName}" not found in ${config.configPath}.`
`Project with name "${projectName}" not found in ${config.filepath}.`
);
}
} else {
projectConfig = config.getConfigForFile(context.getFilename());
try {
projectConfig = config.getProjectForFile(context.getFilename());
} catch (e) {
if (!e instanceof ProjectNotFoundError) {
throw e;
}
}
}
if (projectConfig) {
const key = `${config.configPath}[${projectConfig.projectName}]`;
const key = `${config.filepath}[${projectConfig.name}]`;
schema = projectCache[key];
if (!schema) {
schema = projectConfig.getSchema();
schema = projectConfig.getSchemaSync();
projectCache[key] = schema;
}
}
Expand All @@ -294,7 +305,7 @@ function parseOptions(optionGroup, context) {
} catch (e) {
if (e instanceof ConfigNotFoundError) {
throw new Error(
"Must provide .graphqlconfig file or pass in `schemaJson` option " +
"Must provide GraphQL Config file or pass in `schemaJson` option " +
"with schema object or `schemaJsonFilepath` with absolute path to the json file."
);
}
Expand Down Expand Up @@ -386,7 +397,10 @@ const gqlProcessor = {
postprocess: function(messages) {
// only report graphql-errors
return flatten(messages).filter(message => {
return includes(keys(rules).map(key => `graphql/${key}`), message.ruleId);
return includes(
keys(rules).map(key => `graphql/${key}`),
message.ruleId
);
});
}
};
Expand Down
12 changes: 0 additions & 12 deletions test/graphqlconfig/multiproject-literal/.graphqlconfig

This file was deleted.

12 changes: 12 additions & 0 deletions test/graphqlconfig/multiproject-literal/graphql.config.json
@@ -0,0 +1,12 @@
{
"projects": {
"gql": {
"schema": "../../schema.graphql",
"include": ["first.graphql"]
},
"swapi": {
"schema": "../../second-schema.graphql",
"include": ["second.graphql"]
}
}
}
10 changes: 0 additions & 10 deletions test/graphqlconfig/multiproject/.graphqlconfig

This file was deleted.

10 changes: 10 additions & 0 deletions test/graphqlconfig/multiproject/graphql.config.json
@@ -0,0 +1,10 @@
{
"projects": {
"gql": {
"schema": "../../schema.graphql"
},
"swapi": {
"schema": "../../second-schema.graphql"
}
}
}
3 changes: 0 additions & 3 deletions test/graphqlconfig/simple/.graphqlconfig

This file was deleted.

3 changes: 3 additions & 0 deletions test/graphqlconfig/simple/graphql.config.json
@@ -0,0 +1,3 @@
{
"schema": "../../schema.graphql"
}

0 comments on commit d83a091

Please sign in to comment.