同时使用node和c#学习GraphQL。我正在尝试将C#示例移植到node,因为这将是一个很好的学习练习(因为我不太了解node或graphql )
我有两种类型。帐户和所有者(即帐户所有者)
以下内容一切正常(例如,拥有的帐户(列表)和第一个帐户(单个对象)的字段)
module.exports = new GraphQLObjectType({
name: 'OwnerType',
fields: {
Id: { type: GraphQLID},
Name: {type: GraphQLString},
Address: {type: GraphQLString},
OwnedAccounts: {
type: new GraphQLList(AccountType),
name: "OwnedAccounts",
resolve(obj, args, { mssqlConfig }){
return mssql_account(mssqlConfig).getByOwnerId(obj.Id);
}
},
FirstAccount: {
type: AccountType,
name: "FirstAccount",
resolve(obj, args, {mssqlConfig}){
return mssql_account(mssqlConfig).getFirstByOwnerId(obj.Id);
}
}
}
});
当我试图向AccountType中添加AccountOwner的字段时,出现了这个问题。我收到错误消息“用于构建Schema的一个提供的类型缺少名称”。
我试着给所有我能看到的东西起个名字,但都没有用。
令人不快的AccountType定义是:
module.exports = new GraphQLObjectType({
name: 'AccountType',
fields: {
Id: { type: GraphQLID },
Description: { type: GraphQLString },
OwnerId: { type: GraphQLID },
Type: { type: AccountTypeEnum },
AccountOwner: {
type: OwnerType,
resolve(obj, args, { mssqlConfig }){
return mssql_owner(mssqlConfig).get(obj.OwnerId);
}
}
}
});
如果您需要进一步的信息或任何其他代码,请让我知道。
编辑:如果我更改了两个类型(Account和Owner)的声明,并将它们放入相同的.js文件中,那么它就可以工作(见下文)。我还更改了字段以返回一个箭头函数,我相信该函数将延迟某种绑定,直到所有内容加载完毕。
所以现在我的问题是,我应该如何将这些类型划分到不同的文件中。(JS不是我的强项)
编辑...更改过的类型...
const {
GraphQLObjectType,
GraphQLID,
GraphQLString,
GraphQLList
} = require('graphql');
const AccountTypeEnum = require('./accountTypeEnum');
const mssql_owner = require('../../database/mssql/owner');
const mssql_account = require('../../database/mssql/account');
const ownerType = new GraphQLObjectType({
name: 'OwnerType',
fields: () => ({
Id: { type: GraphQLID, name: "Id"},
Name: {type: GraphQLString, Name: "Name"},
Address: {type: GraphQLString},
OwnedAccounts: {
type: new GraphQLList(accountType),
name: "OwnedAccounts",
resolve(obj, args, { mssqlConfig }){
return mssql_account(mssqlConfig).getByOwnerId(obj.Id);
}
},
FirstAccount: {
type: accountType,
name: "FirstAccount",
resolve(obj, args, {mssqlConfig}){
return mssql_account(mssqlConfig).getFirstByOwnerId(obj.Id);
}
}
})
});
const accountType = new GraphQLObjectType({
name: 'AccountType',
fields: () => ({
Id: { type: GraphQLID, name: "Id" },
Description: { type: GraphQLString, name: "Description" },
OwnerId: { type: GraphQLID, name: "OwnerId" },
Type: { type: AccountTypeEnum, name: "Type" },
AccountOwnerFoo: {
name: "Wombat",
type: ownerType,
resolve(parent, args, {mssqlConfig}){
return mssql_owner(mssqlConfig).get(parent.OwnerId);
}
}
})
});
module.exports = {
ownerType,
accountType
}
发布于 2020-05-05 18:20:10
发布于 2020-05-08 18:22:11
为什么会出现这个错误?因为您提供的类型在编译时未定义。因此,当编译器尝试编译该文件时,它会搜索您在此处指定的类型,即AccountType
。它不是编译的。这就是为什么OwnerType
没有得到AccountType
,而你得到了一个错误。
简单的解决方案:
您需要在OwnerType
文件中导入AccountType
。
const AccountType = require('./AccountType.js');
在OwenerType
代码之前。这可能会有所帮助。
https://stackoverflow.com/questions/61484668
复制相似问题