2017-06-05 22:34:08 +03:00
|
|
|
"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;
|
2017-06-06 08:25:12 +03:00
|
|
|
const ifBreak = docBuilders.ifBreak;
|
2017-06-05 22:34:08 +03:00
|
|
|
|
|
|
|
function genericPrint(path, options, print) {
|
|
|
|
const n = path.getValue();
|
|
|
|
if (!n) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof n === "string") {
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (n.kind) {
|
|
|
|
case "Document": {
|
2017-06-06 08:24:01 +03:00
|
|
|
return join(concat([hardline, hardline]), path.map(print, "definitions"));
|
2017-06-05 22:34:08 +03:00
|
|
|
}
|
|
|
|
case "OperationDefinition": {
|
2017-06-06 08:24:01 +03:00
|
|
|
return concat([
|
|
|
|
n.name === null ? "" : concat([n.operation, " "]),
|
|
|
|
path.call(print, "name"),
|
|
|
|
n.name ? " " : "",
|
|
|
|
path.call(print, "selectionSet")
|
|
|
|
]);
|
2017-06-05 22:34:08 +03:00
|
|
|
}
|
|
|
|
case "SelectionSet": {
|
|
|
|
return concat([
|
|
|
|
"{",
|
2017-06-06 08:23:03 +03:00
|
|
|
indent(
|
|
|
|
concat([hardline, join(hardline, path.map(print, "selections"))])
|
|
|
|
),
|
2017-06-05 22:34:08 +03:00
|
|
|
hardline,
|
|
|
|
"}"
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
case "Field": {
|
|
|
|
return group(
|
|
|
|
concat([
|
|
|
|
path.call(print, "name"),
|
|
|
|
n.arguments.length > 0
|
|
|
|
? group(
|
|
|
|
concat([
|
|
|
|
"(",
|
|
|
|
indent(
|
|
|
|
concat([
|
|
|
|
softline,
|
|
|
|
join(
|
2017-06-06 08:25:12 +03:00
|
|
|
concat([",", ifBreak("", " "), softline]),
|
2017-06-05 22:34:08 +03:00
|
|
|
path.map(print, "arguments")
|
|
|
|
)
|
|
|
|
])
|
|
|
|
),
|
|
|
|
softline,
|
2017-06-06 08:23:03 +03:00
|
|
|
")"
|
2017-06-05 22:34:08 +03:00
|
|
|
])
|
|
|
|
)
|
|
|
|
: "",
|
2017-06-06 08:23:03 +03:00
|
|
|
n.selectionSet ? " " : "",
|
2017-06-05 22:34:08 +03:00
|
|
|
path.call(print, "selectionSet")
|
|
|
|
])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
case "Name": {
|
|
|
|
return n.value;
|
|
|
|
}
|
|
|
|
case "StringValue": {
|
|
|
|
return concat(['"', n.value, '"']);
|
|
|
|
}
|
2017-06-06 08:25:12 +03:00
|
|
|
case "IntValue": {
|
|
|
|
return n.value;
|
|
|
|
}
|
|
|
|
case "FloatValue": {
|
|
|
|
return n.value;
|
|
|
|
}
|
|
|
|
case "BooleanValue": {
|
|
|
|
return n.value ? 'true' : 'false';
|
|
|
|
}
|
|
|
|
case "Variable": {
|
|
|
|
return concat(["$", path.call(print, "name")])
|
|
|
|
}
|
2017-06-05 22:34:08 +03:00
|
|
|
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;
|