• 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已经创建完成,所以就不会报错了.

  • 相关阅读:
    AndroidUI的组成部分ProgressBar
    NVIDIA+关联2015写学校招收评论(嵌入式方向,上海)
    谈论json
    排序算法(三):插入排序
    逻辑地址、线性地址、物理地址以及虚拟存储器
    逻辑地址、线性地址和物理地址的关系
    堆和栈都是虚拟地址空间上的概念
    缺页异常详解
    虚拟内存-插入中间层思想
    深入理解计算机系统之虚拟存储器
  • 原文地址:https://www.cnblogs.com/time-is-life/p/9293730.html
Copyright © 2020-2023  润新知