Adding support for `graphql` (#1982)

* Initial GraphQL implementation

This is the bare minimum amount of code to make graphql work. Turns out it's pretty easy to add support for other languages :)

* Fixing support for `graphql` parsing

There was a few structural changes with the addition of CSS and Typescript that we had to take into account.

* Fixing file glob, adding `graphql` to yarn.lock

* Removing extraneous file

* Adding `graphql` parser, style changes

* Splitting out graphql printer

* Removing merge conflict

* Fixing yarn.lock

* Addressing code review, using `createError`

* Adding release config

* Using exact version

* Removing destructuring
master
Jon Wong 2017-06-05 15:34:08 -04:00 committed by Christopher Chedeau
parent 31ab3b6036
commit e33d6773d6
12 changed files with 157 additions and 3 deletions

View File

@ -97,7 +97,8 @@ function getParserOption() {
value === "flow" ||
value === "babylon" ||
value === "typescript" ||
value === "postcss"
value === "postcss" ||
value === "graphql"
) {
return value;
}

View File

@ -22,6 +22,7 @@
"flow-parser": "0.47.0",
"get-stdin": "5.0.1",
"glob": "7.1.2",
"graphql": "0.10.1",
"jest": "20.0.0",
"jest-validate": "20.0.3",
"minimist": "1.2.0",

View File

@ -5,6 +5,8 @@ function parse(text, opts) {
if (opts.parser === "flow") {
parseFunction = eval("require")("./src/parser-flow");
} else if (opts.parser === "graphql") {
parseFunction = eval("require")("./src/parser-graphql");
} else if (opts.parser === "typescript") {
parseFunction = eval("require")("./src/parser-typescript");
} else if (opts.parser === "postcss") {

View File

@ -23,6 +23,9 @@ node_modules/.bin/rollup -c scripts/build/rollup.parser.config.js --environment
echo 'Bundling lib flow...';
node_modules/.bin/rollup -c scripts/build/rollup.parser.config.js --environment parser:flow
echo 'Bundling lib graphql...';
node_modules/.bin/rollup -c scripts/build/rollup.parser.config.js --environment parser:graphql
echo 'Bundling lib typescript...';
node_modules/.bin/rollup -c scripts/build/rollup.parser.config.js --environment parser:typescript
@ -45,6 +48,9 @@ node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment fi
echo 'Bundling docs flow...';
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:src/parser-flow.js
echo 'Bundling docs graphql...';
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:src/parser-graphql.js
echo 'Bundling docs typescript...';
node_modules/.bin/rollup -c scripts/build/rollup.docs.config.js --environment filepath:src/parser-typescript.js

26
src/parser-graphql.js Normal file
View File

@ -0,0 +1,26 @@
"use strict";
const createError = require("./parser-create-error");
function parse(text) {
// Inline the require to avoid loading all the JS if we don't use it
const parser = require("graphql/language");
try {
const ast = parser.parse(text);
return ast;
} catch (error) {
const GraphQLError = require("graphql/error").GraphQLError;
if (error instanceof GraphQLError) {
throw createError(error.message, {
start: {
line: error.locations[0].line,
column: error.locations[0].column
}
});
} else {
throw error;
}
}
}
module.exports = parse;

81
src/printer-graphql.js Normal file
View File

@ -0,0 +1,81 @@
"use strict";
const docBuilders = require("./doc-builders");
const concat = docBuilders.concat;
const join = docBuilders.join;
const hardline = docBuilders.hardline;
const softline = docBuilders.softline;
const group = docBuilders.group;
const indent = docBuilders.indent;
function genericPrint(path, options, print) {
const n = path.getValue();
if (!n) {
return "";
}
if (typeof n === "string") {
return n;
}
switch (n.kind) {
case "Document": {
return concat([join(hardline, path.map(print, "definitions")), hardline]);
}
case "OperationDefinition": {
return path.call(print, "selectionSet");
}
case "SelectionSet": {
return concat([
"{",
indent(concat([hardline, concat(path.map(print, "selections"))])),
hardline,
"}"
]);
}
case "Field": {
return group(
concat([
path.call(print, "name"),
n.arguments.length > 0
? group(
concat([
"(",
indent(
concat([
softline,
join(
concat([",", softline]),
path.map(print, "arguments")
)
])
),
softline,
") "
])
)
: "",
path.call(print, "selectionSet")
])
);
}
case "Name": {
return n.value;
}
case "StringValue": {
return concat(['"', n.value, '"']);
}
case "Argument": {
return concat([
path.call(print, "name"),
": ",
path.call(print, "value")
]);
}
default:
throw new Error("unknown graphql type: " + JSON.stringify(n.kind));
}
}
module.exports = genericPrint;

View File

@ -50,6 +50,8 @@ function shouldPrintComma(options, level) {
function getPrintFunction(options) {
switch (options.parser) {
case "graphql":
return require("./printer-graphql");
case "postcss":
return require("./printer-postcss");
default:

View File

@ -0,0 +1,16 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`hello.graphql 1`] = `
{
project(name: "GraphQL") {
tagline
}
}
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
project(name: "GraphQL") {
tagline
}
}
`;

View File

@ -0,0 +1,5 @@
{
project(name: "GraphQL") {
tagline
}
}

View File

@ -0,0 +1 @@
run_spec(__dirname, { parser: "graphql" });

View File

@ -10,12 +10,15 @@ const AST_COMPARE = process.env["AST_COMPARE"];
const VERIFY_ALL_PARSERS = process.env["VERIFY_ALL_PARSERS"] || false;
const ALL_PARSERS = process.env["ALL_PARSERS"]
? JSON.parse(process.env["ALL_PARSERS"])
: ["flow", "babylon", "typescript"];
: ["flow", "graphql", "babylon", "typescript"];
function run_spec(dirname, options, additionalParsers) {
fs.readdirSync(dirname).forEach(filename => {
const extension = extname(filename);
if (/^\.([jt]sx?|css)$/.test(extension) && filename !== "jsfmt.spec.js") {
if (
/^\.([jt]sx?|css|graphql)$/.test(extension) &&
filename !== "jsfmt.spec.js"
) {
const path = dirname + "/" + filename;
let rangeStart = 0;
let rangeEnd = Infinity;

View File

@ -1323,6 +1323,12 @@ graceful-fs@^4.1.11, graceful-fs@^4.1.2:
version "1.0.1"
resolved "https://registry.yarnpkg.com/graceful-readlink/-/graceful-readlink-1.0.1.tgz#4cafad76bc62f02fa039b2f94e9a3dd3a391a725"
graphql@0.10.1:
version "0.10.1"
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.10.1.tgz#75c93c2ce73aeb5bae2eefb555a8e9e39c36027d"
dependencies:
iterall "^1.1.0"
growly@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/growly/-/growly-1.3.0.tgz#f10748cbe76af964b7c96c93c6bcc28af120c081"
@ -1703,6 +1709,10 @@ istanbul-reports@^1.1.0:
dependencies:
handlebars "^4.0.3"
iterall@^1.1.0:
version "1.1.1"
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.1.tgz#f7f0af11e9a04ec6426260f5019d9fcca4d50214"
jest-changed-files@^20.0.3:
version "20.0.3"
resolved "https://registry.yarnpkg.com/jest-changed-files/-/jest-changed-files-20.0.3.tgz#9394d5cc65c438406149bef1bf4d52b68e03e3f8"