我很难找出为什么我在“页面/页面”上的PageType给出“未定义PageType”的原因,我是否有另一种方式可以引用它?我觉得这可能是一个如何忽略其他类型的简单忽略
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),
}
},
});
错误(主要是为了帮助其他人搜索类似问题):
ReferenceError: PageType is not defined
at Object.<anonymous> (H:\Coding\Projects\react-starter-kit\build\webpack:\src\data\types\PageType.js:50:1)
at __webpack_require__ (H:\Coding\Projects\react-starter-kit\build\webpack:\webpack\bootstrap 8ce5b4572987765a465d:19:1)
at Object.<anonymous> (H:\Coding\Projects\react-starter-kit\build\server.js:731:74)
at __webpack_require__ (H:\Coding\Projects\react-starter-kit\build\webpack:\webpack\bootstrap 8ce5b4572987765a465d:19:1)
at Object.<anonymous> (H:\Coding\Projects\react-starter-kit\build\webpack:\src\data\schema.js:1:1)
at __webpack_require__ (H:\Coding\Projects\react-starter-kit\build\webpack:\webpack\bootstrap 8ce5b4572987765a465d:19:1)
at Object.<anonymous> (H:\Coding\Projects\react-starter-kit\build\server.js:1098:72)
at __webpack_require__ (H:\Coding\Projects\react-starter-kit\build\webpack:\webpack\bootstrap 8ce5b4572987765a465d:19:1)
at Object.<anonymous> (H:\Coding\Projects\react-starter-kit\build\server.js:7455:18)
at __webpack_require__ (H:\Coding\Projects\react-starter-kit\build\webpack:\webpack\bootstrap 8ce5b4572987765a465d:19:1)
at H:\Coding\Projects\react-starter-kit\build\webpack:\webpack\bootstrap 8ce5b4572987765a465d:65:1
at Object.<anonymous> (H:\Coding\Projects\react-starter-kit\build\server.js:71:10)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
最佳答案
该错误非常简单..您正在尝试在进行赋值之前引用PageType。一个最小的例子将发生相同的错误:
const foo = {
innerFoo: foo // error: foo is not defined
}
这就是为什么在这些递归情况下使用通常称为thunk的函数的原因
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),
}
}),
})