微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

将 graphql 字段实现为函数,是否代表 JavaScript 中闭包或提升行为的使用?

如何解决将 graphql 字段实现为函数,是否代表 JavaScript 中闭包或提升行为的使用?

我最近一直在研究 graphql,我发现要在我的架构中实现循环引用,我需要将我的字段属性声明为 thunkfunction,引用 {{3} } 这对我理解这一点很有帮助。

如果我可以引用那个答案,

假设您在一个文件中有两种类型,并且它们相互引用。

const BookType= new GraphQLObjectType({ 
    name: 'BookType',fields: {  // as object -> not the correct implementation yet
      author: {
        type: AuthorType,resolve: (parentValue,args) => {
          // query the author based on your db implementation.
        }
      }
    }
  });

BookType一个领域作者并引用 AuthorType。现在假设您在 AuthorType 下定义了 BookType,引用 BookType,

const AuthorType= new GraphQLObjectType({
    name: 'AuthorType',fields: {  // as object
      books: {
        type: new GraphQLList(BookType),//one author might have multiple books
        
        resolve: (parentValue,args) => {
          // query the books based on your db implementation.
        }
      }
    }
  });

所以当 Javascript 引擎需要使用 BookType 时,它会看到 fields.author.typeAuthorTypeAuthorType 没有在上面定义。所以它会给 reference error:AuthorType is not defined

所以 BookType 的正确实现是,

const BookType= new GraphQLObjectType({ 
    name: 'BookType',fields: () => {  // as a thunk -> the correct implementation
      author: {
        type: AuthorType,args) => {
          // query the author based on your db implementation.
        }
      } 
    }
  });

我需要知道的是将字段属性定义为 thunk用于循环引用或用于自身类型的引用,描述 JavaScript 的用法

是吊装还是关闭,还是两者兼而有之?

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。