【问题标题】:Grouping graphql mutations对graphql突变进行分组
【发布时间】:2018-10-24 04:47:48
【问题描述】:

我正在尝试将我的突变分组为二级类型。架构已正确解析,但解析器未在 Apollo 中触发。这甚至可能吗?这是我想要的查询:

mutation {
    pets: {
        echo (txt:"test") 
    }
}

这就是我的尝试

架构:

type PetsMutations {
    echo(txt: String): String
}

type Mutation {
    "Mutations related to pets"
    pets: PetsMutations
}

schema {
    mutation: Mutation
}

解析器:

  ...
  return {
    Mutation: {
      pets : {
        echo(root, args, context) {
            return args.txt;
        }
      },
    }

【问题讨论】:

    标签: graphql graphql-js


    【解决方案1】:

    假设您使用apollo-servergraphql-tools,您不能像这样在解析器映射中嵌套解析器。解析器映射中的每个属性都应该对应于架构中的一个类型,并且本身就是字段名称到解析器函数的映射。试试这样的:

    {
      Mutation: {
        // must return an object, if you return null the other resolvers won't fire
        pets: () => ({}),
      },
      PetsMutations: {
        echo: (obj, args, ctx) => args.txt,
      },
    }
    

    旁注,您的查询无效。由于echo 字段是一个标量,因此您不能为其选择字段。您需要删除空括号。

    【讨论】:

    • 谢谢!将问题中的查询更新为有效。
    猜你喜欢
    • 2021-12-02
    • 1970-01-01
    • 2018-04-02
    • 2018-08-03
    • 2016-05-30
    • 2020-08-15
    • 2020-05-30
    • 2021-02-17
    • 2018-09-03
    相关资源
    最近更新 更多