问题描述
我最近一直在研究 graphql,我发现要在我的架构中实现循环引用,我需要将我的字段属性声明为 thunk
或 function
,引用 {{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.type
是 AuthorType
而 AuthorType
没有在上面定义。所以它会给 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 (将#修改为@)