【问题标题】:How to move fields list fragment interpolation outside of the brackets如何将字段列表片段插值移动到括号之外
【发布时间】:2020-06-10 16:12:57
【问题描述】:

这是我正在使用的当前 graphQL 的模拟。

const aDynamicListOfFieldsComingFromElsewhere = 'foo bar anotherField etc'

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

这...从功能上讲,有效。但是由于多种原因被认为是一种不好的方法,其中一个是 eslint-plugin-graphql 提供的 linting 支持。

Eslint 给了我以下错误提示:

Invalid interpolation - fragment interpolation must occur outside of the brackets  graphql/template-strings

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

提前感谢您的帮助!

【问题讨论】:

    标签: graphql eslint apollo-client graphql-js


    【解决方案1】:

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

    查询应如下所示:

    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 客户端的变量:

    {
       withFoo: true, 
       withBar: true, 
       withAnotherField: false,
       withEtc: true
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多