Apollo/GraphQL具有相同参数的相同查询 [英] Apollo / GraphQL Same Query with Different Parameters
本文介绍了Apollo/GraphQL具有相同参数的相同查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何处理具有不同参数的Apollo查询.假设我的应用程序具有用户概念,我希望我的API能够通过ID或用户名找到用户.
How would I go about having an Apollo query with different parameters. Let's say my app has the concept of users and I want my API to be able to find a user either by ID or username.
看来我不能做到这一点
type Query {
user(id: ID!): User
user(username: String!): User
}
我是否必须诉诸于此
type Query {
userById(id: ID!): User
userByUsername(username: String!): User
}
推荐答案
使用ID和用户名作为字段创建新的输入类型,并将其指定为参数.
Create a new input type with ID and username as fields and specify it as parameter.
type Query {
getUser(filters:UserFilter): User
}
input UserFilter{
ID : Int,
Username : String
}
查询应如下所示
Query{
getUser(filters:{ID:123123})
}
或用于用户名过滤器
Query{
getUser(filters:{Username:"Tom"})
}
这篇关于Apollo/GraphQL具有相同参数的相同查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文