gpt4 book ai didi

graphql-compose - 您尝试从 Mongoose 模型生成名称为 "Projects"的 GraphQL 类型,但该类型已存在于 SchemaComposer 中

转载 作者:行者123 更新时间:2023-12-04 08:38:01 24 4
gpt4 key购买 nike

我使用不同的 mongodb 数据库来存储相同的 Mongoose 模式记录“页面”。因此,我希望拥有 X 个 graphql 端点 - 每个端点都应为请求的数据库的“页面”提供服务。
示例:localhost:3000/graphql/:database出于这个原因,我创建了一个 schemaComposer,它从路由中获取数据库名称(express > request.param.database)

import { composeMongoose } from 'graphql-compose-mongoose'
import { SchemaComposer } from 'graphql-compose'
import { Pages } from '../../mongoose/models/PagesModel.js'
import { Projects } from '../../mongoose/models/ProjectsModel.js'

const gqlSchemaComposerBuilder = (database) => {
const gqlSchemaComposer = new SchemaComposer()

const ProjectsTC = composeMongoose(Projects, {})
const PagesTC = composeMongoose(Pages(database), {})

gqlSchemaComposer.Query.addFields({
pageMany: PagesTC.mongooseResolvers.findMany(),
pageOne: PagesTC.mongooseResolvers.findOne(),

projectMany: ProjectsTC.mongooseResolvers.findMany(),
projectOne: ProjectsTC.mongooseResolvers.findOne()
})

gqlSchemaComposer.Mutation.addFields({
projectCreateOne: ProjectsTC.mongooseResolvers.createOne(),
projectUpdateOne: ProjectsTC.mongooseResolvers.updateOne(),
projectRemoveOne: ProjectsTC.mongooseResolvers.removeOne()
})

return gqlSchemaComposer.buildSchema()
}

export default gqlSchemaComposerBuilder
问题是,只要 composeMongoose声明被包裹在函数中我得到这个错误:
{"errors":[{"message":"You try to generate GraphQL Type with name Projects from mongoose model but this type already exists in SchemaComposer. Please choose another type name \"composeWithMongoose(model, { name: 'NewTypeName' })\", or reuse existed type \"schemaComposer.getOTC('TypeName')\", or remove type from SchemaComposer before calling composeWithMongoose method \"schemaComposer.delete('TypeName')\"."}]}

最佳答案

默认 composeMongoose()使用全局schemaComposer ,所以你需要通过你的 gqlSchemaComposercomponseMongo 的选项中

const gqlSchemaComposer = new SchemaComposer()

const ProjectsTC = composeMongoose(Projects, { schemaComposer: gqlSchemaComposer })
const PagesTC = composeMongoose(Pages(database), {schemaComposer: gqlSchemaComposer})

关于graphql-compose - 您尝试从 Mongoose 模型生成名称为 "Projects"的 GraphQL 类型,但该类型已存在于 SchemaComposer 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64705265/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com