Skip to content

Commit d135ddb

Browse files
committed
Update types in ast.ts and ArgumentNode check in printer.ts
1 parent e558091 commit d135ddb

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

src/ast.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,12 +108,13 @@ export type OperationDefinitionNode = Or<
108108
readonly kind: Kind.OPERATION_DEFINITION;
109109
readonly operation: OperationTypeNode;
110110
readonly name?: NameNode;
111-
readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
112111
readonly directives?: ReadonlyArray<DirectiveNode>;
113112
readonly selectionSet: SelectionSetNode;
114113
readonly loc?: Location;
115114
}
116-
>;
115+
> & {
116+
readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
117+
};
117118

118119
export type VariableDefinitionNode = Or<
119120
GraphQL.VariableDefinitionNode,
@@ -191,7 +192,9 @@ export type FragmentSpreadNode = Or<
191192
readonly directives?: ReadonlyArray<DirectiveNode>;
192193
readonly loc?: Location;
193194
}
194-
> & { readonly arguments?: ReadonlyArray<ArgumentNode> };
195+
> & {
196+
readonly arguments?: ReadonlyArray<ArgumentNode>;
197+
};
195198

196199
export type InlineFragmentNode = Or<
197200
GraphQL.InlineFragmentNode,

src/printer.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ASTNode } from './ast';
1+
import type { ASTNode, ArgumentNode } from './ast';
22

33
export function printString(string: string) {
44
return JSON.stringify(string);
@@ -8,8 +8,8 @@ export function printBlockString(string: string) {
88
return '"""\n' + string.replace(/"""/g, '\\"""') + '\n"""';
99
}
1010

11-
const hasItems = <T>(array: ReadonlyArray<T> | undefined | null): array is ReadonlyArray<T> =>
12-
!!(array && array.length);
11+
const hasItems = <T>(array: unknown): array is ReadonlyArray<T> =>
12+
!!(array && (array as unknown[]).length);
1313

1414
const MAX_LINE_LENGTH = 80;
1515

@@ -97,7 +97,7 @@ const nodes: {
9797
},
9898
FragmentSpread(node) {
9999
let out = '...' + node.name.value;
100-
if ('arguments' in node && Array.isArray(node.arguments) && hasItems(node.arguments))
100+
if ('arguments' in node && hasItems<ArgumentNode>(node.arguments))
101101
out += '(' + node.arguments.map(nodes.Argument!).join(', ') + ')';
102102
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
103103
return out;
@@ -110,9 +110,8 @@ const nodes: {
110110
},
111111
FragmentDefinition(node) {
112112
let out = 'fragment ' + node.name.value;
113-
if (hasItems(node.variableDefinitions)) {
113+
if (hasItems(node.variableDefinitions))
114114
out += '(' + node.variableDefinitions.map(nodes.VariableDefinition!).join(', ') + ')';
115-
}
116115
out += ' on ' + node.typeCondition.name.value;
117116
if (hasItems(node.directives)) out += ' ' + node.directives.map(nodes.Directive!).join(' ');
118117
return out + ' ' + print(node.selectionSet);

0 commit comments

Comments
 (0)