简体   繁体   English

函数构造函数对象与对象内部对象之间的javascript区别

[英]javascript difference between function constructor object and an object inside object

I am trying to comprehend/understand GraphQL schema instructor have used. 我正在尝试理解/理解GraphQL模式讲师已使用的内容。

So first he did something like this 所以首先他做了这样的事情

const BookType = new GraphQLObjectType({  
    name: 'Book', 

    fields: () => ({
        id: { type: GraphQLString},
        name: { type: GraphQLString},
        genre: { type: GraphQLString }
    })
})

Here, he mentioned that reason fields needs to be a function is because later when we have multiple type and they have reference to one another. 在这里,他提到原因字段必须是一个函数,是因为稍后当我们有多个类型并且它们相互引用时。 then unless we wrap it in function, one type won't know what other types (more later) 那么除非我们将其包装在函数中,否则一种类型将不知道其他类型是什么(稍后再介绍)

And afterwards when he was making RootQuery he did something like this 之后,当他制作RootQuery时,他做了类似的事情

const RootQuery = new GraphQLObjectType({
    name: "RootQueryType",
    fields: {
        book: {
          type: BookType, 
          args: { id: { type: GraphQLString }},
          resolve(parent, args){
           args.id
            }
         }
      }
})

Here, he did fields: { instead of fields: () => ({ for which he gave following reason 在这里,他做了fields: {而不是fields: () => ({为此,他给出了以下原因

We don't need to wrap it like the field above because we don't need to worry about the order so much inside root Query 我们不需要像上面的字段一样包装它,因为我们不必担心根查询中的订单太多

[Question:] I am unable to understand his explanation so I was looking for someone to explain me why he did fields: { instead of fields: () => ({ this? [问题:]我无法理解他的解释,所以我正在寻找某人向我解释为什么他这样做了fields: {而不是fields: () => ({这?

This is probably because inside your RootQueryType, the field book does not contain any reference of any of your custom declared types let's say AuthorType. 这可能是因为在RootQueryType中,该字段簿不包含任何对您的自定义声明类型的引用,例如AuthorType。 Your field book is therefore, only dependant on either BookType or any of the other GraphQLObjects that you may have imported at the top. 因此,您的现场书仅取决于BookType或您可能已在顶部导入的任何其他GraphQLObjects。 Same goes for the field author. 现场作者也是如此。 There should not be any reference of BookType inside it. 其中不应包含BookType的任何引用。

However, in the case of your custom Types( BookType or AuthorType), they might contain references of each other and hence they are dependant on each other. 但是,对于您的自定义类型(BookType或AuthorType),它们可能包含彼此的引用,因此它们彼此依赖。

Therefore when defining those custom types, your fields needs to be wrapped inside function. 因此,在定义这些自定义类型时,您的字段需要包装在函数中。 But that's not a necessity when it comes to the RootQuery. 但这并不是RootQuery的必要条件。

If you want to know more about it. 如果您想了解更多。 Look up the concept of Hoisting. 查找提升的概念。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM