【问题标题】:GraphQLError: Syntax Error: Expected $, found [GraphQLError:语法错误:预期 $,找到 [
【发布时间】:2019-10-09 05:29:10
【问题描述】:

我正在尝试查询此处的 graphql 服务器

http://sapienscube.pythonanywhere.com/intrusion-detection-system

在 Reactjs 中使用 Apollo 进行此查询:

const PREDICT_ATTACK_TYPE = gql`
  query Query($packets: List[List[String]) {
    predict(packets: $packets)
  }
`;
// features is an array of arrays of strings: List[List[String]] like this: [[0, tcp, http, ..., ]]
<Query query={PREDICT_ATTACK_TYPE} variables={{ features }}>
        {({ loading, error, data }) => {
          if (loading) return <div>loading..</div>;
          if (error) return <div>Error:</div>;
          console.log(data);
          return <div>{data.predict}</div>;
        }}

但我得到了错误

GraphQLError:语法错误:预期 $,找到 [

   5 | 
   6 | import { getFeaturesArray } from "./Utils";
   7 | 
>  8 | const PREDICT_ATTACK_TYPE = gql`
   9 |   query Query($packets: List[List[String]]) {
  10 |     predict(packets: $packets)
  11 |   }

【问题讨论】:

    标签: reactjs syntax-error graphql react-apollo


    【解决方案1】:

    虽然List 在技术上是 GraphQL 中的一种类型,但不能在 GraphQL 文档中按名称引用它。指定特定类型的 List 时,只需将该类型括在方括号中 ([ ])。

    query Query($packets: [[String]]) {
      predict(packets: $packets)
    }
    

    这里的[[String]] 表示ListListStrings 的List

    您会看到上述错误,因为解析器将类型 List 注册为 $packets 的类型并移动到下一个有效字符,它应该是 $ 以指示下一个变量的开始定义。

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 2021-12-09
      • 2019-04-05
      • 2019-11-12
      • 2020-04-18
      • 2022-01-15
      • 2022-11-15
      • 2021-11-10
      • 2023-03-31
      相关资源
      最近更新 更多