无法在GraphQL架构中嵌套类型

时间:2017-05-28 21:10:51

标签: javascript reactjs graphql graphql-js

我很难找到为什么我的PageType在" page / pages"正在给予" PageType未定义",我有没有不同的方式来引用它?我有一种感觉,这可能是一个简单的忽略如何引用其他类型

class IntSpinbox(ttk.Frame):

    def __init__(self, parent, **kwargs):
        ttk.Frame.__init__(self,
                   parent,
                   borderwidth=kwargs.get('frameborderwidth', 2),
                   relief=kwargs.get('framerelief', tk.GROOVE))
        self.valuestr = tk.StringVar()
        self.valuestr2 = tk.StringVar()
        self.minvalue = kwargs.get('minvalue', 0)
        self.maxvalue = kwargs.get('maxvalue', 99)
        self.initval = kwargs.get('initvalue', self.minvalue)
        self.valuestr.set(str(self.initval))
        self.valuestr2.set(str(self.initval))
        self.label = ttk.Label(self,
                   text=kwargs.get('labeltext', 'No label'),
                   anchor='w',
                   width=kwargs.get('labelwidth', 20))
        self.spinbox = tk.Spinbox(self,
                   from_=self.minvalue,
                   to=self.maxvalue,
                   increment=1,
                   textvariable=self.valuestr)
        self.spinbox.bind('<Return>', self.updateSpinbox)
        self.spinbox.bind('<FocusOut>', self.updateSpinbox)
        self.spinbox.bind('<FocusIn>', self.storeSpinbox)
        self.spinbox.bind('<Button-1>', self.storeSpinbox)
        self.spinbox.bind('<Button-2>', self.storeSpinbox)

        self.label.pack(side=tk.TOP, fill=tk.X, expand=True, padx=5)
        self.spinbox.pack(side=tk.BOTTOM, fill=tk.X, expand=True, padx=2, pady=5)
        self.onChange = kwargs.get('onchange', self.doNothing)

    def storeSpinbox(self, event):
        tmpstr = self.valuestr.get()
        try:
            tmpval = int(tmpstr)
        except:
            tmpval = -1000
        if tmpval < self.minvalue:
            tmpval = self.minvalue
        elif tmpval > self.maxvalue:
            tmpval  = self.maxvalue
        self.valuestr2.set(str(tmpval))        

    def updateSpinbox(self, event):
        tmpstr = self.valuestr.get()
        try:
            tmpval = int(tmpstr)
        except:
            tmpstr = self.valuestr2.get()
            self.valuestr.set(tmpstr)
            return
        if tmpval < self.minvalue:
            tmpval = self.minvalue
        elif tmpval > self.maxvalue:
            tmpval  = self.maxvalue
        tmpstr = str(tmpval)
        self.valuestr.set(tmpstr)
        self.valuestr2.set(tmpstr)
        self.onChange()

    def doNothing(self):
        pass

    def getValue(self):
        tmpstr = self.valuestr.get()
        return(int(tmpstr))

    def setValue(self, value):
        self.valuestr.set(str(value))

错误(主要是为了帮助其他人搜索类似的问题):

const PageType = new GraphQLObjectType({
  name: 'Page',
  fields: {
    _id: { type: new GraphQLNonNull(GraphQLID) },
    title: { type: GraphQLString },
    subtitle: { type: GraphQLString },
    description: { type: GraphQLString },
    page: {
      type: PageType
    }
    pages: {
      type: new GraphQLList(PageType),
    }
  },
});

1 个答案:

答案 0 :(得分:1)

错误非常简单..您在进行分配之前尝试引用PageType。使用最小示例会出现相同的错误:

const foo = {
  innerFoo: foo // error: foo is not defined
}

这就是为什么在这些递归情况下使用通常被称为thunks的函数

const foo = {
  innerFoo: () => foo
}

当调用foo.innerFoo()时,foo已经定义,这将有效。由于这个原因,GraphQL模式支持将字段创建为函数。

const FooType = new GraphQLObjectType({
  name: 'Foo',
  fields: () => ({ // fields is a "thunk"
    foo: {
      type: FooType
    },
    foos: {
      type: new GraphQLList(FooType),
    }
  }),
})
相关问题