【发布时间】:2020-04-22 07:37:39
【问题描述】:
我正在尝试将 GraphQL (Apollo/Node.js) 和 gRPC (Go) 粘合在一起。到目前为止,我可以在他们之间进行交流。
但是,我无法从 gRPC 客户端回调中返回创建的用户值。
这是用户架构;
// 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
这是解析器;
// 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
这是 gRPC 客户端,它返回 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
})
}
请帮帮我。
编辑:
我也试过回调函数:
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);
}
}
在突变中仍然返回 null:
addUser: (_: any, {input}: any) => add(input, (res: any) => {
return res
}),
【问题讨论】:
-
@JohnMontgomery 不,它没有。我尝试使用变量检索
res,但未定义。
标签: node.js typescript graphql grpc apollo