Skip to content

Commit

Permalink
Update types in ast.ts and ArgumentNode check in printer.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
kitten committed Mar 22, 2024
1 parent e558091 commit 316e6d0
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 108,13 @@ export type OperationDefinitionNode = Or<
readonly kind: Kind.OPERATION_DEFINITION;
readonly operation: OperationTypeNode;
readonly name?: NameNode;
readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
readonly directives?: ReadonlyArray<DirectiveNode>;
readonly selectionSet: SelectionSetNode;
readonly loc?: Location;
}
>;
> & {
readonly variableDefinitions?: ReadonlyArray<VariableDefinitionNode>;
};

export type VariableDefinitionNode = Or<
GraphQL.VariableDefinitionNode,
Expand Down Expand Up @@ -191,7 192,9 @@ export type FragmentSpreadNode = Or<
readonly directives?: ReadonlyArray<DirectiveNode>;
readonly loc?: Location;
}
> & { readonly arguments?: ReadonlyArray<ArgumentNode> };
> & {
readonly arguments?: ReadonlyArray<ArgumentNode>;
};

export type InlineFragmentNode = Or<
GraphQL.InlineFragmentNode,
Expand Down
11 changes: 5 additions & 6 deletions src/printer.ts
Original file line number Diff line number Diff line change
@@ -1,4 1,4 @@
import type { ASTNode } from './ast';
import type { ASTNode, ArgumentNode } from './ast';

export function printString(string: string) {
return JSON.stringify(string);
Expand All @@ -8,8 8,8 @@ export function printBlockString(string: string) {
return '"""\n' string.replace(/"""/g, '\\"""') '\n"""';
}

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

const MAX_LINE_LENGTH = 80;

Expand Down Expand Up @@ -97,7 97,7 @@ const nodes: {
},
FragmentSpread(node) {
let out = '...' node.name.value;
if ('arguments' in node && Array.isArray(node.arguments) && hasItems(node.arguments))
if ('arguments' in node && hasItems<ArgumentNode>(node.arguments))
out = '(' node.arguments.map(nodes.Argument!).join(', ') ')';
if (hasItems(node.directives)) out = ' ' node.directives.map(nodes.Directive!).join(' ');
return out;
Expand All @@ -110,9 110,8 @@ const nodes: {
},
FragmentDefinition(node) {
let out = 'fragment ' node.name.value;
if (hasItems(node.variableDefinitions)) {
if (hasItems(node.variableDefinitions))
out = '(' node.variableDefinitions.map(nodes.VariableDefinition!).join(', ') ')';
}
out = ' on ' node.typeCondition.name.value;
if (hasItems(node.directives)) out = ' ' node.directives.map(nodes.Directive!).join(' ');
return out ' ' print(node.selectionSet);
Expand Down

0 comments on commit 316e6d0

Please sign in to comment.