GraphQL:从查询字符串中删除字段

发布于 2025-02-01 19:19:21 字数 576 浏览 3 评论 0原文

我有许多客户消耗的GraphQL Server,并具有从内省的过程中生成操作的过程;问题是该模式有一些嵌套的查询,这些查询会吸取我可能想或可能不想调用的论点。

鉴于此生成的操作,

query Query($input: String!) {
  things(input: $input) {
     item1
     item2
     nestedQuery(nestedInput: $nestedInput) {
        nestedItem1
     }
  }   
}

有一个工具(字符串解析,这是给定的)从这里剥离不需要的字段,以便将查询转换为

query Query($input: String!) {
  things(input: $input) {
     item1
     item2
  }   
}

内省JSON,我可以通过存在args <的存在来识别此项目。 /code>在字段上事物超过第一个深度 - 但是我不想丢失有关生成的信息 - 我希望这些嵌套的查询可见/记录。

I have a graphQL server consumed by many clients and have a process that generates operations from introspection; problem is that the schema has a handful of nested queries that take arguments that I may or may not want to invoke.

given this generated operation

query Query($input: String!) {
  things(input: $input) {
     item1
     item2
     nestedQuery(nestedInput: $nestedInput) {
        nestedItem1
     }
  }   
}

is there a tool (other than string parsing, that's a given) to strip unwanted fields from here so that the query is transformed into

query Query($input: String!) {
  things(input: $input) {
     item1
     item2
  }   
}

Looking at the introspection JSON I can identify this item by the presence of args on the field things that is past the first depth - but I don't want to necessarily lose information on generation - I'd like those nested queries to be visible/documented.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自在安然 2025-02-08 19:19:21

我仍然不明白您在此处如何生成GraphQl文档,但是假设您对其有完全控制,则可以使用默认指令(@skip and @include)跳过或根据条件包含字段。

例如,下面的文档将在执行时跳过nestedquery字段。如果,您可以使用变量值作为参数的值。

query Query($input: String!) {
  things(input: $input) {
     item1
     item2
     nestedQuery(nestedInput: $nestedInput) @skip(if: true) {
        nestedItem1
     }
  }   
}

I still don't understand how you are generating the GraphQL document here, but assuming that you have total control over it, you can use the default directives (@skip and @include) to skip or include fields based on a condition.

For example, the following document will skip the nestedQuery field when executing. You may use a variable value as the value for the argument if.

query Query($input: String!) {
  things(input: $input) {
     item1
     item2
     nestedQuery(nestedInput: $nestedInput) @skip(if: true) {
        nestedItem1
     }
  }   
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文