【问题标题】:GraphQL Apollo Mutation String cannot represent value with passed in arrayGraphQL Apollo Mutation String 不能表示传入数组的值
【发布时间】:2021-09-20 03:26:23
【问题描述】:

我在 Apollo 设置中有一个解析器突变,代码如下

Mutation: {
    sendInvite: async (parent, args: MutationSendInviteArgs, context) => {
      console.log('args email >>>>> ', args);
      // create invite code
      // add to invite container
      // send email with invite
      const emailSent = true;
      const emailMessage = 'sent invite';
      // is the record added by the api connector? ---> look into this
      return {
        success: emailSent,
        message: emailMessage,
        email:args.email
      }
    }
  }

我在操场上运行以下突变

mutation{
  sendInvite(email:["anders@email.org","anders@email.com"]){
    email
    success
    message
  }
}

我得到以下响应

 "message": "String cannot represent value: [\"anders@email.org\", \"anders@email.com\"]",

哦,这是我的变异模式

type Mutation {
  sendInvite(email: [String]): Invite
}
type Invite {
  email: String!
  success: Boolean!
  message: String!
}

如何返回电子邮件,使其成为可以映射的数组?

其实我想返回 sendInvite 字段的数组,所以有多个成功消息、电子邮件和消息

提前感谢

【问题讨论】:

标签: javascript graphql apollo resolver


【解决方案1】:

必须做以下事情

type Invite {
  InviteArray: [InviteArray!]
}

type InviteArray {
  email: String!
  verified: Boolean!
  success: Boolean!
  message: String!
}
let items = args.email.map((item) =>{
        return {email: item}
      })
      console.log("test >>>> ", test)
      return {
        InviteArray:items
      }

【讨论】:

  • 类型应该命名为InviteItem,而不是InviteArray
猜你喜欢
  • 2019-06-12
  • 2020-12-01
  • 2018-07-15
  • 2019-06-11
  • 2019-02-25
  • 2021-05-30
  • 2020-12-23
  • 2020-01-15
  • 2021-02-20
相关资源
最近更新 更多