查询类型(query / mutation)GraphQL 后紧跟字符串的意义
The significance of the string immediately after query type (query / mutation) GraphQL
我想知道查询类型后面的字符串有什么意义,在本例中 "ProvisionQueues",似乎从字符串中删除它不会影响任何东西 - 它只是用于日志记录还是其他什么。元数据?
mutation ProvisionQueues {
createQueue(name: "new-queue") {
url
}
}
该字符串是操作名称。如果您不指定名称,则该操作称为 匿名操作。不过,在实用的情况下,我总是喜欢指定一个操作名称,因为这样可以更轻松地执行读取堆栈跟踪等操作。
it seems removing this from the string doesn't affect anything
您只能在执行单个操作时使用匿名操作。例如,以下会导致错误:
query {
user(id: 1) {
name
}
}
query {
user(id: 2) {
name
}
}
错误:
"message": "This anonymous operation must be the only defined operation."
如果你想了解更多,可以查看the GraphQL spec:
If a document contains only one operation, that operation may be unnamed or represented in the shorthand form, which omits both the query keyword and operation name. Otherwise, if a GraphQL query document contains multiple operations, each operation must be named.
用另一个例子添加到@Eric 的回答中。
query allNotifications {
notifications {
success
errors
notifications {
id
title
description
attachment
createdAt
}
}
}
query {
users {
errors
success
users {
id
fullName
}
}
}
注意上面的用户查询没有操作名称。这可以通过以下方式解决。
query allUsers {
users {
errors
success
users {
id
fullName
mohalla
}
}
}
我想知道查询类型后面的字符串有什么意义,在本例中 "ProvisionQueues",似乎从字符串中删除它不会影响任何东西 - 它只是用于日志记录还是其他什么。元数据?
mutation ProvisionQueues {
createQueue(name: "new-queue") {
url
}
}
该字符串是操作名称。如果您不指定名称,则该操作称为 匿名操作。不过,在实用的情况下,我总是喜欢指定一个操作名称,因为这样可以更轻松地执行读取堆栈跟踪等操作。
it seems removing this from the string doesn't affect anything
您只能在执行单个操作时使用匿名操作。例如,以下会导致错误:
query {
user(id: 1) {
name
}
}
query {
user(id: 2) {
name
}
}
错误:
"message": "This anonymous operation must be the only defined operation."
如果你想了解更多,可以查看the GraphQL spec:
If a document contains only one operation, that operation may be unnamed or represented in the shorthand form, which omits both the query keyword and operation name. Otherwise, if a GraphQL query document contains multiple operations, each operation must be named.
用另一个例子添加到@Eric 的回答中。
query allNotifications {
notifications {
success
errors
notifications {
id
title
description
attachment
createdAt
}
}
}
query {
users {
errors
success
users {
id
fullName
}
}
}
注意上面的用户查询没有操作名称。这可以通过以下方式解决。
query allUsers {
users {
errors
success
users {
id
fullName
mohalla
}
}
}