我有2种自定义graphql类型,entityType和containerType,其中1个容器可以具有多个实体。
因此,我通过以下代码将所有实体与其各自的容器绑定在一起:
const graphql = require('graphql')
const _ = require('lodash')
const UserType = require('./userSchema')
const ContainerType = require('./containerSchema')
const Container = require('../models/container')
const Entity = require('../models/entity')
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLSchema,
GraphQLInt,
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLUnionType
} = graphql
const EntityType = new GraphQLObjectType({
name: "Entity",
fields: () => ({
name: { type: new GraphQLNonNull(GraphQLString) },
container: {
type: ContainerType,
resolve: function(parent, args) {
return Container.findById(parent.containerId)
}
},
type: { type: new GraphQLNonNull(GraphQLString) },
detail: { type: new GraphQLNonNull(GraphQLString) },
start: { type: GraphQLString },
end: { type: GraphQLString }
})
})
module.exports = { EntityType }
我非常确定containerType可以正常工作,因为我正在其他地方使用它并且运行良好。这是容器类型的代码:
const graphql = require('graphql')
const _ = require('lodash')
const UserType = require('./userSchema')
const { EntityType } = require('./entitySchema')
const User = require('../models/user')
const Container = require('../models/container')
const Entity = require('../models/entity')
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLSchema,
GraphQLInt,
GraphQLID,
GraphQLList,
GraphQLNonNull
} = graphql
const ContainerType = new GraphQLObjectType ({
name: 'Container',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
user: {
type: UserType,
resolve: function (parent, args) {
return User.findById(parent.userId)
}
},
parent: {
type: ContainerType,
resolve: function (parent, args) {
return Container.findById(parent.parentContainer)
}
},
detail: { type: GraphQLString },
start: { type: GraphQLString },
end: { type: GraphQLString },
createdAt: { type: GraphQLString },
category: { type: GraphQLString },
status: { type: GraphQLString },
entities: {
type: GraphQLList(EntityType),
resolve: async function(parent, args) {
return await Entity.find({ containerId: parent.id })
}
}
})
})
module.exports = ContainerType
终端上没有显示错误,但在加载时Graphiql控制台显示以下错误:
{
"errors": [
{
"message": "The type of Entity.container must be Output Type but got: {}."
},
{
"message": "Expected GraphQL named type but got: {}."
}
]
}
答案 0 :(得分:1)
我发现问题出在所谓的“模块周期”。 this post对于解决该问题非常有帮助。
最后,我最终在一个文件中添加了EntitySchema和ContainerSchema,这是代码:
const graphql = require('graphql')
const _ = require('lodash')
const UserType = require('./userSchema')
const User = require('../models/user')
const Container = require('../models/container')
const Entity = require('../models/entity')
const {
GraphQLObjectType,
GraphQLString,
GraphQLBoolean,
GraphQLInt,
GraphQLID,
GraphQLList,
GraphQLNonNull
} = graphql
const EntityType = new GraphQLObjectType ({
name: "Entity",
fields: () => ({
name: { type: new GraphQLNonNull(GraphQLString) },
container: {
type: ContainerType,
resolve: (parent, args) => {
return Container.findById(parent.containerId)
}
},
type: { type: new GraphQLNonNull(GraphQLString) },
detail: { type: new GraphQLNonNull(GraphQLString) },
start: { type: GraphQLString },
end: { type: GraphQLString }
})
})
const ContainerType = new GraphQLObjectType ({
name: 'Container',
fields: () => ({
id: { type: GraphQLID },
name: { type: GraphQLString },
user: {
type: UserType,
resolve: function (parent, args) {
return User.findById(parent.userId)
}
},
parent: {
type: ContainerType,
resolve: function (parent, args) {
return Container.findById(parent.parentContainer)
}
},
detail: { type: GraphQLString },
start: { type: GraphQLString },
end: { type: GraphQLString },
createdAt: { type: GraphQLString },
category: { type: GraphQLString },
status: { type: GraphQLString },
entities: {
type: GraphQLList(EntityType),
resolve: async function(parent, args) {
return await Entity.find({ containerId: parent.id })
}
}
})
})
module.exports = {
EntityType,
ContainerType
}