I've been struggling to create a GraphQL query in Express that retrieves only one user instead of all users every time.
This is the query I'm using, incorporating TypeORM as my 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;
},
};
Here's the output displayed in Graphiql:
query{
getUser(userId:"2"){
name
email
userId
}
}
{
"data": {
"getUser": [
{
"name": "James",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="365c575b534576515b575f5a1855595b">[email protected]</a>",
"userId": "1"
},
{
"name": "Alicia",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="5534393c363c34153238343c397b363a38">[email protected]</a>",
"userId": "2",
}
]
}
}
It appears to be retrieving all users instead of just Alicia as intended.