带参数的Apollo graphql查询
我跟随这个Graphql介绍。我已经设置好文件(稍微修改),基本查询正在处理中http://localhost:4000/. 读完基础知识后,我的下一个问题是,如何根据参数获取数据?我已经完成了这一步,但游乐场中的查询不会返回结果 index.js带参数的Apollo graphql查询,graphql,apollo-server,Graphql,Apollo Server,我跟随这个Graphql介绍。我已经设置好文件(稍微修改),基本查询正在处理中http://localhost:4000/. 读完基础知识后,我的下一个问题是,如何根据参数获取数据?我已经完成了这一步,但游乐场中的查询不会返回结果 index.js const typeDefs = gql` type Item { name: String description: String full_url: String term:
const typeDefs = gql`
type Item {
name: String
description: String
full_url: String
term: String
}
type Query {
items: [Item]
itemsSearch(term: String!): [Item]
}
`;
const resolvers = {
Query: {
// this works. it is the example from the guide.
items: () => items,
// this doesn't work. `term` is always undefined
itemsSearch: term => {
console.log('term', term);
console.log('items', items);
return items.filter(item => item.title.indexOf(term) > -1 || item.author.indexOf(term) > -1);
},
},
};
然后我在操场上运行这个查询。(主要工作地点为:
我得到了成功的回复,但没有数据。如上所述,在itemsSearch解析器中记录术语将打印未定义的内容
知道我如何将param术语
传递给解析器并获得结果吗?提前感谢。这些是家长
,参数
,上下文
和信息
:
args
包含为此字段提供的所有GraphQL参数的对象
例如,当执行query{user(id:“4”)}
时,args
传递给用户解析器的对象是{“id”:“4”}
因此,您可以通过以下方式通过args
获得term
:
itemsSearch: (parent, { term }) => {
...
}
或:
你能试试
itemsSearch:(parent,{term})=>{
?@pzaenger是的!我现在有term
。谢谢。如果你想让我接受,请添加作为答案。这在同一教程的“下一步”中有详细描述,基本知识如果你想使用变量,不要问。。。
itemsSearch: (parent, { term }) => {
...
}
itemsSearch: (parent, args) => {
const term = args.term;
...
}