nexus
nexus copied to clipboard
Typescript issues when reusing definitions in ObjectDefinition and InputDefinition
In 1.0.0
version, I was able to reuse definitions for both objectType
and inputObjectType
like so:
const experienceFieldsDefinition = (t: ObjectDefinitionBlock<any> | InputDefinitionBlock<any>) => {
t.nonNull.string("position");
// Next line produces compilation error in `1.1.0`
t.field("employmentType", {
type: EmploymentTypeEnumType
});
});
const ExperienceType = objectType({
name: "Experience",
definition(t) {
t.nonNull.objectId("_id");
experienceFieldsDefinition(t);
}
});
const ExperienceInput = inputObjectType({
name: "ExperienceInput",
definition(t) {
experienceFieldsDefinition(t);
}
});
After upgrading to 1.1.0
, t.field(...)
gives the following error:
This expression is not callable.
Each member of the union type '{ <FieldName extends string>(name: FieldName, config: FieldOutConfig<any, FieldName>): void; <FieldName extends string>(config: FieldOutConfigWi
thName<any, FieldName>): void; } | { ...; }' has signatures, but none of those signatures are compatible with each other.
@jasonkuhrt I assume this was related to the change made in #938 🤔
Yep probably, will try to check this out tomorrow or Friday.
I'm not sure why this worked before. I'm not sure if there is a TS limitation with regard to overloaded functions in unions or something we can fix here.
Error message in full:

Maybe a workaround for you right now might be to pick only one of the union members for your function.
@jasonkuhrt I am revisiting this issue again.
Picking one union member does not work for me because the whole point of having definitions like this is to reuse them. There is no point writing them twice and always keeping them in sync - once when defining objectType
and once when defining inputType
.
Since version 1.1
does not allow t.field
in both definitions anymore, is there a different way I can reuse Enums
(since I only currently need t.field
to reuse enums)?
I'm curious if this is the best issue or if there's another related GH issue re: objectType
and inputObjectType
re-use?
Any workaround or update on this and seeing this myself still in the latest version ?
Again was there any workaround on this as this simplifies definitions that are reused across both Input and Object Types - currently you can reuse primitive types but not field types