nexus icon indicating copy to clipboard operation
nexus copied to clipboard

Typescript issues when reusing definitions in ObjectDefinition and InputDefinition

Open hoangbn opened this issue 3 years ago • 7 comments

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.

hoangbn avatar Jul 05 '21 01:07 hoangbn

@jasonkuhrt I assume this was related to the change made in #938 🤔

tgriesser avatar Jul 06 '21 20:07 tgriesser

Yep probably, will try to check this out tomorrow or Friday.

jasonkuhrt avatar Jul 07 '21 04:07 jasonkuhrt

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:

CleanShot 2021-07-12 at 12 12 21@2x

Maybe a workaround for you right now might be to pick only one of the union members for your function.

jasonkuhrt avatar Jul 12 '21 16:07 jasonkuhrt

@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)?

hoangbn avatar Nov 20 '21 22:11 hoangbn

I'm curious if this is the best issue or if there's another related GH issue re: objectType and inputObjectType re-use?

visormatt avatar Jul 18 '22 22:07 visormatt

Any workaround or update on this and seeing this myself still in the latest version ?

MarkAPhillips avatar Sep 14 '22 13:09 MarkAPhillips

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

MarkAPhillips avatar Feb 14 '23 13:02 MarkAPhillips