【发布时间】:2021-03-29 11:34:01
【问题描述】:
我正在尝试在我的项目中使用 mongoose、graphql 和 next.js,但不断收到此错误:
Error: You try to generate GraphQL Type with name Customer 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')".
我拥有的是简单的猫鼬模式:
const mongoose = require('mongoose');
const CustomerSchema = new mongoose.Schema({
name: {
type: String
}
}, {
toJSON: {
transform: function (doc, ret, options) {
ret.id = ret._id.toString();
delete ret.__v;
delete ret._id;
}
}
});
以及组成 gql 架构的单独模块:
import {composeMongoose} from "graphql-compose-mongoose";
import { schemaComposer } from 'graphql-compose';
import {Customer} from '../db/models/Customer';
const customizationOptions = {}
const CustomerTC = composeMongoose(Customer, customizationOptions);
schemaComposer.Query.addFields({
customerById: CustomerTC.mongooseResolvers.findById()
})
console.log("Schema:", schemaComposer.getOTC('Customer'))
const customerSchema = schemaComposer.buildSchema();
export default customerSchema;
和服务器 api 模块:
import {ApolloServer} from 'apollo-server-micro'
import customerSchema from '../../apollo/customer-type-defs';
const apolloServer = new ApolloServer({
schema: customerSchema,
context: () => ({})
});
export const config = {
api: {
bodyParser: false,
},
}
export default apolloServer.createHandler({path: '/api/graphql'})
有人可以告诉我如何修复错误吗?
【问题讨论】:
标签: mongoose graphql next.js apollo-server