Skip to content

Commit

Permalink
fix(document-builder): enum variables key names
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonkuhrt committed Dec 25, 2024
1 parent 0e6618f commit 7816139
Show file tree
Hide file tree
Showing 21 changed files with 424 additions and 27 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
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
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ const InputObject: $$Utilities.SchemaDrivenDataMap.InputObject = {
n: 'InputObject',
fcs: ['date', 'dateRequired'],
f: {
abcEnum: {},
date: {
nt: Date,
},
Expand All @@ -134,6 +135,13 @@ const InputObjectCircular: $$Utilities.SchemaDrivenDataMap.InputObject = {
},
}

const InputObjectEnum: $$Utilities.SchemaDrivenDataMap.InputObject = {
n: 'InputObjectEnum',
f: {
abcEnum: {},
},
}

const InputObjectNested: $$Utilities.SchemaDrivenDataMap.InputObject = {
n: 'InputObjectNested',
fcs: ['InputObject'],
Expand Down Expand Up @@ -648,6 +656,14 @@ const Query: $$Utilities.SchemaDrivenDataMap.OutputObject = {
},
},
},
stringWithArgInputObjectEnum: {
a: {
input: {
nt: InputObjectEnum,
it: [1],
},
},
},
stringWithArgInputObjectRequired: {
a: {
input: {
Expand Down Expand Up @@ -819,6 +835,7 @@ const $schemaDrivenDataMap: $$Utilities.SchemaDrivenDataMap = {
ParentInterfaceHierarchyMember,
InputObject,
InputObjectCircular,
InputObjectEnum,
InputObjectNested,
InputObjectNestedNonNull,
Bar,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ export namespace Schema {
string: Query.$string
stringWithArgEnum: Query.stringWithArgEnum
stringWithArgInputObject: Query.stringWithArgInputObject
stringWithArgInputObjectEnum: Query.stringWithArgInputObjectEnum
stringWithArgInputObjectRequired: Query.stringWithArgInputObjectRequired
stringWithArgs: Query.stringWithArgs
stringWithListArg: Query.stringWithListArg
Expand Down Expand Up @@ -571,6 +572,20 @@ export namespace Schema {
namedType: $$NamedTypes.$$String
}

export interface stringWithArgInputObjectEnum extends $.OutputField {
name: 'stringWithArgInputObjectEnum'
arguments: {
input: {
kind: 'InputField'
name: 'input'
inlineType: [1]
namedType: $$NamedTypes.$$InputObjectEnum
}
}
inlineType: [0]
namedType: $$NamedTypes.$$String
}

export interface stringWithArgInputObjectRequired extends $.OutputField {
name: 'stringWithArgInputObjectRequired'
arguments: {
Expand Down Expand Up @@ -1483,6 +1498,7 @@ export namespace Schema {
name: 'InputObject'
isAllFieldsNullable: true
fields: {
abcEnum: InputObject.abcEnum
date: InputObject.date
dateRequired: InputObject.dateRequired
id: InputObject.id
Expand All @@ -1491,6 +1507,12 @@ export namespace Schema {
}

export namespace InputObject {
export interface abcEnum extends $.InputField {
name: 'abcEnum'
inlineType: [0]
namedType: $$NamedTypes.$$ABCEnum
}

export interface date extends $.InputField {
name: 'date'
inlineType: [0]
Expand Down Expand Up @@ -1543,6 +1565,26 @@ export namespace Schema {
}
}

// InputObjectEnum
// --------------------------------------------------------------------------------------------------
//

export interface InputObjectEnum extends $.InputObject {
name: 'InputObjectEnum'
isAllFieldsNullable: true
fields: {
abcEnum: InputObjectEnum.abcEnum
}
}

export namespace InputObjectEnum {
export interface abcEnum extends $.InputField {
name: 'abcEnum'
inlineType: [0]
namedType: $$NamedTypes.$$ABCEnum
}
}

// InputObjectNested
// --------------------------------------------------------------------------------------------------
//
Expand Down Expand Up @@ -2216,6 +2258,7 @@ export namespace Schema {
export type $$lowerCaseObject2 = lowerCaseObject2
export type $$InputObject = InputObject
export type $$InputObjectCircular = InputObjectCircular
export type $$InputObjectEnum = InputObjectEnum
export type $$InputObjectNested = InputObjectNested
export type $$InputObjectNestedNonNull = InputObjectNestedNonNull
export type $$DateInterface1 = DateInterface1
Expand Down
Loading

0 comments on commit 7816139

Please sign in to comment.