无法在 GraphQL 架构中嵌套类型
Unable to nest Types in GraphQL Schema
我很难找出为什么我在 "page/pages" 上的 PageType 给出 "PageType is not defined",我是否可以通过其他方式引用它?我觉得这可能是对如何引用其他类型的简单忽视
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),
}
}),
})
我很难找出为什么我在 "page/pages" 上的 PageType 给出 "PageType is not defined",我是否可以通过其他方式引用它?我觉得这可能是对如何引用其他类型的简单忽视
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),
}
}),
})