为什么我的 graphql 嵌套查询返回 null?
Why is my graphql nested query returning null?
我是 graphql 的新手 运行 遇到嵌套查询问题,需要帮助传递 id 以识别关系。
查询
您可以在上方看到 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
查询,仅关系应该 return 只有 1 个隶属关系(具有匹配的 uid)。
我假设 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]
}
我见过一些使用 'nodes' 和 'edge' 类型的模式。由于我有许多其他关系,这会是定义图表的更好方法吗?
解析器
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;
},
},
};
模拟数据
//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",
},
]
是的,您应该在解析器中实现它。我猜你会在后台有一个数据库,例如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);
}
});
});
},
我是 graphql 的新手 运行 遇到嵌套查询问题,需要帮助传递 id 以识别关系。
查询
您可以在上方看到 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
查询,仅关系应该 return 只有 1 个隶属关系(具有匹配的 uid)。
我假设 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]
}
我见过一些使用 'nodes' 和 'edge' 类型的模式。由于我有许多其他关系,这会是定义图表的更好方法吗?
解析器
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;
},
},
};
模拟数据
//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",
},
]
是的,您应该在解析器中实现它。我猜你会在后台有一个数据库,例如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);
}
});
});
},