随笔 - 547  文章 - 213 评论 - 417 阅读 - 107万

下面的代码中, 由于friends字段引用了PersonType字段,而friends本身又是PersonType的一部分,在运行的时候会报错:

Expected undefined to be a GraphQL type

 

var PersonType = new GraphQLObjectType({

    name: 'Person',

    description: '...',

    fields: {

                id: {

                    type: GraphQLString,

                    resolve : function (person) {

                        return person.first_name;

                    }

                },

                firstName: {

                    type: GraphQLString,

                    resolve : function (person) {

                        return person.first_name;

                    }

                },

                lastName: {

                    type: GraphQLString,

                    resolve : function (person) {

                        return person.last_name;

                    }

                },

                department: {

                    type: GraphQLString,

                    resolve : function (person) {

                        return person.department;

                    }

                },

                //email: { type: GraphQLString },

                //userName: { type: GraphQLString },

                //id: { type: GraphQLString },

                friends:  {

                     type: GraphQLList(PersonType)

                    //resolve: function (person) {

                    //    //return person.friends.map(getPersonByUrl);

                    //    return person.friends;

                    //}

                    }

            }

});

 

 

原因在于GraphQLList初始化的时候会检查PersonType的类型,而此时PersonType的定义尚未完成,所以还是undefined, 所以会报上面的错误.

[解决方案]

搜索到了这篇文章: https://gist.github.com/fbaiodias/77406c29ddf37fe46c3c

Fix

Using a function to return the fields on author.js does the trick:

On author.js

@@ -13 +13 @@

-   fields: {

+   fields: () => ({

 

 

把代码改成下面的就可以了.

var PersonType = new GraphQLObjectType( {

    name: 'Person',

    description: '...',

    fields: ()=>({

        id: {

            type: GraphQLString,

            resolve : function (person) {

                return person.first_name;

            }

        },

        firstName: {

            type: GraphQLString,

            resolve : function (person) {

                return person.first_name;

            }

        },

        lastName: {

            type: GraphQLString,

            resolve : function (person) {

                return person.last_name;

            }

        },

        department: {

            type: GraphQLString,

            resolve : function (person) {

                return person.department;

            }

        },

        //email: { type: GraphQLString },

        //userName: { type: GraphQLString },

        //id: { type: GraphQLString },

        friends: {

            type: GraphQLList(PersonType)

                    //resolve: function (person) {

                    //    //return person.friends.map(getPersonByUrl);

                    //    return person.friends;

                    //}

        }

    })

});

 

原理就是把fields放到函数中后,会在另一个线程中执行,所以执行的时候PersonType已经创建完成,所以就不会报错了.

posted on   今夜太冷  阅读(390)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
历史上的今天:
2016-07-11 COM中的HRESULT
2016-07-11 Ofstream的endl不好用怎么回事?
2016-07-11 string、wstring、cstring、 char、 tchar、int、dword转换方法(转)
点击右上角即可分享
微信分享提示