Skip to content

fix: enum arguments in nested input objects #1362

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

Merged
merged 12 commits into from
Jul 14, 2025
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 14 additions & 8 deletions src/extensions/DocumentBuilder/Select/$parseSelection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Grafaid } from '../../../lib/grafaid/_namespace.js'
import { Grafaid } from '#lib/grafaid/_namespace.js'
import { Select } from './../Select/__.js'
import type { SelectionSet } from './_.js'
import { Arguments, Directive, Indicator, InlineFragment, SelectAlias, SelectScalarsWildcard } from './_.js'
Expand Down Expand Up @@ -90,15 +90,21 @@ 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,
}
})
// Strip enum prefix ($) from argument keys only, preserving nested values as-is.
// This separation is crucial: argument keys need their $ prefix removed for GraphQL
// (e.g., $case -> case), but values within arguments must keep their $ prefixes
// so that enum fields in input objects can be detected later during value processing.
// Example: { $case: 'Foo', input: { $direction: 'ASC' } }
// - $case is stripped to 'case' (argument key)
// - $direction keeps its $ prefix (will be processed in Value.ts)
const argumentsNormalized: Record<string, any> = {}
for (const [argKey, argValue] of Object.entries(value)) {
const normalizedKey = Select.Arguments.enumKeyPrefixStrip(argKey)
argumentsNormalized[normalizedKey] = argValue
}
return {
type: `Arguments`,
arguments: argumentsNormalized ?? {},
arguments: argumentsNormalized,
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,32 @@ exports[`( variables: false; scalars: (none) ) - Query - args - input object enu
}
--------GRAPHQL DOCUMENT & VARIABLES--------
{
stringWithArgInputObjectEnum(input: {abcEnum: "A"})
stringWithArgInputObjectEnum(input: {abcEnum: A})
}
----------------
{
"$default": {}
}
"
`;

exports[`( variables: false; scalars: (none) ) - Query - args - nested input object enum multiple > args - nested input object enum multiple 1`] = `
"

--------------GRAFFLE QUERY-------------
{
"stringWithArgInputObjectEnum": {
"$": {
"input": {
"$abcEnum": "B",
"id": "test"
}
}
}
}
--------GRAPHQL DOCUMENT & VARIABLES--------
{
stringWithArgInputObjectEnum(input: {abcEnum: B, id: "test"})
}
----------------
{
Expand Down Expand Up @@ -2237,7 +2262,37 @@ query ($input: InputObjectEnum!) {
{
"$default": {
"input": {
"abcEnum": "A"
"$abcEnum": "A"
}
}
}
"
`;

exports[`( variables: true; scalars: (none) ) - Query - args - nested input object enum multiple > args - nested input object enum multiple 1`] = `
"

--------------GRAFFLE QUERY-------------
{
"stringWithArgInputObjectEnum": {
"$": {
"input": {
"$abcEnum": "B",
"id": "test"
}
}
}
}
--------GRAPHQL DOCUMENT & VARIABLES--------
query ($input: InputObjectEnum!) {
stringWithArgInputObjectEnum(input: $input)
}
----------------
{
"$default": {
"input": {
"$abcEnum": "B",
"id": "test"
}
}
}
Expand Down
19 changes: 17 additions & 2 deletions src/extensions/DocumentBuilder/SelectGraphQLMapper/nodes/Value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { Grafaid } from '../../../../lib/grafaid/_namespace.js'
import { Nodes } from '../../../../lib/grafaid/_Nodes.js'
import { Schema } from '../../../../types/Schema/_namespace.js'
import { SchemaDrivenDataMap } from '../../../../types/SchemaDrivenDataMap/_namespace.js'
import { Select } from '../../Select/__.js'
import type { OperationContext } from '../context.js'
import { type GraphQLPostOperationMapper } from '../mapper.js'

Expand Down Expand Up @@ -41,9 +42,23 @@ export const toGraphQLValue: ValueMapper = (context, sddm, value) => {
const sddmInputObject = sddm?.nt
return Nodes.ObjectValue({
fields: Object.entries(value).map(([fieldName, fieldValue]) => {
// When processing input object fields, check for the enum prefix ($) that was
// preserved by parseSelection. This $ prefix is our signal that this field's
// value should be rendered as a GraphQL enum value (unquoted) rather than a
// string literal (quoted). This is the second half of the enum handling logic:
// parseSelection preserves the prefix, and here we detect and act on it.
const isEnumField = Select.Arguments.isEnumKey(fieldName)
const actualFieldName = isEnumField ? Select.Arguments.enumKeyPrefixStrip(fieldName) : fieldName

// Get the field's schema info using the actual field name (without $)
const fieldSddm = sddmInputObject?.f?.[actualFieldName]

// Pass enum context down so string values are rendered as enum values
const fieldContext = isEnumField ? { ...context, value: { isEnum: true } } : context

return Nodes.ObjectField({
name: Nodes.Name({ value: fieldName }),
value: toGraphQLValue(context, sddmInputObject?.f?.[fieldName], fieldValue),
name: Nodes.Name({ value: actualFieldName }),
value: toGraphQLValue(fieldContext, fieldSddm, fieldValue),
})
}),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ const cases = testEachQueryWithDescription([
// arguments
[`args - enum` , { stringWithArgEnum: { $: { $ABCEnum: `A` }}}],
[`args - input object enum` , { stringWithArgInputObjectEnum: { $: { input: { $abcEnum: `A` }}}}],
[`args - nested input object enum multiple` , { stringWithArgInputObjectEnum: { $: { input: { $abcEnum: `B`, id: `test` }}}}],
[`args - on union` , { result: { $: { $case: `Object1` }, __typename: true } }],
[`args - string with args` , { stringWithArgs: { $: { boolean: true, float: 1 } } }],
[`args - alias` , { stringWithArgs: [[`a`, { $: { id: `` }}]] }],
Expand Down Expand Up @@ -131,7 +132,7 @@ const cases = testEachQueryWithDescription([
[`object scalar` , { object: { id: true } }],
[`object nested` , { objectNested: { object: { string: true, id: true, int: false } } }],
])
// cases(...tester({ variables: true, scalars: {} }))
cases(...tester({ variables: true, scalars: {} }))
cases(...tester({ variables: false, scalars: {} }))

// dprint-ignore
Expand Down
Loading