gpt4 book ai didi

javascript - 缩小 graphql 查询是否会遇到问题

转载 作者:行者123 更新时间:2023-11-30 14:04:45 25 4
gpt4 key购买 nike

我有一个 graphql 查询,我正在使用 fetch 发送到服务器(使用 apollo-server)。在发送之前,我从查询字符串中删除了所有额外的空格。我应该担心吗?

const query = `
{
thing {
id
name
relatedThing {
id
name
createdAt
}
}
}`

query.replace(/\s+/g, ' ') 之后我有...

“{ thing { id name relatedThing { id name createdAt } } }”

我没有收到来自服务器的任何投诉或任何奇怪的行为,但我不了解服务器的需求以及这是否有可能破坏某些查询。我是否可以通过这样做来打破一些查询?

最佳答案

来自规范:

White space is used to improve legibility of source text and act as separation between tokens, and any amount of white space may appear before or after any token. White space between tokens is not significant to the semantic meaning of a GraphQL Document, however white space characters may appear within a String or Comment token... Like white space, line terminators are used to improve the legibility of source text, any amount may appear before or after any other token and have no significance to the semantic meaning of a GraphQL Document. Line terminators are not found within any other token.

换句话说,您所做的没有任何问题。只需记住两件事:

  • 如果您的查询包含字符串字面值,那么特定的正则表达式也会更改字符串字面量的值(如果它包含一个以上的空格)。
  • GraphQL 返回错误,其位置包括发生错误的行号和字符号。通过像这样转换您的查询,该信息将反射(reflect)转换后的查询,而不是您的原始查询。

关于javascript - 缩小 graphql 查询是否会遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55646527/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com