GraphQL缺少名称 [英] GraphQL Missing a Name

查看:37
本文介绍了GraphQL缺少名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只需使用node和c#来学习GraphQL.我正在尝试将C#示例移植到节点上,因为这将是一个很好的学习练习(因为我不太了解node或graphql)

Just learning GraphQL using both node and c#. I am trying to port the C# example over to node since it will be a good learning exercise (since I don't know node or graphql that well)

我有2种类型.帐户和所有者(即帐户所有者)

I have 2 types. Account and Owner (i.e. account owner)

在以下情况(即拥有帐户的字段(列表)和第一个帐户的字段(单个对象))下,一切都可以正常工作

Everything works fine with the following (i.e. Fields for Owned Accounts (list) and First Account (single object)

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);
            }
        }
    }
}); 

当我尝试将AccountOwner的字段添加到AccountType时出现问题.我收到错误消息提供的用于构建架构的类型之一缺少名称."

The issue arises when I try to add a field for AccountOwner to the AccountType. I get the error "One of the provided types for building the Schema is missing a name."

我尝试在所有看不到的东西上都加一个名字.

I have tried putting a name on everything I could see which didn't help at all.

有问题的AccountType定义为:

The offending AccountType definition is:

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);
               }
        }
    }
});

如果您需要更多信息或任何其他代码,请告诉我.

If you need further info or any other code please let me know.

如果我更改两种类型(帐户和所有者)的声明并将它们放在相同的.js文件中,则它将起作用(请参见下文).我还更改了字段以返回箭头功能,我相信它将延迟某种绑定,直到所有内容加载完毕.

If I change the declaration of the two types (Account and Owner) and put them in the same .js file then it works (see below). I have also changed the fields to return an arrow function which I believe will delay some kind of binding till after everything is loaded.

所以现在我的问题是我应该如何将类型分成不同的文件.(JS不是我的强项)

So now my question is how should I separate the types into different files. (JS isn't my strong point)

编辑...更改了类型...

EDIT... altered Types...

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
}

推荐答案

Node.js允许循环依赖.并非所有模块系统都允许这样做,但是它可以在Node中工作.问题在于,无论首先解析哪个模块,都需要取消定义其中一个模块.在这种情况下,Node.js分配一个空对象作为此模块的导出.

Node.js allows circular dependencies. Not all module systems allow this but it works in Node. The problem is, that one of the modules needs to be undefined in whatever module gets parsed first. In this case, Node.js assigns an empty object as the export of this module.

因此,基本上,您有时会为一个空对象分配一个类型-从而抱怨该类型缺少name属性.好处是,如果您为其分配属性,对该对象的引用将不会更改.您必须做什么我已经在此处此处

So basically you are assigning an empty object as a type sometimes - thus complaining that the type is missing the name property. The good thing though is that the reference to this object will not change if you assign properties to it. What you have to do I have answered already here and here.

这篇关于GraphQL缺少名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆