如何将嵌套解析器添加到架构?
How to add nested resolver to schema?
假设我的查询如下所示:
query {
post {
id
user { id, name }
}
}
解析器映射如下所示:
{
Query: {
post: myPostResolverFunc,
}
}
如何为 post.user 添加额外的 "nested" 解析器?
我试过这个但它不起作用:
addResolveFunctionsToSchema(schema, {
Query: {
post: {
user: postUserResolveFunc,
},
}
});
你只需要为你的领域写一个解析器。假设您的模式是这样的:
type Post {
id: ID!,
user: User
}
type User {
id: ID!,
username: String!
}
type Query {
post(id: ID!): Post #assuming you want to request a simple post here
}
你可以这样写解析器:
addResolveFunctionsToSchema(schema, {
Post: {
user(root) {
return getUserById(root.user)
}
}
Query: {
post(root, args, context) {
return getPostById(args.id)
}
}
});
假设我的查询如下所示:
query {
post {
id
user { id, name }
}
}
解析器映射如下所示:
{
Query: {
post: myPostResolverFunc,
}
}
如何为 post.user 添加额外的 "nested" 解析器? 我试过这个但它不起作用:
addResolveFunctionsToSchema(schema, {
Query: {
post: {
user: postUserResolveFunc,
},
}
});
你只需要为你的领域写一个解析器。假设您的模式是这样的:
type Post {
id: ID!,
user: User
}
type User {
id: ID!,
username: String!
}
type Query {
post(id: ID!): Post #assuming you want to request a simple post here
}
你可以这样写解析器:
addResolveFunctionsToSchema(schema, {
Post: {
user(root) {
return getUserById(root.user)
}
}
Query: {
post(root, args, context) {
return getPostById(args.id)
}
}
});