为什么我的graphql嵌套查询返回null?

时间:2017-05-03 22:26:00

标签: graphql relayjs

我对graphql遇到嵌套查询问题感到陌生,需要帮助传递id来识别关系。

查询

enter image description here

上面你可以看到PERFORMED_FOR_Affiliation为空,尽管它在模式中被定义为Affiliation类型。

type Query {
      affiliations(affn_id: ID!): [Affiliation]
      performances(pfrm_id: ID!): [Performance]
      PERFORMED_FOR_Affiliation(affn_id: ID!): Affiliation
      Performance_PERFORMED_FOR(pfrm_id: ID!): [Performance]
    }

PERFORMED_FOR_Affiliation查询类似于affiliations查询只有关系应该只返回1个从属关系(与匹配的uid)。

enter image description here

我认为affn_id没有正确传递,也不确定如何正确地做到这一点。 PERFORMED_FOR_Affiliation是否需要自己的架构?

模式

type Performance {
    pfrm_id: ID!
    mark: Int
    affn_id: ID!
    PERFORMED_FOR_Affiliation: Affiliation
}

type Affiliation {
    affn_id: ID!
    name: String
    Performance_PERFORMED_FOR: [Performance]
}

我看过一些使用'节点'和'边缘'类型。由于我有许多其他关系,这将是一个更好的方式来定义图形?

解析器

import performances from './mockData/performances.js';
import affiliations from './mockData/affiliations.js';

export const resolvers = {
  Query: {
    affiliations: (root, args) => {
      return affiliations;
    },
    performances: (root, args) => {
      return performances;
    },
    PERFORMED_FOR_Affiliation: (root, args) => {
      return affiliations;
    },
    Performance_PERFORMED_FOR: (root, args) => {
      return performances;
    },
  },
};

MockData

//affiliations.js
module.exports = [
  {
    "affn_id": "43700F3BE17145399924AC176EACBEF4",
    "name": "Richmond Senior"
  },
  {
    "affn_id": "8BDE709AC757416082950B1BEED0CE0A",
    "name": "Cedar City"
  },
  {
    "affn_id": "123D201BB17545E3B6ECCCCB5FC61FA3",
    "name": "Delta"
  }
]

// performances.js
module.exports = [
  {
    pfrm_id: "6BD41C6B1C4B43D199DE42A4A408DF1A",
    mark: 1270000,
    affn_id: "43700F3BE17145399924AC176EACBEF4",
  },
  {
    pfrm_id: "EA2FBC6AB891460EA557F5B60984AD8A",
    mark: 1422400,
    affn_id: "8BDE709AC757416082950B1BEED0CE0A",
  },
  {
    pfrm_id: "54A6EEB9552C49AC9F7A87E68AC272A2",
    mark: 1422400,
    affn_id: "123D201BB17545E3B6ECCCCB5FC61FA3",
  },
]

1 个答案:

答案 0 :(得分:1)

是的,你应该在你的解析器中实现它。我猜您将在后台拥有一个数据库,例如mongoose

您查询表演,并在其中populate附属机构



performances: (root, args) => {
  return new Promise((resolve, reject) => {
    performanceModel.find() // find all performances
      .populate("PERFORMED_FOR_Affiliation") // add values for affiliates
      .exec((error, performances) => {
        if (!error) {
          resolve(performances); // resolve correct object
        } else {
          reject(error);
        }
      });
  });
},