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

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

问题描述

刚刚使用 node 和 c# 学习 GraphQL.我正在尝试将 C# 示例移植到 node 上,因为这将是一个很好的学习练习(因为我不太了解 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)

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

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.js 中有效.问题是,其中一个模块需要在首先解析的任何模块中未定义.在这种情况下,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天全站免登陆