Skip to content

Commit

Permalink
Add option to pass schema as string, fixes #59 (#78)
Browse files Browse the repository at this point in the history
* Add schemaString option.

* Update README.
  • Loading branch information
christophercliff authored and jnwng committed Jun 30, 2017
1 parent 634ce89 commit 133219c
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 16 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Has built-in settings for three GraphQL clients out of the box:

### Importing schema JSON

You'll need to import your [introspection query result](https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js). This can be done if you define your ESLint config in a JS file. Note: we're always looking for better ways to get the schema, so please open an issue with suggestions.
You'll need to import your [introspection query result](https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js) or the schema as a string in the Schema Language format. This can be done if you define your ESLint config in a JS file. Note: we're always looking for better ways to get the schema, so please open an issue with suggestions.

### Identity template literal tag

Expand Down Expand Up @@ -73,6 +73,9 @@ module.exports = {
// OR provide absolute path to your schema JSON
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),

// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),

// tagName is gql by default
}]
},
Expand Down Expand Up @@ -100,6 +103,9 @@ module.exports = {
// OR provide absolute path to your schema JSON
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),

// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),

// tagName is set for you to Relay.QL
}]
},
Expand Down Expand Up @@ -127,6 +133,9 @@ module.exports = {
// OR provide absolute path to your schema JSON
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),

// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),

// Optional, the name of the template tag, defaults to 'gql'
tagName: 'gql'
}]
Expand Down Expand Up @@ -155,6 +164,9 @@ module.exports = {
// OR provide absolute path to your schema JSON
// schemaJsonFilepath: path.resolve(__dirname, './schema.json'),

// OR provide the schema in the Schema Language format
// schemaString: printSchema(schema),

// tagName is set automatically
}]
},
Expand Down
47 changes: 32 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
parse,
validate,
buildClientSchema,
buildSchema,
specifiedRules as allGraphQLValidators,
} from 'graphql';

Expand Down Expand Up @@ -53,6 +54,9 @@ const defaultRuleProperties = {
schemaJsonFilepath: {
type: 'string',
},
schemaString: {
type: 'string',
},
tagName: {
type: 'string',
pattern: '^[$_a-zA-Z$_][a-zA-Z0-9$_]+(\\.[a-zA-Z0-9$_]+)?$',
Expand Down Expand Up @@ -114,13 +118,16 @@ const rules = {
}],
},
},
// schemaJson and schemaJsonFilepath are mutually exclusive:
// schemaJson, schemaJsonFilepath and schemaString are mutually exclusive:
oneOf: [{
required: ['schemaJson'],
not: { required: ['schemaJsonFilepath'], },
not: { required: ['schemaString', 'schemaJsonFilepath'], },
}, {
required: ['schemaJsonFilepath'],
not: { required: ['schemaJson'], },
not: { required: ['schemaString', 'schemaJson'], },
}, {
required: ['schemaString'],
not: { required: ['schemaJson', 'schemaJsonFilepath'], },
}],
}
},
Expand All @@ -137,10 +144,13 @@ const rules = {
properties: { ...defaultRuleProperties },
oneOf: [{
required: ['schemaJson'],
not: { required: ['schemaJsonFilepath'], },
not: { required: ['schemaString', 'schemaJsonFilepath'], },
}, {
required: ['schemaJsonFilepath'],
not: { required: ['schemaJson'], },
not: { required: ['schemaString', 'schemaJson'], },
}, {
required: ['schemaString'],
not: { required: ['schemaJson', 'schemaJsonFilepath'], },
}],
},
},
Expand Down Expand Up @@ -168,16 +178,16 @@ const rules = {
},
},
},
oneOf: [
{
required: ['schemaJson'],
not: { required: ['schemaJsonFilepath'] },
},
{
required: ['schemaJsonFilepath'],
not: { required: ['schemaJson'] },
},
],
oneOf: [{
required: ['schemaJson'],
not: { required: ['schemaString', 'schemaJsonFilepath'], },
}, {
required: ['schemaJsonFilepath'],
not: { required: ['schemaString', 'schemaJson'], },
}, {
required: ['schemaString'],
not: { required: ['schemaJson', 'schemaJsonFilepath'], },
}],
},
},
},
Expand All @@ -197,6 +207,7 @@ function parseOptions(optionGroup) {
const {
schemaJson, // Schema via JSON object
schemaJsonFilepath, // Or Schema via absolute filepath
schemaString, // Or Schema as string,
env,
tagName: tagNameOption,
validators: validatorNamesOption,
Expand All @@ -208,6 +219,8 @@ function parseOptions(optionGroup) {
schema = initSchema(schemaJson);
} else if (schemaJsonFilepath) {
schema = initSchemaFromFile(schemaJsonFilepath);
} else if (schemaString) {
schema = initSchemaFromString(schemaString);
} else {
throw new Error('Must pass in `schemaJson` option with schema object '
+ 'or `schemaJsonFilepath` with absolute path to the json file.');
Expand Down Expand Up @@ -265,6 +278,10 @@ function initSchemaFromFile(jsonFile) {
return initSchema(JSON.parse(fs.readFileSync(jsonFile, 'utf8')));
}

function initSchemaFromString(source) {
return buildSchema(source)
}

function templateExpressionMatchesTag(tagName, node) {
const tagNameSegments = tagName.split('.').length;
if (tagNameSegments === 1) {
Expand Down
70 changes: 70 additions & 0 deletions test/makeRule.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import {
values,
entries,
} from 'lodash';
import { printSchema, buildClientSchema } from 'graphql';

const schemaJsonFilepath = path.resolve(__dirname, './schema.json');
const secondSchemaJsonFilepath = path.resolve(__dirname, './second-schema.json');
const schemaString = printSchema(buildClientSchema(schemaJson.data))

// Init rule

Expand Down Expand Up @@ -92,6 +94,74 @@ const parserOptions = {
});
}

{
const options = [
{ schemaString },
];

ruleTester.run('schemaString', rule, {
valid: [
{
options,
parserOptions,
code: 'const x = gql`{ number }`',
},
{
options,
parserOptions,
code: 'const x = segmented.TagName`height: 12px;`'
},
{
options,
parserOptions,
code: 'const x = segmented.gql`height: 12px;`'
},
{
options,
parserOptions,
code: 'const x = gql.segmented`height: 12px;`'
},
{
options,
parserOptions,
code: 'const x = gql`{ number } ${x}`',
},
],

invalid: [
{
options,
parserOptions,
code: 'const x = gql``',
errors: [{
message: 'Syntax Error GraphQL request (1:1) Unexpected <EOF>',
type: 'TaggedTemplateExpression'
}]
},
{
options,
parserOptions,
code: 'const x = gql`{ nonExistentQuery }`',
errors: [{
message: 'Cannot query field "nonExistentQuery" on type "RootQuery".',
type: 'TaggedTemplateExpression'
}]
},
{
options,
parserOptions,
code: 'const x = gql`{ ${x} }`',
errors: [{
message: 'Invalid interpolation - fragment interpolation must occur outside of the brackets.',
type: 'Identifier',
line: 1,
column: 19
}]
},
]
});
}

{
const options = [
{ schemaJson, tagName: 'myGraphQLTag' },
Expand Down

0 comments on commit 133219c

Please sign in to comment.