【问题标题】:Relation GraphQL关系图QL
【发布时间】:2018-04-12 18:02:43
【问题描述】:


尝试实现join,但总是得到null

type User {
    id: Int!
    username: String!
    recipes: [Recipe]
  }

  type Recipe {
    id: Int!
    title: String!
    author: User
  }


所以基本上我想得到这样的数据:

User { 
  username,
  recipes: [{//recipe}, {//recipe}]
}

对于我期待的食谱

Recipe {
  title,
  author: {//user}  
}

所以我有如下查询,我想从包含用户的数据库中获取所有食谱

type Query {
    recipes: [Recipe!]!
  }

这是我的 GraphiQL 查询

{
  recipes {
    id,
    author {
      id,
      username
    }
  }
}

但作为回应,我有author: null

{
  "data": {
    "recipes": [
      {
        "id": 1,
        "author": null
      }]
   }
}


有什么建议?谢谢!

【问题讨论】:

  • 问题很可能与您的解析器有关,而不是您的查询。请更新您的问题以包含您的解析器代码。
  • @DanielRearden,是的,这是解析器的问题,感谢您的回复

标签: graphql graphql-js


【解决方案1】:

也许有人会遇到类似的问题。 已解决此问题。正如 cmets 中的@Daniel Rearden 所说 - 是的,问题出在解析器中。

所以您必须将此字段添加到解析器:

const resolverFunctions = {
  User: {
      recipes(author) {
        return author.getRecipes();
      }
    },
  Recipe: {
      author(recipe) {
        return recipe.getUser();
      }
    }
}

在你得到我上面需要的数据之后。

【讨论】:

    猜你喜欢
    • 2011-04-01
    • 2019-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-29
    • 2019-09-19
    • 1970-01-01
    相关资源
    最近更新 更多