typescriptreact-apollographql-codegen

How to reflect field policy's read function return types in graphql-codegen?


Assume this type policy:

// TLDR enum field of User object is replaced with some class instance on read
const userTypePolicies = {
  User: {
    fields: {
      status: {
        read: (value: STATUS_ENUM) => new UserStatus(value),
      },
    },
  },
};

How to reflect this behavior in graphql-codegen output? According to my generated types, User.status is of type STATUS_ENUM, but it should be of type UserStatus (cache is transforming enum to class on every read).


Solution

  • GraphQL Code Generator does not provide a plugin for Apollo Client Type Policies.

    However, you could provide a client-side schema definition that extends the User type, using the schema configuration property: https://www.the-guild.dev/graphql/codegen/docs/config-reference/schema-field#multiple-schemas-and-client-side-schema