简体   繁体   English

Apollo GraphQL - 未触发解析器

[英]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.谢谢。

User (Or more semantic: userById) query用户(或更多语义:userById)查询

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}`);
});

posts query (Or more semantic: 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}`);
});

Query:询问: 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 上下文未传递给嵌套的Apollo GraphQL解析器 - Context not being passed to nested Apollo GraphQL resolver Apollo GraphQL:如何拥有使用递归的解析器? - Apollo GraphQL: How to have a resolver that uses recursion? 设置 http 仅来自 apollo graphql 解析器的 cookie - Set http only cookie from resolver in apollo graphql Apollo GraphQL:扩展而不是覆盖默认解析器行为 - Apollo GraphQL: Augment instead of overriding default resolver behaviour 如何在Apollo GraphQL解析器的一个函数中解析2种不同的方法? - How to resolve 2 different methods in one function on Apollo GraphQL resolver? Apollo GraphQL解析器类型签名中的info参数为空 - info argument is empty in Apollo GraphQL resolver type signature 如何在GraphQL解析器中访问请求对象(使用Apollo-Server-Express) - How to access the request object inside a GraphQL resolver (using Apollo-Server-Express) Apollo GraphQL,有没有办法在查询期间操作正在写入缓存的数据? - Apollo GraphQL, is there a way to manipulate the data that is being written to the cache during querying? Apollo GraphQL 不断接收请求,没有进行查询或突变 - Apollo GraphQL keeps receiving requests with no queries or mutations being made 使用 Apollo 客户端时,GraphQL 突变未发送到请求? - GraphQL mutation not being sent to request when using Apollo client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM