在 GraphQL 中命名查询和突变有什么意义? [英] What is the point of naming queries and mutations in GraphQL?

查看:14
本文介绍了在 GraphQL 中命名查询和突变有什么意义?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请原谅这个幼稚的问题,但我已经四处寻找答案,我发现的所有内容要么含糊不清,要么对我来说毫无意义.以 GraphQL 规范为例:

Pardon the naive question, but I've looked all over for the answer and all I've found is either vague or makes no sense to me. Take this example from the GraphQL spec:

query getZuckProfile($devicePicSize: Int) {
  user(id: 4) {
    id
    name
    profilePic(size: $devicePicSize)
  }
}

将此查询命名为 getZuckProfile 有什么意义?我见过一些关于包含多个操作的 GraphQL 文档.命名查询会以某种方式影响返回的数据吗?我会自己测试一下,但我没有可以轻松进行实验的服务器和数据集.但是,如果某个文档中的某些内容可以澄清这一点,那就太好了——到目前为止,所有示例都是超级简单的单个查询,或者是已命名但未解释其原因的查询(除了这是一个很酷的你可以做的事情.")命名查询有什么好处,当我每个请求发送一个匿名查询时我没有?

What is the point of naming this query getZuckProfile? I've seen something about GraphQL documents containing multiple operations. Does naming queries affect the returned data somehow? I'd test this out myself, but I don't have a server and dataset I can easily play with to experiment. But it would be good if something in some document somewhere could clarify this--thus far all of the examples are super simple single queries, or are queries that are named but that don't explain why they are (other than "here's a cool thing you can do.") What benefits do I get from naming queries that I don't have when I send a single, anonymous query per request?

此外,关于突变,我在规范中看到:

Also, regarding mutations, I see in the spec:

mutation setName {
  setName(name: "Zuck") {
    newName
  }
}

在这种情况下,您指定了 setName 两次.为什么?我知道其中之一是突变的字段名称,需要将其与后端架构匹配,但为什么不:

In this case, you're specifying setName twice. Why? I get that one of these is the field name of the mutation and is needed to match it to the back-end schema, but why not:

mutation {
  setName(name: "Zuck") {
...

两次指定相同的名称有什么好处?我知道第一个可能是任意的,但为什么它不是噪音?我必须遗漏一些明显的东西,但到目前为止我没有发现任何东西可以为我清除它.

What benefit do I get specifying the same name twice? I get that the first is likely arbitrary, but why isn't it noise? I have to be missing something obvious, but nothing I've found thus far has cleared it up for me.

推荐答案

查询名称在服务器上没有任何意义.它仅用于客户端识别响应(因为您可以在单个请求中发送多个查询/变更).

The query name doesn't have any meaning on the server whatsoever. It's only used for clients to identify the responses (since you can send multiple queries/mutations in a single request).

事实上,如果这是 GraphQL 请求中唯一的内容(并且没有任何参数),您可以只发送一个匿名查询对象:

In fact, you can send just an anonymous query object if that's the only thing in the GraphQL request (and doesn't have any parameters):

{
  user(id: 4) {
    id
    name
    profilePic(size: 200)
  }
}

这只适用于查询,不适用于突变.

This only works for a query, not mutation.


正如@orta 在下面指出的那样,服务器也可以使用该名称来标识持久查询.但是,这不是 GraphQL 规范的一部分,它只是顶部的自定义实现.


As @orta notes below, the name could also be used by the server to identify a persistent query. However, this is not part of the GraphQL spec, it's just a custom implementation on top.

这篇关于在 GraphQL 中命名查询和突变有什么意义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆