2017-06-05 15:34:08 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
|
|
|
|
const docBuilders = require("./doc-builders");
|
|
|
|
|
const concat = docBuilders.concat;
|
|
|
|
|
const join = docBuilders.join;
|
|
|
|
|
const hardline = docBuilders.hardline;
|
2017-06-07 19:33:46 +02:00
|
|
|
const line = docBuilders.line;
|
2017-06-05 15:34:08 -04:00
|
|
|
const softline = docBuilders.softline;
|
|
|
|
|
const group = docBuilders.group;
|
|
|
|
|
const indent = docBuilders.indent;
|
2017-06-05 22:25:12 -07:00
|
|
|
const ifBreak = docBuilders.ifBreak;
|
2017-06-05 15:34:08 -04: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-27 09:49:15 -07:00
|
|
|
return concat([
|
|
|
|
|
join(concat([hardline, hardline]), path.map(print, "definitions")),
|
|
|
|
|
hardline
|
|
|
|
|
]);
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
case "OperationDefinition": {
|
2017-06-05 22:24:01 -07:00
|
|
|
return concat([
|
2017-06-06 17:42:47 -07:00
|
|
|
n.name === null ? "" : n.operation,
|
|
|
|
|
n.name ? concat([" ", path.call(print, "name")]) : "",
|
2017-06-06 08:26:04 -07:00
|
|
|
n.variableDefinitions && n.variableDefinitions.length
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
2017-06-27 18:13:07 -07:00
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-06-06 08:26:04 -07:00
|
|
|
path.map(print, "variableDefinitions")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
2017-06-06 17:42:47 -07:00
|
|
|
")"
|
2017-06-06 08:26:04 -07:00
|
|
|
])
|
|
|
|
|
)
|
2017-06-06 17:42:47 -07:00
|
|
|
: "",
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
n.selectionSet ? (n.name === null ? "" : " ") : "",
|
2017-06-05 22:24:01 -07:00
|
|
|
path.call(print, "selectionSet")
|
|
|
|
|
]);
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
2017-06-06 12:35:40 -07:00
|
|
|
case "FragmentDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"fragment ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
" on ",
|
|
|
|
|
path.call(print, "typeCondition"),
|
2017-06-06 17:42:47 -07:00
|
|
|
printDirectives(path, print, n),
|
2017-06-06 12:35:40 -07:00
|
|
|
" ",
|
|
|
|
|
path.call(print, "selectionSet")
|
|
|
|
|
]);
|
|
|
|
|
}
|
2017-06-05 15:34:08 -04:00
|
|
|
case "SelectionSet": {
|
|
|
|
|
return concat([
|
|
|
|
|
"{",
|
2017-06-05 22:23:03 -07:00
|
|
|
indent(
|
|
|
|
|
concat([hardline, join(hardline, path.map(print, "selections"))])
|
|
|
|
|
),
|
2017-06-05 15:34:08 -04:00
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
case "Field": {
|
|
|
|
|
return group(
|
|
|
|
|
concat([
|
2017-06-06 18:06:15 -07:00
|
|
|
n.alias ? concat([path.call(print, "alias"), ": "]) : "",
|
2017-06-05 15:34:08 -04:00
|
|
|
path.call(print, "name"),
|
|
|
|
|
n.arguments.length > 0
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
2017-06-27 18:13:07 -07:00
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-06-05 15:34:08 -04:00
|
|
|
path.map(print, "arguments")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
2017-06-05 22:23:03 -07:00
|
|
|
")"
|
2017-06-05 15:34:08 -04:00
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
2017-06-06 17:42:47 -07:00
|
|
|
printDirectives(path, print, n),
|
2017-06-05 22:23:03 -07:00
|
|
|
n.selectionSet ? " " : "",
|
2017-06-05 15:34:08 -04:00
|
|
|
path.call(print, "selectionSet")
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
case "Name": {
|
|
|
|
|
return n.value;
|
|
|
|
|
}
|
|
|
|
|
case "StringValue": {
|
|
|
|
|
return concat(['"', n.value, '"']);
|
|
|
|
|
}
|
2017-06-06 08:26:04 -07:00
|
|
|
case "IntValue":
|
|
|
|
|
case "FloatValue":
|
|
|
|
|
case "EnumValue": {
|
2017-06-05 22:25:12 -07:00
|
|
|
return n.value;
|
|
|
|
|
}
|
|
|
|
|
case "BooleanValue": {
|
2017-06-05 23:45:15 -07:00
|
|
|
return n.value ? "true" : "false";
|
2017-06-05 22:25:12 -07:00
|
|
|
}
|
2017-06-06 18:06:15 -07:00
|
|
|
case "NullValue": {
|
|
|
|
|
return "null";
|
|
|
|
|
}
|
2017-06-05 22:25:12 -07:00
|
|
|
case "Variable": {
|
2017-06-05 23:45:15 -07:00
|
|
|
return concat(["$", path.call(print, "name")]);
|
|
|
|
|
}
|
|
|
|
|
case "ListValue": {
|
|
|
|
|
return group(
|
|
|
|
|
concat([
|
|
|
|
|
"[",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
2017-06-27 18:13:07 -07:00
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-06-05 23:45:15 -07:00
|
|
|
path.map(print, "values")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
"]"
|
|
|
|
|
])
|
|
|
|
|
);
|
2017-06-05 22:25:12 -07:00
|
|
|
}
|
2017-06-06 14:28:15 -07:00
|
|
|
case "ObjectValue": {
|
|
|
|
|
return group(
|
|
|
|
|
concat([
|
|
|
|
|
"{",
|
2017-06-24 11:06:56 -07:00
|
|
|
options.bracketSpacing && n.fields.length > 0 ? " " : "",
|
2017-06-06 14:28:15 -07:00
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
2017-06-27 18:13:07 -07:00
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-06-06 14:28:15 -07:00
|
|
|
path.map(print, "fields")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
2017-06-24 11:06:56 -07:00
|
|
|
ifBreak("", options.bracketSpacing && n.fields.length > 0 ? " " : ""),
|
2017-06-06 14:28:15 -07:00
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
case "ObjectField":
|
2017-06-05 15:34:08 -04:00
|
|
|
case "Argument": {
|
|
|
|
|
return concat([
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "value")
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 17:42:47 -07:00
|
|
|
case "Directive": {
|
|
|
|
|
return concat([
|
|
|
|
|
"@",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
n.arguments.length > 0
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
2017-06-27 18:13:07 -07:00
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-06-06 17:42:47 -07:00
|
|
|
path.map(print, "arguments")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
")"
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: ""
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 08:26:04 -07:00
|
|
|
case "NamedType": {
|
|
|
|
|
return path.call(print, "name");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "VariableDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
path.call(print, "variable"),
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "type"),
|
|
|
|
|
n.defaultValue ? concat([" = ", path.call(print, "defaultValue")]) : ""
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 18:26:43 -07:00
|
|
|
case "TypeExtensionDefinition": {
|
|
|
|
|
return concat(["extend ", path.call(print, "definition")]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 18:22:04 -07:00
|
|
|
case "ObjectTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"type ",
|
|
|
|
|
path.call(print, "name"),
|
2017-06-27 19:20:53 -07:00
|
|
|
n.interfaces.length > 0
|
2017-06-27 18:51:56 -07:00
|
|
|
? concat([" implements ", join(", ", path.map(print, "interfaces"))])
|
|
|
|
|
: "",
|
|
|
|
|
printDirectives(path, print, n),
|
2017-06-27 18:26:43 -07:00
|
|
|
" {",
|
2017-06-27 19:20:53 -07:00
|
|
|
n.fields.length > 0
|
2017-06-27 18:51:56 -07:00
|
|
|
? indent(
|
|
|
|
|
concat([hardline, join(hardline, path.map(print, "fields"))])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
2017-06-27 18:22:04 -07:00
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "FieldDefinition": {
|
2017-06-27 19:49:59 -07:00
|
|
|
return concat([
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
n.arguments.length > 0
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
concat([ifBreak("", ", "), softline]),
|
|
|
|
|
path.map(print, "arguments")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
")"
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "type")
|
|
|
|
|
]);
|
2017-06-27 18:22:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:10:07 -07:00
|
|
|
case "DirectiveDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"directive ",
|
|
|
|
|
"@",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
n.arguments.length > 0
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
concat([ifBreak("", ", "), softline]),
|
|
|
|
|
path.map(print, "arguments")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
")"
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
2017-06-27 19:58:38 -07:00
|
|
|
concat([" on ", join(" | ", path.map(print, "locations"))])
|
2017-06-27 19:10:07 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:20:53 -07:00
|
|
|
case "EnumTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"enum ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
" {",
|
|
|
|
|
n.values.length > 0
|
|
|
|
|
? indent(
|
|
|
|
|
concat([hardline, join(hardline, path.map(print, "values"))])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "EnumValueDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:10:07 -07:00
|
|
|
case "InputValueDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "type"),
|
|
|
|
|
n.defaultValue ? concat([" = ", path.call(print, "defaultValue")]) : "",
|
|
|
|
|
printDirectives(path, print, n)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:27:02 -07:00
|
|
|
case "InputObjectTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"input ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
" {",
|
|
|
|
|
n.fields.length > 0
|
|
|
|
|
? indent(
|
|
|
|
|
concat([hardline, join(hardline, path.map(print, "fields"))])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 20:15:55 -07:00
|
|
|
case "SchemaDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"schema",
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
" {",
|
|
|
|
|
n.operationTypes.length > 0
|
|
|
|
|
? indent(
|
|
|
|
|
concat([
|
|
|
|
|
hardline,
|
|
|
|
|
join(hardline, path.map(print, "operationTypes"))
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "OperationTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
path.call(print, "operation"),
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "type")
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:30:58 -07:00
|
|
|
case "InterfaceTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"interface ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
" {",
|
|
|
|
|
n.fields.length > 0
|
|
|
|
|
? indent(
|
|
|
|
|
concat([hardline, join(hardline, path.map(print, "fields"))])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 12:35:40 -07:00
|
|
|
case "FragmentSpread": {
|
2017-06-06 17:42:47 -07:00
|
|
|
return concat([
|
|
|
|
|
"...",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n)
|
|
|
|
|
]);
|
2017-06-06 12:35:40 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "InlineFragment": {
|
|
|
|
|
return concat([
|
|
|
|
|
"...",
|
|
|
|
|
n.typeCondition
|
|
|
|
|
? concat([" on ", path.call(print, "typeCondition")])
|
|
|
|
|
: "",
|
2017-06-06 17:42:47 -07:00
|
|
|
printDirectives(path, print, n),
|
2017-06-06 12:35:40 -07:00
|
|
|
" ",
|
|
|
|
|
path.call(print, "selectionSet")
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-07 19:33:46 +02:00
|
|
|
case "UnionTypeDefinition": {
|
|
|
|
|
return group(
|
|
|
|
|
concat([
|
|
|
|
|
"union ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
" =",
|
|
|
|
|
ifBreak("", " "),
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
ifBreak(concat([line, " "])),
|
|
|
|
|
join(concat([line, "| "]), path.map(print, "types"))
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:34:25 -07:00
|
|
|
case "ScalarTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"scalar ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-08 12:20:41 -04:00
|
|
|
case "NonNullType": {
|
2017-06-08 12:49:24 -04:00
|
|
|
return concat([path.call(print, "type"), "!"]);
|
2017-06-08 12:20:41 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-08 12:34:35 -04:00
|
|
|
case "ListType": {
|
2017-06-08 12:49:24 -04:00
|
|
|
return concat(["[", path.call(print, "type"), "]"]);
|
2017-06-08 12:34:35 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-05 15:34:08 -04:00
|
|
|
default:
|
|
|
|
|
throw new Error("unknown graphql type: " + JSON.stringify(n.kind));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-06 17:42:47 -07:00
|
|
|
function printDirectives(path, print, n) {
|
|
|
|
|
if (n.directives.length === 0) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return concat([
|
|
|
|
|
" ",
|
|
|
|
|
group(
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
concat([ifBreak("", " "), softline]),
|
|
|
|
|
path.map(print, "directives")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-05 15:34:08 -04:00
|
|
|
module.exports = genericPrint;
|