【问题标题】:Error: BabelPluginRemoveGraphQL: String interpolations错误:BabelPluginRemoveGraphQL:字符串插值
【发布时间】:2018-11-17 03:48:47
【问题描述】:

尝试在 gatsBy Graphql 中动态传递变量时出现以下错误。

错误

Module build failed (from ./node_modules/gatsby/dist/utils/babel-loader.js):
Error: BabelPluginRemoveGraphQL: String interpolations are not allowed in graphql fragments. Included fragments should be referenced as `...MyModule_foo`.

查询

let mytext = 'welcome'
let myQuery = graphql`query($text: String = "${mytext}") {
            allGhostPost : allGhostPost(filter:{title:{eq: $text}}) {
              edges {
                node {
                  id
                  slug
                }
              }
            }
          }`

请帮忙!!!

【问题讨论】:

    标签: graphql gatsby


    【解决方案1】:

    像这样在查询中插入任意文本是well-known security issue,Babel 插件几乎可以肯定地禁止它。 GraphQL 定义了a JSON-over-HTTP payload format,允许单独传递变量(编码为 JSON 对象以最大程度地减少注入攻击的可能性)。

    您不会显示实际执行查询的内容,但它应该有一个地方可以添加 GraphQL 变量的映射。 (例如,graphql-js 参考实现包括一个variableValues 参数到its top-level graphql function。)删除查询的= "${mytext}" 部分,而是使用像{text: mytext} 这样的变量对象。

    【讨论】:

    • 谢谢,但有什么解决办法?我需要动态传递mytext
    • this 建议您不能将变量传递给字符串,因为查询是在编译时解析的。
    猜你喜欢
    • 2014-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 2017-06-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多