[英]Get one user with a query in graphql
I have been trying to create create a query in grapqhl in express to get just one user我一直在尝试在 express 中的 grapqhl 中创建查询以仅获取一个用户
but is not working, it gives me all the users everytime但不起作用,它每次都会给我所有用户
this is my query, I am using typeorm as ORM这是我的查询,我使用 typeorm 作为 ORM
import { GraphQLList, GraphQLID } from 'graphql';
import { UserType } from '../TypeDefs/User';
import { Users } from '../../Entities/Users';
export const GET_ALL_USERS = {
type: new GraphQLList(UserType),
resolve() {
return Users.find();
},
};
export const GET_USER = {
type: GraphQLList(UserType),
args: {
userId: { type: GraphQLID },
},
async resolve(userId: any) {
// const { userId } = args;
const user = await Users.find({
where: {
userId: userId,
},
});
return user;
},
};
and this is the output in the Graphiql:这是 Graphiql 中的 output:
query{
getUser(userId:"2"){
name
email
userId
}
}
{
"data": {
"getUser": [
{
"name": "James",
"email": "james@gmail.com",
"userId": "1"
},
{
"name": "Alicia",
"email": "alicia@gmail.com",
"userId": "2"
}
]
}
}
It should be getting only the user named Alicia but it's getting all of them它应该只获取名为 Alicia 的用户,但它获取了所有用户
Maybe what's happening is that in your GET_USER
you are specifying type: GraphQLList(UserType)
as a return type, whereas you should be returning a single item: eg if you are sure it won't be null, a GraphQLNonNull
.也许发生的事情是在您的GET_USER
中您指定type: GraphQLList(UserType)
作为返回类型,而您应该返回一个项目:例如,如果您确定它不会是 null,一个GraphQLNonNull
。
Cheers干杯
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.