使用GraphQL变量调用StarWars API报错:未知参数where求解决
解决StarWars GraphQL API使用变量查询影片标题的错误
问题场景
你在Postman中调用StarWars GraphQL API(POST - https://swapi-graphql.netlify.app/.netlify/functions/index)时,尝试通过变量过滤影片标题,使用了以下查询:
query Query ($title: String!) { allFilms { films (where: {title: {_eq: $title}}){ title director releaseDate speciesConnection { species { name classification homeworld { name } } } } } }
但收到错误:
"message": "Unknown argument \"where\" on field \"films\" of type \"FilmsConnection\"."
错误原因
该API的allFilms.films字段并不支持where参数,FilmsConnection类型的定义中没有这个过滤参数,因此无法通过这种方式筛选影片。
正确解决方案
改用API提供的film字段直接通过标题查询单部影片,这个字段支持接收title参数,结合变量的正确查询语句如下:
query GetFilmByTitle($title: String!) { film(title: $title) { title director releaseDate speciesConnection { species { name classification homeworld { name } } } } }
Postman中变量设置步骤
- 在Postman的GraphQL请求编辑器中,切换到Variables标签页
- 添加一个键为
title的变量,值设置为你要查询的影片标题(例如"A New Hope") - 发送请求即可获取对应影片的详细信息
补充说明
如果需要查询多部符合条件的影片,由于该API的allFilms没有提供过滤参数,你需要先获取所有影片数据,再在客户端进行筛选。
内容的提问来源于stack exchange,提问作者zishan paya
相关产品推荐
相关产品推荐

