【问题标题】:Syntax Error: Expected Name, found String "" gatsby and graphql语法错误:预期名称,找到字符串“” gatsby 和 graphql
【发布时间】:2021-02-18 03:37:40
【问题描述】:

我在 gatsby 网站中的 graphql 查询中收到以下错误,我从 Strapi CMS 获取数据。

语法错误:预期名称,找到字符串“” gatsby

代码如下:

export const Query = graphql`
 {
   source {
     people(where: {isSupport: true}) {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

通过上述查询,我​​正在尝试获取属于支持团队的人员,但我收到此错误语法错误:预期名称,找到字符串“isSupport”

以上代码在 Graphql 浏览器上运行良好。然后我想因为查询在模板字符串中,所以我应该按如下方式更改我的代码。

export const Query = graphql`
 {
   source {
     people(where: {${isSupport}: true}) {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

使用上面的代码,我仍然无法得到想要的结果。

仔细查看 graphql 资源管理器,我注意到 where 过滤器采用 JSON 对象,因此我将代码转换如下:

export const Query = graphql`
 {
   source {
     people(where: {"isSupport": true}) {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

使用上面的代码,我仍然无法得到想要的结果。

【问题讨论】:

标签: graphql gatsby strapi


【解决方案1】:

您是否尝试过类似的方法:

export const Query = graphql`
 query ($isSupport: Boolean){
   source {
     people(where: {$isSupport: true}) {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

您没有在查询构造函数 ($isSupport: String) 中定义变量类型。

另外,我对where过滤器不熟悉。以下也应该有效:

export const Query = graphql`
 query ($isSupport: Boolean){
   source (people: {isSupport: {eq: $isSupport}}) {
     people {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

上面的 sn-ps 将过滤所有 sourcespeople.isSupport 属性等于通过上下文传递的值。

如果您没有从上下文中传递任何值,请直接检查该值:

export const Query = graphql`
 {
   source (people: {isSupport: {eq: true}}) {
     people {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

或者应用where过滤器:

export const Query = graphql`
 {
   source {
     people(where: {isSupport: true}) {
       name
       photo {
         url
       }
       surname
       isSupport
     }
  }
`

【讨论】:

  • 最简单的:将过滤器/where值where: {isSupport: true}传递给$where
  • ...只有最后一个可以工作:D 其他参数更改不会改变结果第一个:不允许使用道具名称的变量;第二个 $isSupport 应该是布尔值;第 3 个 $isSupport 参数未使用;第四个原始问题:D
  • query ($where: SomeWhereInputType){ source { people(where: $where) { 和变量:{ where: {isSupport: true} }
猜你喜欢
  • 2020-10-03
  • 2018-06-28
  • 2020-01-01
  • 1970-01-01
  • 2021-07-28
  • 2021-11-10
  • 2023-03-31
  • 1970-01-01
  • 2021-12-09
相关资源
最近更新 更多