Skip to content

Commit

Permalink
fix(document-builder): enum variables key names (#1294)
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt authored Dec 25, 2024
1 parent 0e6618f commit dee9df7
Show file tree
Hide file tree
Showing 37 changed files with 678 additions and 154 deletions.
1 change: 0 additions & 1 deletion examples/70_type-level/selection-sets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,6 @@ const getPokemonsLike = async (filter: Graffle.SelectionSets.Query.pokemons$Argu
name: true,
})

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

// We don't lose any type safety. :)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
headers: Headers {
accept: 'application/graphql-response+json; charset=utf-8, application/json; charset=utf-8',
'content-type': 'application/json',
'x-sent-at-time': '1734878384165'
'x-sent-at-time': '1735104630184'
},
method: 'post',
url: 'http://localhost:3000/graphql',
Expand Down
2 changes: 1 addition & 1 deletion examples/__outputs__/20_output/output_envelope.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
headers: Headers {
'content-type': 'application/graphql-response+json; charset=utf-8',
'content-length': '142',
date: 'Sun, 22 Dec 2024 14:39:45 GMT',
date: 'Wed, 25 Dec 2024 05:30:30 GMT',
connection: 'keep-alive',
'keep-alive': 'timeout=5'
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
parentId: '45c84f0a43771166',
traceId: '2d49d55d453267b20a3306c412c2c4bb',
parentId: '3294547ab163e342',
traceState: undefined,
name: 'encode',
id: 'f723a4047c4f8246',
id: '1ef2540c3612ae74',
kind: 0,
timestamp: 1734878385686000,
duration: 1952.542,
timestamp: 1735104630493000,
duration: 965.459,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -33,14 +33,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
parentId: '45c84f0a43771166',
traceId: '2d49d55d453267b20a3306c412c2c4bb',
parentId: '3294547ab163e342',
traceState: undefined,
name: 'pack',
id: '8404e21eb12fbc92',
id: '12dbd874c3b489ce',
kind: 0,
timestamp: 1734878385689000,
duration: 12830.875,
timestamp: 1735104630495000,
duration: 10833.958,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -57,14 +57,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
parentId: '45c84f0a43771166',
traceId: '2d49d55d453267b20a3306c412c2c4bb',
parentId: '3294547ab163e342',
traceState: undefined,
name: 'exchange',
id: 'c5691fe0bcfc9991',
id: '837a3ca4af1a71df',
kind: 0,
timestamp: 1734878385703000,
duration: 20900.792,
timestamp: 1735104630506000,
duration: 19948.584,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -81,14 +81,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
parentId: '45c84f0a43771166',
traceId: '2d49d55d453267b20a3306c412c2c4bb',
parentId: '3294547ab163e342',
traceState: undefined,
name: 'unpack',
id: 'bafdd78624e21445',
id: '392e6c2ad292f089',
kind: 0,
timestamp: 1734878385724000,
duration: 992.417,
timestamp: 1735104630526000,
duration: 1046.75,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -105,14 +105,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
parentId: '45c84f0a43771166',
traceId: '2d49d55d453267b20a3306c412c2c4bb',
parentId: '3294547ab163e342',
traceState: undefined,
name: 'decode',
id: '32fa8b5942d69ca9',
id: 'f6f9b43acdbbb5d3',
kind: 0,
timestamp: 1734878385725000,
duration: 446.542,
timestamp: 1735104630528000,
duration: 480.041,
attributes: {},
status: { code: 0 },
events: [],
Expand All @@ -129,14 +129,14 @@
}
},
instrumentationScope: { name: 'graffle', version: undefined, schemaUrl: undefined },
traceId: '7adcc8f82ea6b22e5d53dc12a862e3b7',
traceId: '2d49d55d453267b20a3306c412c2c4bb',
parentId: undefined,
traceState: undefined,
name: 'request',
id: '45c84f0a43771166',
id: '3294547ab163e342',
kind: 0,
timestamp: 1734878385686000,
duration: 39771.791,
timestamp: 1735104630492000,
duration: 35822.333,
attributes: {},
status: { code: 0 },
events: [],
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"serve:pokemon": "tsx tests/_/services/pokemonManual.ts",
"gen:graffle": "pnpm gen:graffle:tests && pnpm build && cd website && pnpm gen:graffle",
"gen:graffle:tests": "tsx tests/_/schemas/generate.ts && pnpm graffle --project src/extensions/SchemaErrors/tests/fixture",
"gen:graffle:examples": "pnpm build && cd examples && pnpm i && pnpm gen:graffle",
"examples:gen:graffle": "pnpm build && cd examples && pnpm i && pnpm gen:graffle",
"examples:link-mode": "cd examples && pnpm add ..",
"graffle": "tsx ./src/generator/cli/generate.ts",
"gen:examples": "tsx scripts/generate-examples-derivatives/generate.ts && pnpm format",
Expand Down
10 changes: 9 additions & 1 deletion src/documentBuilder/Select/$parseSelection.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Grafaid } from '../../lib/grafaid/__.js'
import { Select } from './../Select/__.js'
import type { SelectionSet } from './_.js'
import { Arguments, Directive, Indicator, InlineFragment, SelectAlias, SelectScalarsWildcard } from './_.js'

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

export const parseSelection = (key: string, value: any): ParsedSelection => {
if (key === Arguments.key) {
const argumentsNormalized = Grafaid.mapVariables(value, (key, value) => {
return {
key: Select.Arguments.enumKeyPrefixStrip(key),
value,
}
})
return {
type: `Arguments`,
arguments: value,
arguments: argumentsNormalized ?? {},
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/documentBuilder/Select/arguments.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,6 @@ export const enumKeyPrefix = `$`

export const enumKeyPrefixPattern = /^\$/g

export const enumKeyPrefixStrip = (key: string) => key.replace(enumKeyPrefixPattern, ``)

export const isEnumKey = (key: string) => key.startsWith(enumKeyPrefix)
Original file line number Diff line number Diff line change
Expand Up @@ -675,6 +675,52 @@ exports[`( variables: false; scalars: (none) ) - Query - args - custom scalar -
"
`;

exports[`( variables: false; scalars: (none) ) - Query - args - enum > args - enum 1`] = `
"
--------------GRAFFLE QUERY-------------
{
"stringWithArgEnum": {
"$": {
"$ABCEnum": "A"
}
}
}
--------GRAPHQL DOCUMENT & VARIABLES--------
{
stringWithArgEnum(ABCEnum: A)
}
----------------
{
"$default": {}
}
"
`;

exports[`( variables: false; scalars: (none) ) - Query - args - input object enum > args - input object enum 1`] = `
"
--------------GRAFFLE QUERY-------------
{
"stringWithArgInputObjectEnum": {
"$": {
"input": {
"$abcEnum": "A"
}
}
}
}
--------GRAPHQL DOCUMENT & VARIABLES--------
{
stringWithArgInputObjectEnum(input: {abcEnum: "A"})
}
----------------
{
"$default": {}
}
"
`;

exports[`( variables: false; scalars: (none) ) - Query - args - object with args (empty object) > args - object with args (empty object) 1`] = `
"
Expand Down Expand Up @@ -2146,6 +2192,58 @@ query ($input: InputObjectNested) {
"
`;

exports[`( variables: true; scalars: (none) ) - Query - args - enum > args - enum 1`] = `
"
--------------GRAFFLE QUERY-------------
{
"stringWithArgEnum": {
"$": {
"$ABCEnum": "A"
}
}
}
--------GRAPHQL DOCUMENT & VARIABLES--------
query ($ABCEnum: ABCEnum) {
stringWithArgEnum(ABCEnum: $ABCEnum)
}
----------------
{
"$default": {
"ABCEnum": "A"
}
}
"
`;

exports[`( variables: true; scalars: (none) ) - Query - args - input object enum > args - input object enum 1`] = `
"
--------------GRAFFLE QUERY-------------
{
"stringWithArgInputObjectEnum": {
"$": {
"input": {
"$abcEnum": "A"
}
}
}
}
--------GRAPHQL DOCUMENT & VARIABLES--------
query ($input: InputObjectEnum!) {
stringWithArgInputObjectEnum(input: $input)
}
----------------
{
"$default": {
"input": {
"abcEnum": "A"
}
}
}
"
`;

exports[`( variables: true; scalars: (none) ) - Query - args - object with args (empty object) > args - object with args (empty object) 1`] = `
"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const toGraphQLDocument = (
definitions: operationsAndVariables.map(_ => _.operation),
})

const operationsVariables = Object.fromEntries(operationsAndVariables.map((_): [string, Grafaid.Variables] => {
const operationsVariables = Object.fromEntries(operationsAndVariables.map((_) => {
const name = _.operation.name?.value ?? defaultOperationName
return [name, _.variables]
}))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,5 @@ export const toGraphQLOperationDefinition: GraphQLPreOperationMapper<
variables,
}
}

// TODO: optimization: skip when using SDDM b/c we don't need $-directed enums
2 changes: 1 addition & 1 deletion src/documentBuilder/SelectGraphQLMapper/nodes/5_Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const toGraphQLField: GraphQLPostOperationMapper<
case `Arguments`: {
const sddmArguments = sddm?.a
for (const argName in keyParsed.arguments) {
const argNameSchema = argName.replace(/^\$/, ``)
const argNameSchema = argName
const sddmArgument = sddmArguments?.[argNameSchema]
const argValue = keyParsed.arguments[argName]

Expand Down
3 changes: 3 additions & 0 deletions src/documentBuilder/SelectGraphQLMapper/toGraphQL.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const tester = <$Scalars extends Schema.Scalar.ScalarMap>(
// todo test a case where we provide an operation name
// dprint-ignore
const cases = testEachQueryWithDescription([
// inline fragment
[`fg - one` , { ___: { __typename: true } }],
[`fg - multiple` , { ___: [{ __typename: true }, { abcEnum: true }] }],
[`fg - interface` , { interface: { ___: { __typename: true } } }],
Expand All @@ -81,6 +82,8 @@ const cases = testEachQueryWithDescription([
[`alias - scalar directive` , { id: [`x`, { $skip: true }] }],
[`alias - scalar directive+select` , { object: [`x`, { $skip: true, id: true }] }],
// arguments
[`args - enum` , { stringWithArgEnum: { $: { $ABCEnum: `A` }}}],
[`args - input object enum` , { stringWithArgInputObjectEnum: { $: { input: { $abcEnum: `A` }}}}],
[`args - on union` , { result: { $: { $case: `Object1` }, __typename: true } }],
[`args - string with args` , { stringWithArgs: { $: { boolean: true, float: 1 } } }],
[`args - alias` , { stringWithArgs: [[`a`, { $: { id: `` }}]] }],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,26 @@ export interface QueryMethods<$Context extends $$Utilities.Context> {
>
>

stringWithArgInputObjectEnum: $$Utilities.ClientTransports.PreflightCheck<
$Context,
<$SelectionSet>(
selectionSet: $$Utilities.Exact<
$SelectionSet,
$$SelectionSets.Query.stringWithArgInputObjectEnum<$Context['scalars']>
>,
) => Promise<
& (null | {})
& $$Utilities.HandleOutputGraffleRootField<
$Context,
InferResult.OperationQuery<
{ stringWithArgInputObjectEnum: $SelectionSet },
$$Schema.Schema<$Context['scalars']>
>,
'stringWithArgInputObjectEnum'
>
>
>

stringWithArgInputObjectRequired: $$Utilities.ClientTransports.PreflightCheck<
$Context,
<$SelectionSet>(
Expand Down
Loading

0 comments on commit dee9df7

Please sign in to comment.