2022-04-29 14:04:17 +08:00
|
|
|
import {
|
2023-12-17 19:34:12 +00:00
|
|
|
group,
|
2022-04-29 14:04:17 +08:00
|
|
|
hardline,
|
2023-12-17 19:34:12 +00:00
|
|
|
ifBreak,
|
|
|
|
|
indent,
|
|
|
|
|
join,
|
2022-04-29 14:04:17 +08:00
|
|
|
line,
|
|
|
|
|
softline,
|
2025-11-03 01:52:26 +08:00
|
|
|
} from "../document/index.js";
|
2025-12-01 07:25:25 +08:00
|
|
|
import isNextLineEmpty from "../utilities/is-next-line-empty.js";
|
|
|
|
|
import isNonEmptyArray from "../utilities/is-non-empty-array.js";
|
|
|
|
|
import UnexpectedNodeError from "../utilities/unexpected-node-error.js";
|
2022-12-19 13:18:44 +08:00
|
|
|
import getVisitorKeys from "./get-visitor-keys.js";
|
2023-12-17 19:34:12 +00:00
|
|
|
import { locEnd, locStart } from "./loc.js";
|
|
|
|
|
import { insertPragma } from "./pragma.js";
|
2022-10-20 09:03:47 +08:00
|
|
|
import printDescription from "./print/description.js";
|
2017-06-05 15:34:08 -04:00
|
|
|
|
|
|
|
|
function genericPrint(path, options, print) {
|
2022-09-11 18:39:06 +09:00
|
|
|
const { node } = path;
|
2022-08-28 23:54:17 +08:00
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
switch (node.kind) {
|
2022-10-20 09:03:47 +08:00
|
|
|
case "Document":
|
|
|
|
|
return [
|
|
|
|
|
...join(hardline, printSequence(path, options, print, "definitions")),
|
|
|
|
|
hardline,
|
|
|
|
|
];
|
|
|
|
|
|
2017-06-05 15:34:08 -04:00
|
|
|
case "OperationDefinition": {
|
2021-03-11 04:14:08 +08:00
|
|
|
const hasOperation = options.originalText[locStart(node)] !== "{";
|
|
|
|
|
const hasName = Boolean(node.name);
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2025-11-04 20:14:15 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-11 04:14:08 +08:00
|
|
|
hasOperation ? node.operation : "",
|
2021-03-22 22:57:03 +08:00
|
|
|
hasOperation && hasName ? [" ", print("name")] : "",
|
2021-04-12 10:50:39 +02:00
|
|
|
hasOperation && !hasName && isNonEmptyArray(node.variableDefinitions)
|
|
|
|
|
? " "
|
|
|
|
|
: "",
|
2023-01-27 11:31:10 +09:00
|
|
|
printVariableDefinitions(path, print),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2023-01-27 11:31:10 +09:00
|
|
|
!hasOperation && !hasName ? "" : " ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
2022-10-08 10:44:30 +08:00
|
|
|
case "FragmentDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2025-11-04 20:14:15 +08:00
|
|
|
printDescription(path, options, print),
|
2017-06-06 12:35:40 -07:00
|
|
|
"fragment ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2023-01-27 11:31:10 +09:00
|
|
|
printVariableDefinitions(path, print),
|
2017-06-06 12:35:40 -07:00
|
|
|
" on ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("typeCondition"),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2017-06-06 12:35:40 -07:00
|
|
|
" ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "SelectionSet":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-05 15:34:08 -04:00
|
|
|
"{",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
|
|
|
|
hardline,
|
2022-03-31 21:04:12 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "selections")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
2017-06-05 15:34:08 -04:00
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "Field":
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
2021-03-22 22:57:03 +08:00
|
|
|
node.alias ? [print("alias"), ": "] : "",
|
|
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-03-31 21:04:12 +08:00
|
|
|
printSequence(path, options, print, "arguments"),
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
|
|
|
|
: "",
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
|
|
|
|
node.selectionSet ? " " : "",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
]);
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "Name":
|
2021-03-11 04:14:08 +08:00
|
|
|
return node.value;
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "StringValue":
|
2021-03-11 04:14:08 +08:00
|
|
|
if (node.block) {
|
2024-06-04 09:39:19 +08:00
|
|
|
const lines = node.value
|
|
|
|
|
.replaceAll('"""', String.raw`\"""`)
|
|
|
|
|
.split("\n");
|
2022-04-22 14:38:44 +02:00
|
|
|
if (lines.length === 1) {
|
|
|
|
|
lines[0] = lines[0].trim();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-16 07:33:09 +02:00
|
|
|
if (lines.every((line) => line === "")) {
|
|
|
|
|
lines.length = 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return join(hardline, ['"""', ...lines, '"""']);
|
2017-12-31 16:45:06 +01:00
|
|
|
}
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2018-07-05 00:14:09 +08:00
|
|
|
'"',
|
2024-06-04 09:39:19 +08:00
|
|
|
node.value
|
2025-12-21 23:44:08 +08:00
|
|
|
.replaceAll(/["\\]/g, String.raw`\$&`)
|
2024-06-04 09:39:19 +08:00
|
|
|
.replaceAll("\n", String.raw`\n`),
|
2018-07-05 00:14:09 +08:00
|
|
|
'"',
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2022-10-08 10:44:30 +08:00
|
|
|
|
2017-06-06 08:26:04 -07:00
|
|
|
case "IntValue":
|
|
|
|
|
case "FloatValue":
|
2022-10-08 10:44:30 +08:00
|
|
|
case "EnumValue":
|
2021-03-11 04:14:08 +08:00
|
|
|
return node.value;
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "BooleanValue":
|
2021-03-11 04:14:08 +08:00
|
|
|
return node.value ? "true" : "false";
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "NullValue":
|
2017-06-06 18:06:15 -07:00
|
|
|
return "null";
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "Variable":
|
2021-03-22 22:57:03 +08:00
|
|
|
return ["$", print("name")];
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "ListValue":
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
|
|
|
|
"[",
|
|
|
|
|
indent([
|
2017-06-05 23:45:15 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join([ifBreak("", ", "), softline], path.map(print, "values")),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
"]",
|
|
|
|
|
]);
|
2022-10-08 10:44:30 +08:00
|
|
|
|
2023-01-27 11:31:10 +09:00
|
|
|
case "ObjectValue": {
|
|
|
|
|
const bracketSpace =
|
|
|
|
|
options.bracketSpacing && node.fields.length > 0 ? " " : "";
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
|
|
|
|
"{",
|
2023-01-27 11:31:10 +09:00
|
|
|
bracketSpace,
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
2017-06-06 14:28:15 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join([ifBreak("", ", "), softline], path.map(print, "fields")),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
2023-01-27 11:31:10 +09:00
|
|
|
ifBreak("", bracketSpace),
|
2021-01-11 13:46:05 +08:00
|
|
|
"}",
|
|
|
|
|
]);
|
2023-01-27 11:31:10 +09:00
|
|
|
}
|
2017-06-05 15:34:08 -04:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "ObjectField":
|
|
|
|
|
case "Argument":
|
|
|
|
|
return [print("name"), ": ", print("value")];
|
|
|
|
|
|
|
|
|
|
case "Directive":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-06 17:42:47 -07:00
|
|
|
"@",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-06 17:42:47 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-03-31 21:04:12 +08:00
|
|
|
printSequence(path, options, print, "arguments"),
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-06 17:42:47 -07:00
|
|
|
: "",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 17:42:47 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "NamedType":
|
2021-03-22 22:57:03 +08:00
|
|
|
return print("name");
|
2017-06-06 08:26:04 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "VariableDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2025-11-04 20:14:15 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-22 22:57:03 +08:00
|
|
|
print("variable"),
|
2017-06-06 08:26:04 -07:00
|
|
|
": ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("type"),
|
|
|
|
|
node.defaultValue ? [" = ", print("defaultValue")] : "",
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 08:26:04 -07:00
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "ObjectTypeExtension":
|
2022-08-30 22:42:58 +08:00
|
|
|
case "ObjectTypeDefinition":
|
|
|
|
|
case "InputObjectTypeExtension":
|
|
|
|
|
case "InputObjectTypeDefinition":
|
|
|
|
|
case "InterfaceTypeExtension":
|
|
|
|
|
case "InterfaceTypeDefinition": {
|
|
|
|
|
const { kind } = node;
|
|
|
|
|
const parts = [];
|
|
|
|
|
|
|
|
|
|
if (kind.endsWith("TypeDefinition")) {
|
2022-10-20 09:03:47 +08:00
|
|
|
parts.push(printDescription(path, options, print));
|
2022-08-30 22:42:58 +08:00
|
|
|
} else {
|
|
|
|
|
parts.push("extend ");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (kind.startsWith("ObjectType")) {
|
|
|
|
|
parts.push("type");
|
|
|
|
|
} else if (kind.startsWith("InputObjectType")) {
|
|
|
|
|
parts.push("input");
|
|
|
|
|
} else {
|
|
|
|
|
parts.push("interface");
|
|
|
|
|
}
|
|
|
|
|
parts.push(" ", print("name"));
|
|
|
|
|
|
|
|
|
|
if (!kind.startsWith("InputObjectType") && node.interfaces.length > 0) {
|
|
|
|
|
parts.push(" implements ", ...printInterfaces(path, options, print));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
parts.push(printDirectives(path, print, node));
|
|
|
|
|
|
|
|
|
|
if (node.fields.length > 0) {
|
|
|
|
|
parts.push([
|
|
|
|
|
" {",
|
|
|
|
|
indent([
|
|
|
|
|
hardline,
|
|
|
|
|
join(hardline, printSequence(path, options, print, "fields")),
|
|
|
|
|
]),
|
|
|
|
|
hardline,
|
|
|
|
|
"}",
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return parts;
|
2017-06-27 18:22:04 -07:00
|
|
|
}
|
|
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "FieldDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-27 19:49:59 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-03-31 21:04:12 +08:00
|
|
|
printSequence(path, options, print, "arguments"),
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-27 19:49:59 -07:00
|
|
|
: "",
|
|
|
|
|
": ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("type"),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 18:22:04 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "DirectiveDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2017-06-27 19:10:07 -07:00
|
|
|
"directive ",
|
|
|
|
|
"@",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.arguments.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
2017-06-27 19:10:07 -07:00
|
|
|
softline,
|
2021-01-11 13:46:05 +08:00
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
2022-03-31 21:04:12 +08:00
|
|
|
printSequence(path, options, print, "arguments"),
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
])
|
2017-06-27 19:10:07 -07:00
|
|
|
: "",
|
2021-03-11 04:14:08 +08:00
|
|
|
node.repeatable ? " repeatable" : "",
|
2021-01-11 10:19:13 +08:00
|
|
|
" on ",
|
2022-10-20 09:03:47 +08:00
|
|
|
...join(" | ", path.map(print, "locations")),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:10:07 -07:00
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "EnumTypeExtension":
|
2022-10-08 10:44:30 +08:00
|
|
|
case "EnumTypeDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.kind === "EnumTypeExtension" ? "extend " : "",
|
2017-06-27 19:20:53 -07:00
|
|
|
"enum ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
|
|
|
|
node.values.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
2017-12-31 16:45:06 +01:00
|
|
|
" {",
|
2021-01-11 13:46:05 +08:00
|
|
|
indent([
|
|
|
|
|
hardline,
|
2022-03-31 21:04:12 +08:00
|
|
|
join(hardline, printSequence(path, options, print, "values")),
|
2021-01-11 13:46:05 +08:00
|
|
|
]),
|
2017-12-31 16:45:06 +01:00
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
]
|
2017-12-31 16:45:06 +01:00
|
|
|
: "",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:20:53 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "EnumValueDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:20:53 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "InputValueDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2017-06-27 19:10:07 -07:00
|
|
|
": ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("type"),
|
|
|
|
|
node.defaultValue ? [" = ", print("defaultValue")] : "",
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:10:07 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "SchemaExtension":
|
2022-03-26 08:59:04 -07:00
|
|
|
return [
|
|
|
|
|
"extend schema",
|
|
|
|
|
printDirectives(path, print, node),
|
|
|
|
|
...(node.operationTypes.length > 0
|
|
|
|
|
? [
|
|
|
|
|
" {",
|
|
|
|
|
indent([
|
|
|
|
|
hardline,
|
|
|
|
|
join(
|
|
|
|
|
hardline,
|
2022-03-31 21:04:12 +08:00
|
|
|
printSequence(path, options, print, "operationTypes"),
|
2022-03-26 08:59:04 -07:00
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
hardline,
|
|
|
|
|
"}",
|
|
|
|
|
]
|
|
|
|
|
: []),
|
|
|
|
|
];
|
2022-10-08 10:44:30 +08:00
|
|
|
|
|
|
|
|
case "SchemaDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-01-11 13:46:05 +08:00
|
|
|
"schema",
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
" {",
|
2021-03-11 04:14:08 +08:00
|
|
|
node.operationTypes.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? indent([
|
|
|
|
|
hardline,
|
|
|
|
|
join(
|
|
|
|
|
hardline,
|
2022-03-31 21:04:12 +08:00
|
|
|
printSequence(path, options, print, "operationTypes"),
|
2021-01-11 13:46:05 +08:00
|
|
|
),
|
|
|
|
|
])
|
2017-06-27 20:15:55 -07:00
|
|
|
: "",
|
|
|
|
|
hardline,
|
|
|
|
|
"}",
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 20:15:55 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "OperationTypeDefinition":
|
2022-08-28 23:54:17 +08:00
|
|
|
return [node.operation, ": ", print("type")];
|
2017-06-27 20:15:55 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "FragmentSpread":
|
2021-03-22 22:57:03 +08:00
|
|
|
return ["...", print("name"), printDirectives(path, print, node)];
|
2017-06-06 12:35:40 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "InlineFragment":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2017-06-06 12:35:40 -07:00
|
|
|
"...",
|
2021-03-22 22:57:03 +08:00
|
|
|
node.typeCondition ? [" on ", print("typeCondition")] : "",
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2017-06-06 12:35:40 -07:00
|
|
|
" ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("selectionSet"),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-06 12:35:40 -07:00
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "UnionTypeExtension":
|
2022-10-08 10:44:30 +08:00
|
|
|
case "UnionTypeDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-01-11 13:46:05 +08:00
|
|
|
group([
|
2021-03-11 04:14:08 +08:00
|
|
|
node.kind === "UnionTypeExtension" ? "extend " : "",
|
2021-01-11 13:46:05 +08:00
|
|
|
"union ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
|
|
|
|
node.types.length > 0
|
2021-01-11 13:46:05 +08:00
|
|
|
? [
|
|
|
|
|
" =",
|
|
|
|
|
ifBreak("", " "),
|
|
|
|
|
indent([
|
2024-01-07 05:49:39 -08:00
|
|
|
ifBreak([line, "| "]),
|
2021-01-11 13:46:05 +08:00
|
|
|
join([line, "| "], path.map(print, "types")),
|
|
|
|
|
]),
|
|
|
|
|
]
|
|
|
|
|
: "",
|
|
|
|
|
]),
|
|
|
|
|
]);
|
2017-06-07 19:33:46 +02:00
|
|
|
|
2017-12-31 16:45:06 +01:00
|
|
|
case "ScalarTypeExtension":
|
2022-10-08 10:44:30 +08:00
|
|
|
case "ScalarTypeDefinition":
|
2021-01-11 13:46:05 +08:00
|
|
|
return [
|
2022-10-20 09:03:47 +08:00
|
|
|
printDescription(path, options, print),
|
2021-03-11 04:14:08 +08:00
|
|
|
node.kind === "ScalarTypeExtension" ? "extend " : "",
|
2017-06-27 19:34:25 -07:00
|
|
|
"scalar ",
|
2021-03-22 22:57:03 +08:00
|
|
|
print("name"),
|
2021-03-11 04:14:08 +08:00
|
|
|
printDirectives(path, print, node),
|
2021-01-11 13:46:05 +08:00
|
|
|
];
|
2017-06-27 19:34:25 -07:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "NonNullType":
|
2021-03-22 22:57:03 +08:00
|
|
|
return [print("type"), "!"];
|
2017-06-08 12:20:41 -04:00
|
|
|
|
2022-10-08 10:44:30 +08:00
|
|
|
case "ListType":
|
2021-03-22 22:57:03 +08:00
|
|
|
return ["[", print("type"), "]"];
|
2017-06-08 12:34:35 -04:00
|
|
|
|
2017-06-05 15:34:08 -04:00
|
|
|
default:
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore next */
|
2022-08-29 18:16:38 +08:00
|
|
|
throw new UnexpectedNodeError(node, "Graphql", "kind");
|
2017-06-05 15:34:08 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
function printDirectives(path, print, node) {
|
|
|
|
|
if (node.directives.length === 0) {
|
2017-06-06 17:42:47 -07:00
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-03 02:57:17 +09:00
|
|
|
const printed = join(line, path.map(print, "directives"));
|
|
|
|
|
|
2021-03-11 04:14:08 +08:00
|
|
|
if (
|
|
|
|
|
node.kind === "FragmentDefinition" ||
|
|
|
|
|
node.kind === "OperationDefinition"
|
|
|
|
|
) {
|
2021-01-11 13:46:05 +08:00
|
|
|
return group([line, printed]);
|
2020-09-03 02:57:17 +09:00
|
|
|
}
|
|
|
|
|
|
2021-01-11 13:46:05 +08:00
|
|
|
return [" ", group(indent([softline, printed]))];
|
2017-06-06 17:42:47 -07:00
|
|
|
}
|
|
|
|
|
|
2022-03-31 21:04:12 +08:00
|
|
|
function printSequence(path, options, print, property) {
|
2022-10-20 09:03:47 +08:00
|
|
|
return path.map(({ isLast, node }) => {
|
2021-03-22 22:57:03 +08:00
|
|
|
const printed = print();
|
2017-11-12 02:10:54 -02:00
|
|
|
|
2023-02-10 14:23:42 +08:00
|
|
|
if (!isLast && isNextLineEmpty(options.originalText, locEnd(node))) {
|
2021-01-11 13:46:05 +08:00
|
|
|
return [printed, hardline];
|
2017-11-12 02:10:54 -02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return printed;
|
2022-03-31 21:04:12 +08:00
|
|
|
}, property);
|
2017-11-12 02:10:54 -02:00
|
|
|
}
|
|
|
|
|
|
2025-10-12 19:56:44 +08:00
|
|
|
function canAttachComment(node /* , ancestors */) {
|
2022-08-28 23:54:17 +08:00
|
|
|
return node.kind !== "Comment";
|
2017-12-27 00:04:09 +11:00
|
|
|
}
|
|
|
|
|
|
2025-09-12 01:34:34 +08:00
|
|
|
function printComment({ node: comment }) {
|
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
|
|
|
|
2022-10-22 23:33:08 +08:00
|
|
|
/* c8 ignore next */
|
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
|
|
|
}
|
|
|
|
|
|
2020-01-21 19:18:53 +08:00
|
|
|
function printInterfaces(path, options, print) {
|
2022-09-11 18:39:06 +09:00
|
|
|
const { node } = path;
|
2020-01-21 19:18:53 +08:00
|
|
|
const parts = [];
|
|
|
|
|
const { interfaces } = node;
|
2022-08-28 23:54:17 +08:00
|
|
|
const printed = path.map(print, "interfaces");
|
2020-01-21 19:18:53 +08:00
|
|
|
|
|
|
|
|
for (let index = 0; index < interfaces.length; index++) {
|
|
|
|
|
const interfaceNode = interfaces[index];
|
|
|
|
|
parts.push(printed[index]);
|
2020-04-10 21:09:54 +08:00
|
|
|
const nextInterfaceNode = interfaces[index + 1];
|
|
|
|
|
if (nextInterfaceNode) {
|
|
|
|
|
const textBetween = options.originalText.slice(
|
|
|
|
|
interfaceNode.loc.end,
|
|
|
|
|
nextInterfaceNode.loc.start,
|
|
|
|
|
);
|
|
|
|
|
const hasComment = textBetween.includes("#");
|
|
|
|
|
|
2022-05-08 20:20:34 +08:00
|
|
|
parts.push(" &", hasComment ? line : " ");
|
2020-04-10 21:09:54 +08:00
|
|
|
}
|
2018-03-27 08:52:57 +02:00
|
|
|
}
|
2020-04-10 21:09:54 +08:00
|
|
|
|
2020-01-21 19:18:53 +08:00
|
|
|
return parts;
|
2018-03-27 08:52:57 +02:00
|
|
|
}
|
|
|
|
|
|
2023-01-27 11:31:10 +09:00
|
|
|
function printVariableDefinitions(path, print) {
|
|
|
|
|
const { node } = path;
|
|
|
|
|
if (!isNonEmptyArray(node.variableDefinitions)) {
|
|
|
|
|
return "";
|
|
|
|
|
}
|
|
|
|
|
return group([
|
|
|
|
|
"(",
|
|
|
|
|
indent([
|
|
|
|
|
softline,
|
|
|
|
|
join(
|
|
|
|
|
[ifBreak("", ", "), softline],
|
|
|
|
|
path.map(print, "variableDefinitions"),
|
|
|
|
|
),
|
|
|
|
|
]),
|
|
|
|
|
softline,
|
|
|
|
|
")",
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-04 14:32:54 +08:00
|
|
|
function clean(original, cloned /* , parent */) {
|
2022-04-22 14:38:44 +02:00
|
|
|
// We print single line `""" string """` as multiple line string,
|
|
|
|
|
// and the parser ignores space in multiple line string
|
2024-02-04 14:32:54 +08:00
|
|
|
if (
|
|
|
|
|
original.kind === "StringValue" &&
|
|
|
|
|
original.block &&
|
|
|
|
|
!original.value.includes("\n")
|
|
|
|
|
) {
|
|
|
|
|
cloned.value = original.value.trim();
|
2022-04-22 14:38:44 +02:00
|
|
|
}
|
|
|
|
|
}
|
2020-11-05 14:12:14 +08:00
|
|
|
clean.ignoredProperties = new Set(["loc", "comments"]);
|
2018-05-14 20:52:27 +08:00
|
|
|
|
2020-11-10 09:12:46 +08:00
|
|
|
function hasPrettierIgnore(path) {
|
2022-09-11 18:39:06 +09:00
|
|
|
const { node } = path;
|
2022-04-24 10:28:20 +08:00
|
|
|
return node?.comments?.some(
|
|
|
|
|
(comment) => comment.value.trim() === "prettier-ignore",
|
2020-11-10 09:12:46 +08:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-21 10:25:39 +08:00
|
|
|
const printer = {
|
2017-12-26 12:23:50 +11:00
|
|
|
print: genericPrint,
|
2018-05-14 20:52:27 +08:00
|
|
|
massageAstNode: clean,
|
2020-11-10 09:12:46 +08:00
|
|
|
hasPrettierIgnore,
|
2018-06-17 14:04:48 +08:00
|
|
|
insertPragma,
|
2017-12-27 00:04:09 +11:00
|
|
|
printComment,
|
|
|
|
|
canAttachComment,
|
2022-08-28 23:54:17 +08:00
|
|
|
getVisitorKeys,
|
2017-12-26 12:23:50 +11:00
|
|
|
};
|
2022-02-21 10:25:39 +08:00
|
|
|
|
|
|
|
|
export default printer;
|