Skip to content

Commit

Permalink
fix(ts-client): no infer runtime objects
Browse files Browse the repository at this point in the history
This is because of TypeScript's inability to infer from circular types.
  • Loading branch information
jasonkuhrt committed Apr 14, 2024
1 parent 1238a1c commit 118d1d7
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/generator/__snapshots__/files.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -291,39 +291,57 @@ export const InputObject = $.InputObject(\`InputObject\`, {
dateRequired: $.Input.field($Scalar.Date),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const DateObject1 = $.Object$(\`DateObject1\`, {
date1: $.field($.Output.Nullable($Scalar.Date)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const DateObject2 = $.Object$(\`DateObject2\`, {
date2: $.field($.Output.Nullable($Scalar.Date)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const ObjectUnion = $.Object$(\`ObjectUnion\`, {
fooBarUnion: $.field($.Output.Nullable(() => FooBarUnion)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Foo = $.Object$(\`Foo\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Bar = $.Object$(\`Bar\`, {
int: $.field($.Output.Nullable($Scalar.Int)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const ObjectNested = $.Object$(\`ObjectNested\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
object: $.field($.Output.Nullable(() => Object1)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const lowerCaseObject = $.Object$(\`lowerCaseObject\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const lowerCaseObject2 = $.Object$(\`lowerCaseObject2\`, {
int: $.field($.Output.Nullable($Scalar.Int)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Object1 = $.Object$(\`Object1\`, {
string: $.field($.Output.Nullable($Scalar.String)),
int: $.field($.Output.Nullable($Scalar.Int)),
Expand All @@ -332,11 +350,15 @@ export const Object1 = $.Object$(\`Object1\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Object1ImplementingInterface = $.Object$(\`Object1ImplementingInterface\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
int: $.field($.Output.Nullable($Scalar.Int)),
})
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Object2ImplementingInterface = $.Object$(\`Object2ImplementingInterface\`, {
id: $.field($.Output.Nullable($Scalar.ID)),
boolean: $.field($.Output.Nullable($Scalar.Boolean)),
Expand All @@ -356,6 +378,8 @@ export const Interface = $.Interface(\`Interface\`, { id: $.field($.Output.Nulla
Object2ImplementingInterface,
])
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Query = $.Object$(\`Query\`, {
date: $.field($.Output.Nullable($Scalar.Date)),
dateNonNull: $.field($Scalar.Date),
Expand Down
2 changes: 2 additions & 0 deletions src/generator/code/schemaRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ const object = (config: Config, type: GraphQLObjectType) => {
return `${field.name}: ${outputField(config, field)}`
}).join(`,\n`)
return `
// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const ${type.name} = $.Object$(\`${type.name}\`, {
${fields}
})
Expand Down
24 changes: 24 additions & 0 deletions tests/ts/_/schema/generated/SchemaRuntime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,39 +10,57 @@ export const InputObject = $.InputObject(`InputObject`, {
dateRequired: $.Input.field($Scalar.Date),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const DateObject1 = $.Object$(`DateObject1`, {
date1: $.field($.Output.Nullable($Scalar.Date)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const DateObject2 = $.Object$(`DateObject2`, {
date2: $.field($.Output.Nullable($Scalar.Date)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const ObjectUnion = $.Object$(`ObjectUnion`, {
fooBarUnion: $.field($.Output.Nullable(() => FooBarUnion)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Foo = $.Object$(`Foo`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Bar = $.Object$(`Bar`, {
int: $.field($.Output.Nullable($Scalar.Int)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const ObjectNested = $.Object$(`ObjectNested`, {
id: $.field($.Output.Nullable($Scalar.ID)),
object: $.field($.Output.Nullable(() => Object1)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const lowerCaseObject = $.Object$(`lowerCaseObject`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const lowerCaseObject2 = $.Object$(`lowerCaseObject2`, {
int: $.field($.Output.Nullable($Scalar.Int)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Object1 = $.Object$(`Object1`, {
string: $.field($.Output.Nullable($Scalar.String)),
int: $.field($.Output.Nullable($Scalar.Int)),
Expand All @@ -51,11 +69,15 @@ export const Object1 = $.Object$(`Object1`, {
id: $.field($.Output.Nullable($Scalar.ID)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Object1ImplementingInterface = $.Object$(`Object1ImplementingInterface`, {
id: $.field($.Output.Nullable($Scalar.ID)),
int: $.field($.Output.Nullable($Scalar.Int)),
})

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Object2ImplementingInterface = $.Object$(`Object2ImplementingInterface`, {
id: $.field($.Output.Nullable($Scalar.ID)),
boolean: $.field($.Output.Nullable($Scalar.Boolean)),
Expand All @@ -75,6 +97,8 @@ export const Interface = $.Interface(`Interface`, { id: $.field($.Output.Nullabl
Object2ImplementingInterface,
])

// eslint-disable-next-line
// @ts-ignore - circular types cannot infer. Ignore in case there are any. This comment is always added, it does not indicate if this particular type could infer or not.
export const Query = $.Object$(`Query`, {
date: $.field($.Output.Nullable($Scalar.Date)),
dateNonNull: $.field($Scalar.Date),
Expand Down

0 comments on commit 118d1d7

Please sign in to comment.