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

[typescript-resolvers] Fix interfaces not handling nested correctly #9962

Merged
merged 5 commits into from May 15, 2024

Conversation

eddeee888
Copy link
Collaborator

@eddeee888 eddeee888 commented May 8, 2024

Description

This PR fixes an issue where self-referencing and nested interfaces are not using resolver types correctly.

Interface is an abstract type like union. So, we need to reference the ResolversInterfaceTypes whenever there's a object type with a field with type interface or an array of interfaces.

Otherwise, the wrapping object type would just refer to the base generated type, causing mappers for the interface to be ignored.

Related eddeee888/graphql-code-generator-plugins#266

Note that this bug has been here for awhile. I can observe that in this PR to rebase tests. So at least it's been around before ResolversInterfaceTypes implementation

Example

Given this schema:

type Query {
  node: Node
  nodeSinglePayload: NodeSinglePayload!
}

interface Node {
  id: ID!
}

type User implements Node {
  id: ID!
}

type Post implements Node {
  id: ID!
}

type NodeSinglePayload {
  node: Node
}

And this mapper:

// schema.mappers.ts
export type UserMapper = { _id: string };
export type PostMapper = { _id: string };

Before

// types.generated.ts
export type NodeSinglePayload = {
  __typename?: 'NodeSinglePayload';
  node?: Maybe<Node>;
};

export type ResolversInterfaceTypes<RefType extends Record<string, unknown>> = {
  Node: ( PostMapper ) | ( UserMapper );
};

/** Mapping between all available schema types and the resolvers types */
export type ResolversTypes = {
  // ... other fields
  // ✅ Correctly referring `ResolversInterfaceTypes` which points to `PostMapper` and `UserMapper`
  Node: ResolverTypeWrapper<ResolversInterfaceTypes<ResolversTypes>['Node']>;
  // ❌ Referring `NodeSinglePayload` and its node isn't pointing to `PostMapper` or `UserMapper`
  NodeSinglePayload: ResolverTypeWrapper<NodeSinglePayload>;
}

After

// types.generated.ts
export type NodeSinglePayload = {
  __typename?: 'NodeSinglePayload';
  node?: Maybe<Node>;
};

export type ResolversInterfaceTypes<RefType extends Record<string, unknown>> = {
  Node: ( PostMapper ) | ( UserMapper );
};

export type ResolversTypes = {
  // ... other fields
  Node: ResolverTypeWrapper<ResolversInterfaceTypes<ResolversTypes>['Node']>;
  // ✅ The `node` field correctly points to `ResolversTypes['Node']` which in turn points to `PostMapper` and `UserMapper`
  NodeSinglePayload: ResolverTypeWrapper<Omit<NodeSinglePayload, 'node'> & { node?: Maybe<ResolversTypes['Node']> }>;
};

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • Unit test
  • Integration test with Server Preset

Copy link

changeset-bot bot commented May 8, 2024

🦋 Changeset detected

Latest commit: 4ea1da4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 9 packages
Name Type
@graphql-codegen/visitor-plugin-common Patch
@graphql-codegen/typescript-resolvers Patch
@graphql-codegen/typescript-document-nodes Patch
@graphql-codegen/gql-tag-operations Patch
@graphql-codegen/typescript-operations Patch
@graphql-codegen/typed-document-node Patch
@graphql-codegen/typescript Patch
@graphql-codegen/graphql-modules-preset Patch
@graphql-codegen/client-preset Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@eddeee888 eddeee888 marked this pull request as draft May 8, 2024 10:40
Copy link
Contributor

github-actions bot commented May 8, 2024

🚀 Snapshot Release (alpha)

The latest changes of this PR are available as alpha on npm (based on the declared changesets):

Package Version Info
@graphql-codegen/visitor-plugin-common 5.1.1-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/typescript-document-nodes 4.0.7-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/gql-tag-operations 4.0.7-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/typescript-operations 4.2.1-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/typescript-resolvers 4.0.7-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/typed-document-node 5.0.7-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/typescript 4.0.7-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/client-preset 4.2.6-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎
@graphql-codegen/graphql-modules-preset 4.0.7-alpha-20240509105210-4ea1da44bcbd1282abf3a9b6578d6c267619b98b npm ↗︎ unpkg ↗︎

Copy link
Contributor

github-actions bot commented May 8, 2024

💻 Website Preview

The latest changes are available as preview in: https://bb42a566.graphql-code-generator.pages.dev

Copy link

@joeyhotz joeyhotz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤩

@eddeee888 eddeee888 merged commit b49457b into master May 15, 2024
20 checks passed
@eddeee888 eddeee888 deleted the fix-interface-not-handled-nested-correctly branch May 15, 2024 09:24
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

Successfully merging this pull request may close these issues.

None yet

3 participants