首页 > 解决方案 > 如何在解析器中使用 Apollo-Server Graphql 的枚举?

问题描述

环境

  1. 阿波罗服务器
  2. 表示
  3. 打字稿
  4. 打字机

类型定义 (typeDefs.ts)

import { gql } from 'apollo-server-express';

const typeDefs = gql`

  enum Part {
    Hand, Arm, Waist, Bottom
  }

  type PartInfo {
    team: Int,
    tag: String,
    part: Part
  }

  ...

  type Query {
    ...
    hand(team: Int): PartInfo,
    ...
  }

`;
export default typeDefs;

解析器 (resolvers.ts)

const resolvers = {
  Query: {
    ...
    hand: async (parent, args, context, info) => {
      const { team } = args;

      ...

      return {
        team,
        tag: "handTag",
        part: Part.hand
      }
    }
    ...
  },
};

export default resolvers;

问题

我想使用 enum 的typeDefs.ts一部分resolvers.ts

我试过了

return {
    team,
    tag: "handTag",
    part: "Hand"
}

也,但剂量工作。

如何使用typeDefs.tsat中定义的枚举类型resolvers.ts

请检查!

标签: typescriptgraphqlapollo

解决方案


除了 Schema (typeDef.ts),您还应该在解析器中定义您的枚举。

const resolvers = {
  Query: {
    ...
    hand: async (parent, args, context, info) => {
      const { team } = args;

      ...

      return {
        team,
        tag: "handTag",
        part: Part.hand
      }
    }
    ...
  },
  Part: {
    Hand: Part.hand
  }
};

export default resolvers;

推荐阅读