• GraphQL循环引用的问题


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

  • 相关阅读:
    Linux查看用于终止进程命令
    Linux查看当前正在运行的进程
    Windows 和 Linux 平台下的端口转发工具
    Windows 和 Linux 平台下的端口转发工具
    linux下最简单的端口转发工具
    linux下最简单的端口转发工具
    try与finally块中return的问题
    try与finally块中return的问题
    为啥还要写呢?——北漂18年序言
    JavaScript DOM对象和JQuery对象相互转换
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9293730.html
Copyright © 2020-2023  润新知