Skip to content

Commit dee9df7

Browse files
authored
fix(document-builder): enum variables key names (#1294)
1 parent 0e6618f commit dee9df7

File tree

37 files changed

+678
-154
lines changed

37 files changed

+678
-154
lines changed

examples/70_type-level/selection-sets.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ const getPokemonsLike = async (filter: Graffle.SelectionSets.Query.pokemons$Argu
114114
name: true,
115115
})
116116

117-
// todo add test coverage for $ stripping on arguments.
118117
const pokemons = await getPokemonsLike({ $type: `water` })
119118

120119
// We don't lose any type safety. :)

examples/__outputs__/10_transport-http/transport-http_extension_headers__dynamicHeaders.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
headers: Headers {
55
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
66
'content-type': 'application/json',
7-
'x-sent-at-time': '1734878384165'
7+
'x-sent-at-time': '1735104630184'
88
},
99
method: 'post',
1010
url: 'http://localhost:3000/graphql',

examples/__outputs__/20_output/output_envelope.output.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
headers: Headers {
1717
'content-type': 'application/graphql-response+json; charset=utf-8',
1818
'content-length': '142',
19-
date: 'Sun, 22 Dec 2024 14:39:45 GMT',
19+
date: 'Wed, 25 Dec 2024 05:30:30 GMT',
2020
connection: 'keep-alive',
2121
'keep-alive': 'timeout=5'
2222
},

examples/__outputs__/60_extension/extension_opentelemetry__opentelemetry.output.txt

Lines changed: 29 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
}
1010
},
1111
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
12-
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
13-
parentId: '45c84f0a43771166',
12+
traceId: '2d49d55d453267b20a3306c412c2c4bb',
13+
parentId: '3294547ab163e342',
1414
traceState: undefined,
1515
name: 'encode',
16-
id: 'f723a4047c4f8246',
16+
id: '1ef2540c3612ae74',
1717
kind: 0,
18-
timestamp: 1734878385686000,
19-
duration: 1952.542,
18+
timestamp: 1735104630493000,
19+
duration: 965.459,
2020
attributes: {},
2121
status: { code: 0 },
2222
events: [],
@@ -33,14 +33,14 @@
3333
}
3434
},
3535
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
36-
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
37-
parentId: '45c84f0a43771166',
36+
traceId: '2d49d55d453267b20a3306c412c2c4bb',
37+
parentId: '3294547ab163e342',
3838
traceState: undefined,
3939
name: 'pack',
40-
id: '8404e21eb12fbc92',
40+
id: '12dbd874c3b489ce',
4141
kind: 0,
42-
timestamp: 1734878385689000,
43-
duration: 12830.875,
42+
timestamp: 1735104630495000,
43+
duration: 10833.958,
4444
attributes: {},
4545
status: { code: 0 },
4646
events: [],
@@ -57,14 +57,14 @@
5757
}
5858
},
5959
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
60-
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
61-
parentId: '45c84f0a43771166',
60+
traceId: '2d49d55d453267b20a3306c412c2c4bb',
61+
parentId: '3294547ab163e342',
6262
traceState: undefined,
6363
name: 'exchange',
64-
id: 'c5691fe0bcfc9991',
64+
id: '837a3ca4af1a71df',
6565
kind: 0,
66-
timestamp: 1734878385703000,
67-
duration: 20900.792,
66+
timestamp: 1735104630506000,
67+
duration: 19948.584,
6868
attributes: {},
6969
status: { code: 0 },
7070
events: [],
@@ -81,14 +81,14 @@
8181
}
8282
},
8383
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
84-
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
85-
parentId: '45c84f0a43771166',
84+
traceId: '2d49d55d453267b20a3306c412c2c4bb',
85+
parentId: '3294547ab163e342',
8686
traceState: undefined,
8787
name: 'unpack',
88-
id: 'bafdd78624e21445',
88+
id: '392e6c2ad292f089',
8989
kind: 0,
90-
timestamp: 1734878385724000,
91-
duration: 992.417,
90+
timestamp: 1735104630526000,
91+
duration: 1046.75,
9292
attributes: {},
9393
status: { code: 0 },
9494
events: [],
@@ -105,14 +105,14 @@
105105
}
106106
},
107107
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
108-
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
109-
parentId: '45c84f0a43771166',
108+
traceId: '2d49d55d453267b20a3306c412c2c4bb',
109+
parentId: '3294547ab163e342',
110110
traceState: undefined,
111111
name: 'decode',
112-
id: '32fa8b5942d69ca9',
112+
id: 'f6f9b43acdbbb5d3',
113113
kind: 0,
114-
timestamp: 1734878385725000,
115-
duration: 446.542,
114+
timestamp: 1735104630528000,
115+
duration: 480.041,
116116
attributes: {},
117117
status: { code: 0 },
118118
events: [],
@@ -129,14 +129,14 @@
129129
}
130130
},
131131
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
132-
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
132+
traceId: '2d49d55d453267b20a3306c412c2c4bb',
133133
parentId: undefined,
134134
traceState: undefined,
135135
name: 'request',
136-
id: '45c84f0a43771166',
136+
id: '3294547ab163e342',
137137
kind: 0,
138-
timestamp: 1734878385686000,
139-
duration: 39771.791,
138+
timestamp: 1735104630492000,
139+
duration: 35822.333,
140140
attributes: {},
141141
status: { code: 0 },
142142
events: [],

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"serve:pokemon": "tsx tests/_/services/pokemonManual.ts",
9393
"gen:graffle": "pnpm gen:graffle:tests && pnpm build && cd website && pnpm gen:graffle",
9494
"gen:graffle:tests": "tsx tests/_/schemas/generate.ts && pnpm graffle --project src/extensions/SchemaErrors/tests/fixture",
95-
"gen:graffle:examples": "pnpm build && cd examples && pnpm i && pnpm gen:graffle",
95+
"examples:gen:graffle": "pnpm build && cd examples && pnpm i && pnpm gen:graffle",
9696
"examples:link-mode": "cd examples && pnpm add ..",
9797
"graffle": "tsx ./src/generator/cli/generate.ts",
9898
"gen:examples": "tsx scripts/generate-examples-derivatives/generate.ts && pnpm format",

src/documentBuilder/Select/$parseSelection.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { Grafaid } from '../../lib/grafaid/__.js'
2+
import { Select } from './../Select/__.js'
13
import type { SelectionSet } from './_.js'
24
import { Arguments, Directive, Indicator, InlineFragment, SelectAlias, SelectScalarsWildcard } from './_.js'
35

@@ -88,9 +90,15 @@ export const parseSelectionInlineFragment = (key: string, value: any): ParsedInl
8890

8991
export const parseSelection = (key: string, value: any): ParsedSelection => {
9092
if (key === Arguments.key) {
93+
const argumentsNormalized = Grafaid.mapVariables(value, (key, value) => {
94+
return {
95+
key: Select.Arguments.enumKeyPrefixStrip(key),
96+
value,
97+
}
98+
})
9199
return {
92100
type: `Arguments`,
93-
arguments: value,
101+
arguments: argumentsNormalized ?? {},
94102
}
95103
}
96104

src/documentBuilder/Select/arguments.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,6 @@ export const enumKeyPrefix = `$`
88

99
export const enumKeyPrefixPattern = /^\$/g
1010

11+
export const enumKeyPrefixStrip = (key: string) => key.replace(enumKeyPrefixPattern, ``)
12+
1113
export const isEnumKey = (key: string) => key.startsWith(enumKeyPrefix)

src/documentBuilder/SelectGraphQLMapper/__snapshots__/toGraphQL.test.ts.snap

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -675,6 +675,52 @@ exports[`( variables: false; scalars: (none) ) - Query - args - custom scalar -
675675
"
676676
`;
677677

678+
exports[`( variables: false; scalars: (none) ) - Query - args - enum > args - enum 1`] = `
679+
"
680+
681+
--------------GRAFFLE QUERY-------------
682+
{
683+
"stringWithArgEnum": {
684+
"$": {
685+
"$ABCEnum": "A"
686+
}
687+
}
688+
}
689+
--------GRAPHQL DOCUMENT & VARIABLES--------
690+
{
691+
stringWithArgEnum(ABCEnum: A)
692+
}
693+
----------------
694+
{
695+
"$default": {}
696+
}
697+
"
698+
`;
699+
700+
exports[`( variables: false; scalars: (none) ) - Query - args - input object enum > args - input object enum 1`] = `
701+
"
702+
703+
--------------GRAFFLE QUERY-------------
704+
{
705+
"stringWithArgInputObjectEnum": {
706+
"$": {
707+
"input": {
708+
"$abcEnum": "A"
709+
}
710+
}
711+
}
712+
}
713+
--------GRAPHQL DOCUMENT & VARIABLES--------
714+
{
715+
stringWithArgInputObjectEnum(input: {abcEnum: "A"})
716+
}
717+
----------------
718+
{
719+
"$default": {}
720+
}
721+
"
722+
`;
723+
678724
exports[`( variables: false; scalars: (none) ) - Query - args - object with args (empty object) > args - object with args (empty object) 1`] = `
679725
"
680726
@@ -2146,6 +2192,58 @@ query ($input: InputObjectNested) {
21462192
"
21472193
`;
21482194

2195+
exports[`( variables: true; scalars: (none) ) - Query - args - enum > args - enum 1`] = `
2196+
"
2197+
2198+
--------------GRAFFLE QUERY-------------
2199+
{
2200+
"stringWithArgEnum": {
2201+
"$": {
2202+
"$ABCEnum": "A"
2203+
}
2204+
}
2205+
}
2206+
--------GRAPHQL DOCUMENT & VARIABLES--------
2207+
query ($ABCEnum: ABCEnum) {
2208+
stringWithArgEnum(ABCEnum: $ABCEnum)
2209+
}
2210+
----------------
2211+
{
2212+
"$default": {
2213+
"ABCEnum": "A"
2214+
}
2215+
}
2216+
"
2217+
`;
2218+
2219+
exports[`( variables: true; scalars: (none) ) - Query - args - input object enum > args - input object enum 1`] = `
2220+
"
2221+
2222+
--------------GRAFFLE QUERY-------------
2223+
{
2224+
"stringWithArgInputObjectEnum": {
2225+
"$": {
2226+
"input": {
2227+
"$abcEnum": "A"
2228+
}
2229+
}
2230+
}
2231+
}
2232+
--------GRAPHQL DOCUMENT & VARIABLES--------
2233+
query ($input: InputObjectEnum!) {
2234+
stringWithArgInputObjectEnum(input: $input)
2235+
}
2236+
----------------
2237+
{
2238+
"$default": {
2239+
"input": {
2240+
"abcEnum": "A"
2241+
}
2242+
}
2243+
}
2244+
"
2245+
`;
2246+
21492247
exports[`( variables: true; scalars: (none) ) - Query - args - object with args (empty object) > args - object with args (empty object) 1`] = `
21502248
"
21512249

src/documentBuilder/SelectGraphQLMapper/nodes/1_Document.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const toGraphQLDocument = (
2121
definitions: operationsAndVariables.map(_ => _.operation),
2222
})
2323

24-
const operationsVariables = Object.fromEntries(operationsAndVariables.map((_): [string, Grafaid.Variables] => {
24+
const operationsVariables = Object.fromEntries(operationsAndVariables.map((_) => {
2525
const name = _.operation.name?.value ?? defaultOperationName
2626
return [name, _.variables]
2727
}))

src/documentBuilder/SelectGraphQLMapper/nodes/2_OperationDefinition.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,5 @@ export const toGraphQLOperationDefinition: GraphQLPreOperationMapper<
5252
variables,
5353
}
5454
}
55+
56+
// TODO: optimization: skip when using SDDM b/c we don't need $-directed enums

0 commit comments

Comments
 (0)