首页 > 解决方案 > 无法将枚举值传递给突变

问题描述

我有以下架构定义:

enum CommunityType {
  INTEREST
  COMPANY
  INDUSTRY
}
...
extend type Mutation {
  createCommunity(name: String!, type: CommunityType): Community
}

以及以下突变:

mutation CreateCommunity($name: String!, $type: CommunityType) {
  createCommunity(name: $name, type: $type) {
    id
    name
    type
  }
}

Apollo 也为我生成了正确的 TypeScript 类型:

export interface CreateCommunity_createCommunity {
  __typename: "Community";
  id: string | null;
  name: string | null;
  type: CommunityType | null;
}

export interface CreateCommunity {
  createCommunity: CreateCommunity_createCommunity | null;
}

export interface CreateCommunityVariables {
  name: string;
  type?: CommunityType | null;
}

和全局类型:

/**
 * CommunityType
 */
export enum CommunityType {
  COMPANY = "COMPANY",
  INDUSTRY = "INDUSTRY",
  INTEREST = "INTEREST",
  LOCATION = "LOCATION",
  MINISTRY = "MINISTRY",
}

当我像这样使用突变时

 const [create, {
    loading,
    error,
  }] = useMutation<CreateCommunity, CreateCommunityVariables>(CREATE_COMMUNITY, {
    variables: {
      name: title,
      type: CommunityType.COMPANY,
    },
  });

调用时出现此错误create

GraphQL 错误:应为“CommunityType”类型的值,但收到:“company”

似乎 Apollo Client 以某种方式将枚举值转换COMPANY为小写。

我正在使用 apollo-client 2.6.8。编辑:我升级到 apollo-client 3.3.6,同样的问题

标签: typescriptenumsapolloapollo-client

解决方案


推荐阅读