I'm trying to glue GraphQL (Apollo/Node.js) and gRPC (Go) together. So far, I can communicate between them.
However, I can't return a created user value from gRPC client callback.
Here's the user schema;
// schema.ts
import {gql} from 'apollo-server-express'
const schema = gql`
type Mutation {
addUser(input: AddUser): User
}
type User {
id: ID!
email: String
}
input AddUser {
email: String!
password: String!
}
`
export default schema
And this is the resolver;
// resolver.ts
import {add} from '../client'
const resolver = {
Query: {
users: () => console.log('in progress'),
},
Mutation: {
// addUser: (_: any, {input}: any) => add(input),
// This successfully logs the `res`
// addUser: (_: any, {input}: any) => add(input, (res: any) => console.log('Logged from resolver >', res)),
// This returns null in mutation
addUser: (_: any, {input}: any) => add(input, (res: any) => {
return res
}),
}
}
export default resolver
This is the gRPC client and it returns undefined.
// client.ts
export async function add(input: any) {
// Confirmed created in database
client.addUser({
email: input.email,
password: input.password
}, (_err: any, res: any) => {
// Successfully logs `res`
console.log('Logged res here > ', res)
return res
})
}
Please help me.
Edit:
I also tried with callback function:
export async function add(input: Input, callback: any) {
try {
await client.addUser({
email: input.email,
password: input.password
}, (_err: any, res: any) => {
console.log('Logged res here > ', res)
return callback(res)
})
} catch (error) {
console.log(error);
}
}
Still returns null in mutation:
addUser: (_: any, {input}: any) => add(input, (res: any) => {
return res
}),