graphql - Grouping graphql mutations
问题描述
I'm trying to group my mutations into second level types. The schema is parsed correctly, but resolvers aren't firing in Apollo. Is this even possible? Here's the query I want:
mutation {
pets: {
echo (txt:"test")
}
}
Here's how I'm trying to do it
Schema:
type PetsMutations {
echo(txt: String): String
}
type Mutation {
"Mutations related to pets"
pets: PetsMutations
}
schema {
mutation: Mutation
}
Resolvers:
...
return {
Mutation: {
pets : {
echo(root, args, context) {
return args.txt;
}
},
}
解决方案
Assuming you're using apollo-server
or graphql-tools
, you cannot nest resolvers in your resolver map like that. Each property in the resolver map should correspond to a type in your schema, and itself be a map of field names to resolver functions. Try something like this:
{
Mutation: {
// must return an object, if you return null the other resolvers won't fire
pets: () => ({}),
},
PetsMutations: {
echo: (obj, args, ctx) => args.txt,
},
}
Side note, your query isn't valid. Since the echo
field is a scalar, you can't have a subselection of fields for it. You need to remove the empty brackets.
推荐阅读
- c# - 如何在文本中写入数字(浮点数和整数)
- java - 覆盖中的 ActionEvent 找不到 JFrame
- javascript - 通过用户 id 获取 firestore 文档
- shopify - 如何在液体中为购买按钮输出 themekit 配置
- android - Android 11 上的 Android 通知 setFullScreenIntent 操作按钮不需要的边框
- python - 为什么我的 zip 返回的项目不能在 Python 中应用两次?
- r - 根据另一列的下一行的值创建具有值的列
- windows - 除了 AppData、文档之外,用户数据是否存储在其他任何地方
- websocket - 在 Micronaut 中使用 RxJava 进行 Websocket 请求路由
- android - 创建 MutableList 时这意味着什么?