[英]Apollo GraphQL - resolver not being triggered
I'm new to GraphQL.我是 GraphQL 的新手。 I started with the Javascript Apollo library and cannot get a resolver to fire.
我从 Javascript Apollo 库开始,但无法触发解析器。 It's hard for me to tell when resolvers should be called, and what key I should put.
我很难判断何时应该调用解析器,以及应该放置什么键。
Here's my schema and resolvers:这是我的架构和解析器:
// The GraphQL schema in string form
const typeDefs = `
type Query { user(id: ID!): User, posts: Post }
type User { id: ID!, created: Int, username: String, bio: String, status: String, avatar: String, posts: [Post] }
type Post { id: ID!, created: Int, user_id: Int, text: String, object_set_id: Int, edited: Int, views: Int, upvotes: Int, downvotes: Int, Parent: Int }
`;
// The resolvers
const resolvers = {
Query: {
user: (parent, args, context, info) => {
return users.find(user => user.id === Number(args.id));
},
// posts: () => posts,
},
Post: {
id: (parent, args, context, info) => {
console.log(posts.filter(post => post.user_id === Number(parent.id)))
return posts.filter(post => post.user_id === Number(parent.id))
}
}
};
I want to specify a user by their user ID, then filter down to get all the posts that user has made (eventually with a time filter).我想通过用户 ID 指定用户,然后过滤以获取用户发布的所有帖子(最终使用时间过滤器)。
I've tried several things, but 'posts' under a user always comes up 'null'.我尝试了几件事,但是用户下的“帖子”总是显示为“空”。 What am I missing?
我错过了什么? Thanks.
谢谢。
Your first query works fine .您的第一个查询工作正常。 You should also add some
data
(Otherwise you get an error "message": "users is not defined",
).您还应该添加一些
data
(否则您会收到错误"message": "users is not defined",
)。
Execute query:执行查询:
query{
user(id: 1){
id
username
}
}
Basic code example:基本代码示例:
const { ApolloServer, gql } = require("apollo-server");
const users = [
{
id: 1,
username: "Kate Chopin"
},
{
id: 2,
username: "Paul Auster"
}
];
// The GraphQL schema in string form
const typeDefs = gql`
type Query {
userById(id: ID!): User
}
type User {
id: ID!
username: String
}
`;
// The resolvers
const resolvers = {
Query: {
userById: (parent, args, context, info) => {
return users.find((user) => user.id === Number(args.id));
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
postById
):postById
): About posts
- your query is posts
and no resolver match + looks like you do a little "salad" their.关于
posts
- 您的查询是posts
并且没有解析器匹配 + 看起来您对他们做了一点“沙拉”。
It is better to follow this example ( Same idea only books/authors instead of posts/user ): https://www.apollographql.com/docs/apollo-server/getting-started/最好遵循这个例子(同样的想法只有书籍/作者而不是帖子/用户): https : //www.apollographql.com/docs/apollo-server/getting-started/
Next, read this Resolver chains : https://www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-chains接下来,阅读这个解析器链: https : //www.apollographql.com/docs/apollo-server/data/resolvers/#resolver-chains
Again hello world
example再次
hello world
例子
const posts = [
{
id: 1,
name: "Article One"
},
{
id: 2,
name: "Article Two"
}
];
const users = [
{
id: 1,
username: "Kate Chopin",
posts: posts
},
{
id: 2,
username: "Paul Auster"
}
];
// The GraphQL schema in string form
const typeDefs = gql`
type Query {
userById(id: ID!): User
}
type User {
id: ID!
username: String
posts: [Post]
}
type Post {
id: ID!
name: String
}
`;
// The resolvers
const resolvers = {
Query: {
userById: (parent, args, context, info) => {
return users.find((user) => user.id === Number(args.id));
}
}
};
const server = new ApolloServer({
typeDefs,
resolvers
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.