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

查看:40
本文介绍了在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.

但是,一旦使用 query (或 mutation )关键字,就需要提供一个名称.从理论上讲,即使没有名称,服务器也可能能够处理请求,但这不是当前GraphQL规范所允许的.

Once you use the query (or mutation) keyword though, you need to provide a name. The server might theoretically be able to process the request even without a name but that's not what the current GraphQL specification allows.

编辑

如下面的@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天全站免登陆