码迷,mamicode.com
首页 > 其他好文 > 详细

GraphQL循环引用的问题

时间:2018-07-11 15:00:09      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:var   hql   desc   expected   last   git   UNC   nbsp   创建   

下面的代码中, 由于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已经创建完成,所以就不会报错了.

GraphQL循环引用的问题

标签:var   hql   desc   expected   last   git   UNC   nbsp   创建   

原文地址:https://www.cnblogs.com/time-is-life/p/9293730.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!