Skip to content
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

Calling "t.modify" makes modified field required even if has a custom resolver #1104

Open
VictorGlindasPaf opened this issue Jun 22, 2022 · 1 comment

Comments

@VictorGlindasPaf
Copy link

VictorGlindasPaf commented Jun 22, 2022

Modifying a field on a type surprisingly modifies the generated "source type", on the object, even if the signature is the same. This causes typing errors in the parent field resolver.

import { interfaceType, objectType } from 'nexus';

export const Node = interfaceType({
  name: 'Node',
  definition(t) {
    t.nonNull.id('id', {
      resolve() {
        return '<example>';
      },
    });
  },
});

export const ChildObjectType = objectType({
  name: 'ChildObjectType',
  definition(t) {
    t.implements(Node);
    // Without this, there are no typing errors,
    // and the parent only needs to resolve "firstName" & "lastName".
    // But when added, it complains about the parent not passing "id" as well.
    t.modify('id', {
      resolve() {
        return '<modified-example>';
      },
    });
    t.nonNull.string('firstName');
    t.nonNull.string('lastName');
    t.nonNull.string('fullName', {
      resolve(source) {
        return `${source.firstName} ${source.lastName}`;
      },
    });
  },
});

export const ParentObjectType = objectType({
  name: 'ParentObjectType',
  definition(t) {
    t.field('child', {
      type: ChildObjectType,
      resolve() { // <-- This is where it throws a type error
        return {
          firstName: 'child',
          lastName: 'child',
        };
      },
    });
  },
});

Generated typescript output:

// Without modifying "id"
export interface NexusGenObjects {
  ChildObjectType: {
    firstName: string;
    lastName: string;
  }
}

// After modifying "id"
export interface NexusGenObjects {
  ChildObjectType: {
    id: string;
    firstName: string;
    lastName: string;
  }
}
@larrifax
Copy link

larrifax commented Oct 5, 2022

I'm experiencing the same thing.
Found out that re-declaring the field on the subtype instead of using t.modify acts as a workaround.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants