首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将字段列表片段内插移到括号外

如何将字段列表片段内插移到括号外
EN

Stack Overflow用户
提问于 2020-02-26 17:59:09
回答 1查看 436关注 0票数 2

下面是我正在使用的当前graphQL的一个模拟。

代码语言:javascript
复制
const aDynamicListOfFieldsComingFromElsewhere = 'foo bar anotherField etc'

const query = gql`{
  QueryResult: TableName {
    Data {
      id
      name
      ${aDynamicListOfFieldsComingFromElsewhere}
    }
  }
}`

这..。从功能上讲,是有效的。但是由于多种原因被认为是一种糟糕的方法,其中之一是由提供的衬里支持。

Eslint给了我一个提示,给了我以下错误:

代码语言:javascript
复制
Invalid interpolation - fragment interpolation must occur outside of the brackets  graphql/template-strings

我设法用变量找到了一些很好的例子,但是没有一个提供了包含外部定义变量的方法。

提前感谢您的帮助!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-03-13 20:33:44

您可以对此使用指令。GraphQL:https://graphql.org/learn/queries/#directives规范

查询应该如下所示:

代码语言:javascript
复制
const query = gql`
  query QueryResult($withFoo: Boolean!, $withBar: Boolean!, $withAnotherField: Boolean!, $withEtc: Boolean!) {
    Data {
      id
      name
      foo @include(if: $withFoo)
      bar @include(if: $withBar)
      anotherField @include(if: $withAnotherField)
      etc @include(if: $withEtc)
    }
  }
`

以及应该传递给GraphQL客户端的变量:

代码语言:javascript
复制
{
   withFoo: true, 
   withBar: true, 
   withAnotherField: false,
   withEtc: true
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60419837

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档