I am learning GraphQL. Just made a simple API with Apollo-server-express with Knex.js and PostgreSQL.
I am facing a weird issue with querying user by ID, which return null
.
This is user schema.
// user.schema.ts
extend type Query {
users: [User!]!
user(id: ID): User
}
This is user resolver.
// user.resolver.ts
user: async (_: any, { id }: { id: number }, { dataSources }: any) => {
await dataSources.userAPI.findOne({ id });
}
This is datasource for user.
async findOne({ id: idArg }: { id?: number } = {}) {
const user: User = await this.knex('user')
.where('id', idArg)
.first();
if (!user) return;
console.log('@ USER HERE > ', user)
return user;
}
Note: console.log
returns user
with values.
@ USER HERE > {
id: 1,
first_name: 'Some',
last_name: 'One',
email: 's@one.com',
password: '$2a$08$C8PP992UaUX.T90mxm2yD.RKB.ZnAx.gzMpK796JQei4H1BvVNxBG',
role: 'staff',
is_active: false,
created_at: 2019-09-17T16:03:06.949Z,
updated_at: 2019-09-17T16:03:06.949Z
}
But somehow, it returns null
in GraphQL playground.
{
"data": {
"user": null
}
}
Does it relate to the caching? Because I tested some cache controls from Apollo docs. This is what I added in user findOne
resolver before;
// Add cache hints dynamically, this will hide result and return null
// info.cacheControl.setCacheHint({ maxAge: 60, scope: 'PRIVATE' });
await dataSources.userAPI.findOne({ id });
After that it started returning null
, even after I removed it back. I have the same code for Post without testing that info.cache
. Querying by Post ID is working fine.
Please help me.