原始代码来自此处:https://github.com/apollographql/federation-demo/blob/master/services/reviews/index.js
我的代码是这样的:
const typeDefs = gql`
extend type User @key(fields: "id") {
id: ID! @external
numberOfReviews: Int
reviews: [Review]
}
type Review @key(fields: "id") {
id: ID!
body: String
author: User
product: Product
}
extend type Product @key(fields: "upc") {
upc: String! @external
reviews: [Review]
}
`;
const resolvers = {
Review: {
author(review) {
// Review.author -> provides the field `username` and requires User.username to be marked as @external.
console.log("review service returning author")
return { __typename: "User", id: review.authorID };
}
},
User: {
reviews(user, _args, { dataSources }) {
console.log("review service returning reviews for user " + user.id)
return reviews.filter(review => review.authorID === user.id);
},
numberOfReviews(user, _args, { dataSources }) {
console.log("review service returning numberOfReviews for user " + user.id)
return reviews.filter(review => review.authorID === user.id).length;
}
},
Product: {
reviews(product, _args, { dataSources }) {
console.log("review service returning reviews for product " + product.upc)
return reviews.filter(review => review.product.upc === product.upc);
}
}
};
我有其他非常相似的服务,它们可以毫无问题地解析类型。
我该怎么做才能解决这个错误?
Fetching info from federated service
✔ Loading Apollo Project
✔ Loading Apollo Project
✖ Uploading service to Engine
→ Unknown type: "User".
Error: Unknown type: "User".