Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/411.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将graphql字段实现为函数是否表示JavaScript中闭包或提升行为的使用?_Javascript_Express_Graphql_Closures_Hoisting - Fatal编程技术网

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

将graphql字段实现为函数是否表示JavaScript中闭包或提升行为的使用?,javascript,express,graphql,closures,hoisting,Javascript,Express,Graphql,Closures,Hoisting,我最近一直在研究graphql,我发现要在我的模式中实现循环引用,我需要将我的字段属性声明为thunk或函数,引用它可以帮助我理解这一点 请允许我引用这个答案 假设文件中有两种类型,它们相互引用 BookType有一个字段author,并引用AuthorType。现在,假设您在BookType引用BookType下定义了AuthorType 因此,当Javascript引擎需要使用BookType时,它会看到字段.author.type是AuthorType并且上面没有定义AuthorType。

我最近一直在研究graphql,我发现要在我的模式中实现循环引用,我需要将我的字段属性声明为
thunk
函数
,引用它可以帮助我理解这一点

请允许我引用这个答案

假设文件中有两种类型,它们相互引用

BookType
有一个字段author,并引用
AuthorType
。现在,假设您在
BookType
引用
BookType
下定义了
AuthorType

因此,当Javascript引擎需要使用
BookType
时,它会看到
字段.author.type
AuthorType
并且上面没有定义
AuthorType
。因此它将给出
引用错误:未定义AuthorType

因此,
BookType
的正确实现是

const BookType= new GraphQLObjectType({ 
    name: 'BookType',
    fields: () => {  // as a thunk -> the correct implementation
      author: {
        type: AuthorType,

        resolve: (parentValue, args) => {
          // query the author based on your db implementation.
        }
      } 
    }
  });
我需要知道的是,将字段属性定义为
thunk
,对于循环引用或其自身类型的引用,描述JavaScript的用法


是提升还是闭合还是两者兼而有之?

是的,@Bergi你能解释一下为什么它是闭合的用法吗。我只看到提升的用法?如果JavaScript中没有闭包且只有提升可用,调用arrow函数会抱怨
未声明的变量“AuthorType”
@Bergi,因为它既不是参数,也不是在函数中定义的。
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.
        }
      }
    }
  });
const BookType= new GraphQLObjectType({ 
    name: 'BookType',
    fields: () => {  // as a thunk -> the correct implementation
      author: {
        type: AuthorType,

        resolve: (parentValue, args) => {
          // query the author based on your db implementation.
        }
      } 
    }
  });