下面是我正在使用的当前graphQL的一个模拟。
const aDynamicListOfFieldsComingFromElsewhere = 'foo bar anotherField etc'
const query = gql`{
QueryResult: TableName {
Data {
id
name
${aDynamicListOfFieldsComingFromElsewhere}
}
}
}`这..。从功能上讲,是有效的。但是由于多种原因被认为是一种糟糕的方法,其中之一是由提供的衬里支持。
Eslint给了我一个提示,给了我以下错误:
Invalid interpolation - fragment interpolation must occur outside of the brackets graphql/template-strings我设法用变量找到了一些很好的例子,但是没有一个提供了包含外部定义变量的方法。
提前感谢您的帮助!
发布于 2020-03-13 20:33:44
您可以对此使用指令。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
}https://stackoverflow.com/questions/60419837
复制相似问题