title: Does implementing graphql fields as functions, represent the usage of closure or hoisting behaviors in JavaScript?
body: <p>I've been studying graphql recently and I found out to implement a circular reference inside my schema, I need to declare my field property as a <code>thunk</code> or a <code>function</code>, referring <a href="https://stackoverflow.com/questions/39579416/graphql-fields-as-a-function/62736260#62736260">this answer</a> it helped me a lot to understand this.</p>
  <p>If I may quote from that answer,</p>
  <blockquote>
  <p>Imagine you have two types in a file and they are referencing each other.</p>
  </blockquote>
  <pre><code>const BookType= new GraphQLObjectType({ 
      name: 'BookType',
      fields: {  // as object -&gt; not the correct implementation yet
        author: {
          type: AuthorType,
          
          resolve: (parentValue, args) =&gt; {
            // query the author based on your db implementation.
          }
        }
      }
    });
  </code></pre>
  <blockquote>
  <p><code>BookType</code> has a field author and referencing <code>AuthorType</code>. Now imagine you have <code>AuthorType</code> defined under <code>BookType</code> referencing <code>BookType</code>,</p>
  </blockquote>
  <pre><code>const AuthorType= new GraphQLObjectType({
      name: 'AuthorType',
      fields: {  // as object
        books: {
          type: new GraphQLList(BookType), //one author might have multiple books
          
          resolve: (parentValue, args) =&gt; {
            // query the books based on your db implementation.
          }
        }
      }
    });
  </code></pre>
  <blockquote>
  <p>So when Javascript engine needs to use <code>BookType</code> it will see that <code>fields.author.type</code> is <code>AuthorType</code> and <code>AuthorType</code> is not defined above. So it will give <code>reference error:AuthorType is not defined</code>.</p>
  </blockquote>
  <p>So the correct implementation of <code>BookType</code> would be,</p>
  <pre><code>const BookType= new GraphQLObjectType({ 
      name: 'BookType',
      fields: () =&gt; {  // as a thunk -&gt; the correct implementation
        author: {
          type: AuthorType,
  
          resolve: (parentValue, args) =&gt; {
            // query the author based on your db implementation.
          }
        } 
      }
    });
  </code></pre>
  <p>What I need to know is defining the field property as a <code>thunk</code>, <em>for circular references or for references of its own type</em>, describes what usage of JavaScript.</p>
  <p>Is it the hoisting or a closure or the both?</p>