node.js - 为什么孩子的resolve函数不包含父母的resolve函数的结果?
问题描述
我试图隔离这个例子,我希望它没问题。我知道,这不是很好的代码,但我希望你能理解。
解析器暂时返回一个静态结果对象。
这是我的问题:
公司解析函数的结果应该传递给用户的解析函数。但这并没有发生,我想知道我错过了什么。
const GraphQL = require('graphql');
const UserType = new GraphQL.GraphQLObjectType({
name: 'User',
fields: {
givenName: { type: GraphQL.GraphQLString },
familyName: { type: GraphQL.GraphQLString },
city: { type: GraphQL.GraphQLString },
},
});
const CompanyType = new GraphQL.GraphQLObjectType({
name: 'Company',
fields: {
legalName: { type: GraphQL.GraphQLString },
city: { type: GraphQL.GraphQLString },
employees: { type: new GraphQL.GraphQLList(UserType) },
},
});
const queryDef = new GraphQL.GraphQLObjectType({
name: 'Query',
fields: {
user: {
type: UserType,
args: {
id: { type: GraphQL.GraphQLID },
givenName: { type: GraphQL.GraphQLString },
familyName: { type: GraphQL.GraphQLString },
city: { type: GraphQL.GraphQLString },
},
resolve: (parent, args, context, info) => {
console.log('parent should provide company object', parent);
// currentyl parent is undefined
return {
id: 10,
givenName: 'test',
};
},
},
company: {
type: CompanyType,
args: {
id: { type: GraphQL.GraphQLID },
},
resolve: (parent, args, context, info) => {
return {
id: 3,
legalName: 'legal test name',
city: 'company location',
};
},
},
},
});
const schema = new GraphQL.GraphQLSchema({ query: queryDef });
const companyQuery = `
{
company(id: 1) {
city
employees {
familyName
}
}
}`;
GraphQL.graphql(schema, companyQuery).then( (companyResult) => {
console.log(companyResult);
} ).catch( (err) => {
console.error(err);
});
解决方案
推荐阅读
- postgresql - Postgres:为不同角色/用户创建的未来表授予对角色/用户的访问权限
- r - 如何在 R 中解密字符串(算法河豚)
- android - Select Query ArrayList java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
- angular - 如何使用条件复选框在材料表中应用排序
- c# - 如何使用 c# 将结果值从 sql Query 返回到 bot 应用程序作为回复
- javascript - 未捕获的类型错误:无法读取 null Chrome 扩展的属性“点击”
- php - 您可以在查询后使用类似 WHERE 子句的内容吗?
- mysql - MySQL基于两列的多个日期时间间隔
- javascript - 强制填充字符串字段的正则表达式
- eclipse - 无法从 Eclipse 市场空间下载 Kotlin 插件