-
Notifications
You must be signed in to change notification settings - Fork 4
feat: add support for fragment arguments #23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 6 commits
ec70269
6a6e2ae
762c0f9
08f43d4
b1279ee
197f1d3
222cbc1
72eca4a
a48b9cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@0no-co/graphql.web': minor | ||
--- | ||
|
||
Add support for variable definitions on fragments and arguments on fragment spreads (Fragment Arguments Spec Addition) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,94 @@ | |
).toBe('[Type!]'); | ||
}); | ||
|
||
it('prints fragment-definition with variables', () => { | ||
expect( | ||
print({ | ||
kind: Kind.FRAGMENT_DEFINITION, | ||
directives: [], | ||
name: { | ||
kind: Kind.NAME, | ||
value: 'x', | ||
}, | ||
typeCondition: { | ||
kind: Kind.NAMED_TYPE, | ||
name: { | ||
kind: Kind.NAME, | ||
value: 'Type', | ||
}, | ||
}, | ||
variableDefinitions: [ | ||
{ | ||
kind: Kind.VARIABLE_DEFINITION, | ||
type: { | ||
kind: Kind.NAMED_TYPE, | ||
name: { | ||
kind: Kind.NAME, | ||
value: 'Int', | ||
}, | ||
}, | ||
variable: { | ||
kind: Kind.VARIABLE, | ||
name: { | ||
kind: Kind.NAME, | ||
value: 'var', | ||
}, | ||
}, | ||
defaultValue: { | ||
kind: Kind.INT, | ||
value: '1', | ||
}, | ||
directives: [], | ||
}, | ||
], | ||
selectionSet: { | ||
kind: Kind.SELECTION_SET, | ||
selections: [ | ||
{ | ||
alias: undefined, | ||
kind: Kind.FIELD, | ||
directives: [], | ||
selectionSet: undefined, | ||
arguments: [], | ||
name: { | ||
kind: Kind.NAME, | ||
value: 'field', | ||
}, | ||
}, | ||
], | ||
}, | ||
} as any) | ||
).toBe(`fragment x($var: Int = 1) on Type { | ||
field | ||
}`); | ||
}); | ||
|
||
it('prints fragment-spread with arguments', () => { | ||
expect( | ||
print({ | ||
kind: Kind.FRAGMENT_SPREAD, | ||
directives: [], | ||
name: { | ||
kind: Kind.NAME, | ||
value: 'x', | ||
}, | ||
arguments: [ | ||
{ | ||
kind: 'Argument', | ||
name: { | ||
kind: 'Name', | ||
value: 'var', | ||
}, | ||
value: { | ||
kind: 'IntValue', | ||
value: '2', | ||
}, | ||
}, | ||
], | ||
} as any) | ||
).toBe(`...x(var: 2)`); | ||
}); | ||
|
||
// NOTE: The shim won't throw for invalid AST nodes | ||
it('returns empty strings for invalid AST', () => { | ||
const badAST = { random: 'Data' }; | ||
|
@@ -184,22 +272,22 @@ | |
|
||
it('Handles empty array selections', () => { | ||
const document: DocumentNode = { | ||
kind: Kind.DOCUMENT, | ||
Check failure on line 275 in src/__tests__/printer.test.ts
|
||
definitions: [ | ||
{ | ||
kind: Kind.OPERATION_DEFINITION, | ||
Check failure on line 278 in src/__tests__/printer.test.ts
|
||
operation: OperationTypeNode.QUERY, | ||
name: undefined, | ||
selectionSet: { | ||
kind: Kind.SELECTION_SET, | ||
Check failure on line 282 in src/__tests__/printer.test.ts
|
||
selections: [ | ||
{ | ||
kind: Kind.FIELD, | ||
Check failure on line 285 in src/__tests__/printer.test.ts
|
||
name: { kind: Kind.NAME, value: 'id' }, | ||
Check failure on line 286 in src/__tests__/printer.test.ts
|
||
alias: undefined, | ||
arguments: [], | ||
directives: [], | ||
selectionSet: { kind: Kind.SELECTION_SET, selections: [] }, | ||
Check failure on line 290 in src/__tests__/printer.test.ts
|
||
}, | ||
], | ||
}, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,6 +25,7 @@ | |
} | ||
|
||
const leadingRe = / +(?=[^\s])/y; | ||
const nameRe = /[A-Za-z_][0-9A-Za-z_]*/y; | ||
function blockString(string: string) { | ||
const lines = string.split('\n'); | ||
let out = ''; | ||
|
@@ -84,7 +85,7 @@ | |
|
||
function nameNode(): ast.NameNode { | ||
return { | ||
kind: 'Name' as Kind.NAME, | ||
Check failure on line 88 in src/parser.ts
|
||
value: name(), | ||
}; | ||
} | ||
|
@@ -106,7 +107,7 @@ | |
idx++; | ||
ignored(); | ||
return { | ||
kind: 'ListValue' as Kind.LIST, | ||
Check failure on line 110 in src/parser.ts
|
||
values, | ||
}; | ||
|
||
|
@@ -119,7 +120,7 @@ | |
if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error('ObjectField'); | ||
ignored(); | ||
fields.push({ | ||
kind: 'ObjectField' as Kind.OBJECT_FIELD, | ||
Check failure on line 123 in src/parser.ts
|
||
name, | ||
value: value(constant), | ||
}); | ||
|
@@ -245,17 +246,22 @@ | |
}; | ||
} | ||
|
||
function arguments_(constant: boolean): ast.ArgumentNode[] | undefined { | ||
function arguments_( | ||
constant: boolean, | ||
fragmentArgument?: boolean | ||
): ast.ArgumentNode[] | ast.FragmentArgumentNode[] | undefined { | ||
if (input.charCodeAt(idx) === 40 /*'('*/) { | ||
const args: ast.ArgumentNode[] = []; | ||
const args: ast.ArgumentNode[] | ast.FragmentArgumentNode[] = []; | ||
idx++; | ||
ignored(); | ||
do { | ||
const name = nameNode(); | ||
if (input.charCodeAt(idx++) !== 58 /*':'*/) throw error('Argument'); | ||
ignored(); | ||
args.push({ | ||
kind: 'Argument' as Kind.ARGUMENT, | ||
kind: fragmentArgument | ||
? ('FragmentArgument' as Kind.FRAGMENT_ARGUMENT) | ||
: ('Argument' as Kind.ARGUMENT), | ||
name, | ||
value: value(constant), | ||
}); | ||
|
@@ -357,6 +363,8 @@ | |
selections.push({ | ||
kind: 'FragmentSpread' as Kind.FRAGMENT_SPREAD, | ||
name: nameNode(), | ||
// @ts-expect-error | ||
kitten marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
arguments: arguments_(false, true) as readonly ast.FragmentArgumentNode[], | ||
directives: directives(false), | ||
}); | ||
} | ||
|
@@ -377,6 +385,8 @@ | |
selections.push({ | ||
kind: 'FragmentSpread' as Kind.FRAGMENT_SPREAD, | ||
name: nameNode(), | ||
// @ts-expect-error | ||
arguments: arguments_(false, true) as readonly ast.FragmentArgumentNode[], | ||
directives: directives(false), | ||
}); | ||
} | ||
|
@@ -460,19 +470,27 @@ | |
} | ||
|
||
function fragmentDefinition(description?: ast.StringValueNode): ast.FragmentDefinitionNode { | ||
const name = nameNode(); | ||
if (input.charCodeAt(idx++) !== 111 /*'o'*/ || input.charCodeAt(idx++) !== 110 /*'n'*/) | ||
throw error('FragmentDefinition'); | ||
let _name: string | undefined; | ||
let _condition: string | undefined; | ||
if ((_name = advance(nameRe)) == null) throw error('FragmentDefinition'); | ||
kitten marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
const _variableDefinitions = variableDefinitions(); | ||
if (advance(nameRe) !== 'on') throw error('FragmentDefinition'); | ||
ignored(); | ||
if ((_condition = advance(nameRe)) == null) throw error('FragmentDefinition'); | ||
ignored(); | ||
const _directives = directives(false); | ||
if (input.charCodeAt(idx++) !== 123 /*'{'*/) throw error('FragmentDefinition'); | ||
ignored(); | ||
const fragDef: ast.FragmentDefinitionNode = { | ||
kind: 'FragmentDefinition' as Kind.FRAGMENT_DEFINITION, | ||
name, | ||
name: { kind: 'Name' as Kind.NAME, value: _name }, | ||
typeCondition: { | ||
kind: 'NamedType' as Kind.NAMED_TYPE, | ||
name: nameNode(), | ||
name: { kind: 'Name' as Kind.NAME, value: _condition }, | ||
}, | ||
directives: directives(false), | ||
selectionSet: selectionSetStart(), | ||
variableDefinitions: _variableDefinitions, | ||
directives: _directives, | ||
selectionSet: selectionSet(), | ||
}; | ||
if (description) { | ||
fragDef.description = description; | ||
|
Uh oh!
There was an error while loading. Please reload this page.