如何在GraphQL中使用Sequelize多对多关联查询?收到空错误

时间:2019-02-09 17:00:09

标签: postgresql sequelize.js graphql apollo

我有很多关联,模型是Decks和Tag。我可以用两个对象console.log一个JSON字符串,但是我不确定如何设置我的架构和解析器以在一个查询中一起返回这两个查询,并且当前接收到null。我在Apollo服务器上使用带有Sequelize的GraphQL,PostgreSQL为数据库。在GraphQL中查询此关系的正确方法是什么,我想我没有为GraphQL正确返回数据以读取它?

模型

        Deck.belongsToMany(models.Tag, {
          through: models.DeckTag,
          onDelete: "CASCADE"
        });
      Tag.associate = models => {
        Tag.belongsToMany(models.Deck, {
          through: models.DeckTag,
          onDelete: "CASCADE"
        });

甲板方案

    export default gql`
      extend type Query {
        decks(cursor: String, limit: Int): DeckConnection!
        deck(id: ID, deckname: String): Deck!
        decksWithTags: [Deck!]!
      }

      extend type Mutation {
        createDeck(deckname: String!, description: String!): Deck!
        deleteDeck(id: ID!): Boolean!
      }

      type DeckConnection {
        edges: [Deck!]!
        pageInfo: DeckPageInfo!
      }

      type DeckPageInfo {
        hasNextPage: Boolean!
        endCursor: String!
      }

      type Deck {
        id: ID!
        description: String!
        createdAt: Date!
        user: User!
        cards: [Card!]
      }
    `;

有问题的解析器

        decksWithTags: async (parent, args, { models }) => {
          return await models.Deck.findAll({
            include: [models.Tag]
          }).then(tags => {
            console.log(JSON.stringify(tags));  //able to console.log correctly
          });
        },

缩短示例Console.logged JSON字符串示例

        [
          {
            "id":1,
            "deckname":"50 words in Chinese",
            "description":"Prepare for your immigration interview",
            ***
            "userId":1,
            "tags":[
                {
                  "id":1,
                  "tagname":"Chinese",
                  ***
                  "decktag":{
                      ***
                      "deckId":1,
                      "tagId":1
                  }
                },
                {
                  "id":2,
                  ***
          {
            "id":2,
     "deckname":"English",
     ***

我希望在GraphQL游乐场中获得类似于JSON字符串的结果。

0 个答案:

没有答案