2017-06-05 15:34:08 -04:00
|
|
|
"use strict";
|
|
|
|
|
|
2018-05-27 21:36:12 +10:00
|
|
|
const {
|
|
|
|
|
concat,
|
|
|
|
|
join,
|
|
|
|
|
hardline,
|
|
|
|
|
line,
|
|
|
|
|
softline,
|
|
|
|
|
group,
|
|
|
|
|
indent,
|
|
|
|
|
ifBreak
|
2020-01-14 18:58:42 +08:00
|
|
|
} = require("../document").builders;
|
2018-05-27 21:36:12 +10:00
|
|
|
const { hasIgnoreComment } = require("../common/util");
|
|
|
|
|
const { isNextLineEmpty } = require("../common/util-shared");
|
2018-06-17 14:04:48 +08:00
|
|
|
const { insertPragma } = require("./pragma");
|
2017-11-12 02:10:54 -02:00
|
|
|
|
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": {
|
2018-05-20 23:14:49 +05:30
|
|
|
const parts = [];
|
|
|
|
|
path.map((pathChild, index) => {
|
|
|
|
|
parts.push(concat([pathChild.call(print)]));
|
2018-06-01 00:38:39 +08:00
|
|
|
if (index !== n.definitions.length - 1) {
|
2018-05-20 23:14:49 +05:30
|
|
|
parts.push(hardline);
|
2018-06-01 00:38:39 +08:00
|
|
|
if (
|
|
|
|
|
isNextLineEmpty(options.originalText, pathChild.getValue(), options)
|
|
|
|
|
) {
|
|
|
|
|
parts.push(hardline);
|
|
|
|
|
}
|
2018-05-20 23:14:49 +05:30
|
|
|
}
|
|
|
|
|
}, "definitions");
|
2018-06-01 00:38:39 +08:00
|
|
|
return concat([concat(parts), hardline]);
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
case "OperationDefinition": {
|
2018-02-09 13:17:48 +01:00
|
|
|
const hasOperation = options.originalText[options.locStart(n)] !== "{";
|
2017-12-31 16:45:06 +01:00
|
|
|
const hasName = !!n.name;
|
2017-06-05 22:24:01 -07:00
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
hasOperation ? n.operation : "",
|
|
|
|
|
hasOperation && hasName ? 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),
|
2017-12-31 16:45:06 +01:00
|
|
|
n.selectionSet ? (!hasOperation && !hasName ? "" : " ") : "",
|
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"),
|
2019-04-04 13:44:52 +01:00
|
|
|
n.variableDefinitions && n.variableDefinitions.length
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
concat([ifBreak("", ", "), softline]),
|
|
|
|
|
path.map(print, "variableDefinitions")
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
")"
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
2017-06-06 12:35:40 -07:00
|
|
|
" 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(
|
2017-11-12 02:10:54 -02:00
|
|
|
concat([
|
|
|
|
|
hardline,
|
|
|
|
|
join(
|
|
|
|
|
hardline,
|
|
|
|
|
path.call(
|
|
|
|
|
selectionsPath => printSequence(selectionsPath, options, print),
|
|
|
|
|
"selections"
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
])
|
2017-06-05 22:23:03 -07:00
|
|
|
),
|
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-11-12 02:10:54 -02:00
|
|
|
path.call(
|
|
|
|
|
argsPath => printSequence(argsPath, options, print),
|
|
|
|
|
"arguments"
|
|
|
|
|
)
|
2017-06-05 15:34:08 -04:00
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
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": {
|
2017-12-31 16:45:06 +01:00
|
|
|
if (n.block) {
|
2018-01-02 11:55:53 -08:00
|
|
|
return concat([
|
|
|
|
|
'"""',
|
|
|
|
|
hardline,
|
|
|
|
|
join(hardline, n.value.replace(/"""/g, "\\$&").split("\n")),
|
|
|
|
|
hardline,
|
|
|
|
|
'"""'
|
|
|
|
|
]);
|
2017-12-31 16:45:06 +01:00
|
|
|
}
|
2018-07-05 00:14:09 +08:00
|
|
|
return concat([
|
|
|
|
|
'"',
|
|
|
|
|
n.value.replace(/["\\]/g, "\\$&").replace(/\n/g, "\\n"),
|
|
|
|
|
'"'
|
|
|
|
|
]);
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
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-11-12 02:10:54 -02:00
|
|
|
path.call(
|
|
|
|
|
argsPath => printSequence(argsPath, options, print),
|
|
|
|
|
"arguments"
|
|
|
|
|
)
|
2017-06-06 17:42:47 -07:00
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
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"),
|
2019-04-04 13:47:05 +01:00
|
|
|
n.defaultValue ? concat([" = ", path.call(print, "defaultValue")]) : "",
|
|
|
|
|
printDirectives(path, print, n)
|
2017-06-06 08:26:04 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 18:26:43 -07:00
|
|
|
case "TypeExtensionDefinition": {
|
|
|
|
|
return concat(["extend ", path.call(print, "definition")]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "ObjectTypeExtension":
|
2017-06-27 18:22:04 -07:00
|
|
|
case "ObjectTypeDefinition": {
|
|
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
|
|
|
|
n.kind === "ObjectTypeExtension" ? "extend " : "",
|
2017-06-27 18:22:04 -07:00
|
|
|
"type ",
|
|
|
|
|
path.call(print, "name"),
|
2017-06-27 19:20:53 -07:00
|
|
|
n.interfaces.length > 0
|
2018-03-27 08:52:57 +02:00
|
|
|
? concat([
|
|
|
|
|
" implements ",
|
|
|
|
|
join(
|
|
|
|
|
determineInterfaceSeparator(
|
|
|
|
|
options.originalText.substr(
|
|
|
|
|
options.locStart(n),
|
|
|
|
|
options.locEnd(n)
|
|
|
|
|
)
|
|
|
|
|
),
|
|
|
|
|
path.map(print, "interfaces")
|
|
|
|
|
)
|
|
|
|
|
])
|
2017-06-27 18:51:56 -07:00
|
|
|
: "",
|
|
|
|
|
printDirectives(path, print, n),
|
2017-06-27 19:20:53 -07:00
|
|
|
n.fields.length > 0
|
2017-12-31 16:45:06 +01:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
2017-11-12 02:10:54 -02:00
|
|
|
hardline,
|
2017-12-31 16:45:06 +01:00
|
|
|
join(
|
|
|
|
|
hardline,
|
|
|
|
|
path.call(
|
|
|
|
|
fieldsPath => printSequence(fieldsPath, options, print),
|
|
|
|
|
"fields"
|
|
|
|
|
)
|
2017-11-12 02:10:54 -02:00
|
|
|
)
|
2017-12-31 16:45:06 +01:00
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ""
|
2017-06-27 18:22:04 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "FieldDefinition": {
|
2017-06-27 19:49:59 -07:00
|
|
|
return concat([
|
2018-01-02 11:55:53 -08:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
2017-06-27 19:49:59 -07:00
|
|
|
path.call(print, "name"),
|
|
|
|
|
n.arguments.length > 0
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-11-12 02:10:54 -02:00
|
|
|
path.call(
|
|
|
|
|
argsPath => printSequence(argsPath, options, print),
|
|
|
|
|
"arguments"
|
|
|
|
|
)
|
2017-06-27 19:49:59 -07:00
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
")"
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
": ",
|
2017-06-27 20:22:03 -07:00
|
|
|
path.call(print, "type"),
|
|
|
|
|
printDirectives(path, print, n)
|
2017-06-27 19:49:59 -07:00
|
|
|
]);
|
2017-06-27 18:22:04 -07:00
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:10:07 -07:00
|
|
|
case "DirectiveDefinition": {
|
|
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
2017-06-27 19:10:07 -07:00
|
|
|
"directive ",
|
|
|
|
|
"@",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
n.arguments.length > 0
|
|
|
|
|
? group(
|
|
|
|
|
concat([
|
|
|
|
|
"(",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
concat([ifBreak("", ", "), softline]),
|
2017-11-12 02:10:54 -02:00
|
|
|
path.call(
|
|
|
|
|
argsPath => printSequence(argsPath, options, print),
|
|
|
|
|
"arguments"
|
|
|
|
|
)
|
2017-06-27 19:10:07 -07:00
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
softline,
|
|
|
|
|
")"
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
2020-01-06 10:20:25 -08:00
|
|
|
n.repeatable ? " repeatable" : "",
|
2017-06-27 19:58:38 -07:00
|
|
|
concat([" on ", join(" | ", path.map(print, "locations"))])
|
2017-06-27 19:10:07 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "EnumTypeExtension":
|
2017-06-27 19:20:53 -07:00
|
|
|
case "EnumTypeDefinition": {
|
|
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
|
|
|
|
n.kind === "EnumTypeExtension" ? "extend " : "",
|
2017-06-27 19:20:53 -07:00
|
|
|
"enum ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
2017-12-31 16:45:06 +01:00
|
|
|
|
2017-06-27 19:20:53 -07:00
|
|
|
n.values.length > 0
|
2017-12-31 16:45:06 +01:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
2017-11-12 02:10:54 -02:00
|
|
|
hardline,
|
2017-12-31 16:45:06 +01:00
|
|
|
join(
|
|
|
|
|
hardline,
|
|
|
|
|
path.call(
|
|
|
|
|
valuesPath => printSequence(valuesPath, options, print),
|
|
|
|
|
"values"
|
|
|
|
|
)
|
2017-11-12 02:10:54 -02:00
|
|
|
)
|
2017-12-31 16:45:06 +01:00
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ""
|
2017-06-27 19:20:53 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "EnumValueDefinition": {
|
|
|
|
|
return concat([
|
2018-01-03 01:29:11 -03:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
2017-06-27 19:20:53 -07:00
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 19:10:07 -07:00
|
|
|
case "InputValueDefinition": {
|
|
|
|
|
return concat([
|
2018-01-03 01:29:11 -03:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? (n.description.block ? hardline : line) : "",
|
2017-06-27 19:10:07 -07:00
|
|
|
path.call(print, "name"),
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "type"),
|
|
|
|
|
n.defaultValue ? concat([" = ", path.call(print, "defaultValue")]) : "",
|
|
|
|
|
printDirectives(path, print, n)
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "InputObjectTypeExtension":
|
2017-06-27 19:27:02 -07:00
|
|
|
case "InputObjectTypeDefinition": {
|
|
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
|
|
|
|
n.kind === "InputObjectTypeExtension" ? "extend " : "",
|
2017-06-27 19:27:02 -07:00
|
|
|
"input ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
n.fields.length > 0
|
2017-12-31 16:45:06 +01:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
2017-11-12 02:10:54 -02:00
|
|
|
hardline,
|
2017-12-31 16:45:06 +01:00
|
|
|
join(
|
|
|
|
|
hardline,
|
|
|
|
|
path.call(
|
|
|
|
|
fieldsPath => printSequence(fieldsPath, options, print),
|
|
|
|
|
"fields"
|
|
|
|
|
)
|
2017-11-12 02:10:54 -02:00
|
|
|
)
|
2017-12-31 16:45:06 +01:00
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ""
|
2017-06-27 19:27:02 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-27 20:15:55 -07:00
|
|
|
case "SchemaDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
"schema",
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
" {",
|
|
|
|
|
n.operationTypes.length > 0
|
|
|
|
|
? indent(
|
|
|
|
|
concat([
|
|
|
|
|
hardline,
|
2017-11-12 02:10:54 -02:00
|
|
|
join(
|
|
|
|
|
hardline,
|
|
|
|
|
path.call(
|
|
|
|
|
opsPath => printSequence(opsPath, options, print),
|
|
|
|
|
"operationTypes"
|
|
|
|
|
)
|
|
|
|
|
)
|
2017-06-27 20:15:55 -07:00
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case "OperationTypeDefinition": {
|
|
|
|
|
return concat([
|
|
|
|
|
path.call(print, "operation"),
|
|
|
|
|
": ",
|
|
|
|
|
path.call(print, "type")
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "InterfaceTypeExtension":
|
2017-06-27 19:30:58 -07:00
|
|
|
case "InterfaceTypeDefinition": {
|
|
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
|
|
|
|
n.kind === "InterfaceTypeExtension" ? "extend " : "",
|
2017-06-27 19:30:58 -07:00
|
|
|
"interface ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
2017-12-31 16:45:06 +01:00
|
|
|
|
2017-06-27 19:30:58 -07:00
|
|
|
n.fields.length > 0
|
2017-12-31 16:45:06 +01:00
|
|
|
? concat([
|
|
|
|
|
" {",
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
2017-11-12 02:10:54 -02:00
|
|
|
hardline,
|
2017-12-31 16:45:06 +01:00
|
|
|
join(
|
|
|
|
|
hardline,
|
|
|
|
|
path.call(
|
|
|
|
|
fieldsPath => printSequence(fieldsPath, options, print),
|
|
|
|
|
"fields"
|
|
|
|
|
)
|
2017-11-12 02:10:54 -02:00
|
|
|
)
|
2017-12-31 16:45:06 +01:00
|
|
|
])
|
|
|
|
|
),
|
|
|
|
|
hardline,
|
|
|
|
|
"}"
|
|
|
|
|
])
|
|
|
|
|
: ""
|
2017-06-27 19:30:58 -07:00
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
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-12-31 16:45:06 +01:00
|
|
|
case "UnionTypeExtension":
|
2017-06-07 19:33:46 +02:00
|
|
|
case "UnionTypeDefinition": {
|
|
|
|
|
return group(
|
|
|
|
|
concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
|
|
|
|
group(
|
2017-06-07 19:33:46 +02:00
|
|
|
concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
n.kind === "UnionTypeExtension" ? "extend " : "",
|
|
|
|
|
"union ",
|
|
|
|
|
path.call(print, "name"),
|
|
|
|
|
printDirectives(path, print, n),
|
|
|
|
|
n.types.length > 0
|
|
|
|
|
? concat([
|
|
|
|
|
" =",
|
|
|
|
|
ifBreak("", " "),
|
|
|
|
|
indent(
|
|
|
|
|
concat([
|
|
|
|
|
ifBreak(concat([line, " "])),
|
|
|
|
|
join(concat([line, "| "]), path.map(print, "types"))
|
|
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
: ""
|
2017-06-07 19:33:46 +02:00
|
|
|
])
|
|
|
|
|
)
|
|
|
|
|
])
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "ScalarTypeExtension":
|
2017-06-27 19:34:25 -07:00
|
|
|
case "ScalarTypeDefinition": {
|
|
|
|
|
return concat([
|
2017-12-31 16:45:06 +01:00
|
|
|
path.call(print, "description"),
|
|
|
|
|
n.description ? hardline : "",
|
|
|
|
|
n.kind === "ScalarTypeExtension" ? "extend " : "",
|
2017-06-27 19:34:25 -07:00
|
|
|
"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:
|
2017-09-03 22:40:44 +02:00
|
|
|
/* istanbul ignore next */
|
2017-06-05 15:34:08 -04:00
|
|
|
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-11-12 02:10:54 -02:00
|
|
|
function printSequence(sequencePath, options, print) {
|
|
|
|
|
const count = sequencePath.getValue().length;
|
|
|
|
|
|
|
|
|
|
return sequencePath.map((path, i) => {
|
|
|
|
|
const printed = print(path);
|
|
|
|
|
|
|
|
|
|
if (
|
2018-05-27 21:36:12 +10:00
|
|
|
isNextLineEmpty(options.originalText, path.getValue(), options) &&
|
2017-11-12 02:10:54 -02:00
|
|
|
i < count - 1
|
|
|
|
|
) {
|
|
|
|
|
return concat([printed, hardline]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return printed;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-27 00:04:09 +11:00
|
|
|
function canAttachComment(node) {
|
|
|
|
|
return node.kind && node.kind !== "Comment";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function printComment(commentPath) {
|
|
|
|
|
const comment = commentPath.getValue();
|
2018-07-18 02:31:44 +05:30
|
|
|
if (comment.kind === "Comment") {
|
2019-11-19 04:22:24 +08:00
|
|
|
return "#" + comment.value.trimEnd();
|
2017-12-27 00:04:09 +11:00
|
|
|
}
|
2018-07-18 02:31:44 +05:30
|
|
|
|
|
|
|
|
throw new Error("Not a comment: " + JSON.stringify(comment));
|
2017-12-27 00:04:09 +11:00
|
|
|
}
|
|
|
|
|
|
2018-03-27 08:52:57 +02:00
|
|
|
function determineInterfaceSeparator(originalSource) {
|
|
|
|
|
const start = originalSource.indexOf("implements");
|
|
|
|
|
if (start === -1) {
|
|
|
|
|
throw new Error("Must implement interfaces: " + originalSource);
|
|
|
|
|
}
|
|
|
|
|
let end = originalSource.indexOf("{");
|
|
|
|
|
if (end === -1) {
|
|
|
|
|
end = originalSource.length;
|
|
|
|
|
}
|
|
|
|
|
return originalSource.substr(start, end).includes("&") ? " & " : ", ";
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-14 20:52:27 +08:00
|
|
|
function clean(node, newNode /*, parent*/) {
|
|
|
|
|
delete newNode.loc;
|
|
|
|
|
delete newNode.comments;
|
|
|
|
|
}
|
|
|
|
|
|
2017-12-26 12:23:50 +11:00
|
|
|
module.exports = {
|
|
|
|
|
print: genericPrint,
|
2018-05-14 20:52:27 +08:00
|
|
|
massageAstNode: clean,
|
2018-05-27 21:36:12 +10:00
|
|
|
hasPrettierIgnore: hasIgnoreComment,
|
2018-06-17 14:04:48 +08:00
|
|
|
insertPragma,
|
2017-12-27 00:04:09 +11:00
|
|
|
printComment,
|
|
|
|
|
canAttachComment
|
2017-12-26 12:23:50 +11:00
|
|
|
};
|