Generation of schema with array of refs to another collection doesn't work
dbayarchyk opened this issue · comments
If I have the following field type in mongoose schema
MySchema = new mongoose.Schema({
items: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Item',
}],
});
And I want to generate graphql schema using this:
ExerciseType = createType({
name: 'MyType',
description: 'My schema',
class: 'GraphQLObjectType',
schema: MySchema,
exclude: ['items'],
extend: {
items: {
type: new GraphQLList(ItemType),
resolve: (root, params, context) => {
...
},
},
},
});
I get the following error:
Error:
type with name "Item" doesn't exist,
but was specified as population reference.
As I understand when I exclude items
field and extend this schema with my custom field it should skip generation of items
and use my custom specification for this field
Hi, if you are using the same name of prop. you shouldn't exclude it, just add it to extend
and that's it it will replace original one.
Hi
Thanks for your response, but I also tried your approach, but it didn't fix this issue.
ref: 'Item'
is Item
model specified? can I see the whole file? I never test referencing another schema by using ref: 'model name'
, but anyway it should just replace it. So if you can provide me the full example I can test it and see what's going on.
MuscleModel.js
import mongoose from 'mongoose';
export const MuscleSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
}
}, { collection: 'muscles', timestamps: true, versionKey: false });
export default mongoose.model('Muscle', MuscleSchema);
ExerciseModel.js
import mongoose from 'mongoose';
export const ExerciseSchema = new mongoose.Schema({
name: {
type: String,
required: true,
unique: true,
},
muscules: [{
type: mongoose.Schema.Types.ObjectId,
ref: 'Muscle',
}],
}, { collection: 'exercises', timestamps: true, versionKey: false });
export default mongoose.model('Exercise', ExerciseSchema);
ExerciseType.js
export const ExerciseType = createType({
name: 'ExerciseType',
description: 'Exercise schema',
class: 'GraphQLObjectType',
schema: ExerciseSchema,
exclude: ['muscules'],
extend: {
muscules: {
type: new GraphQLList(MuscleType),
resolve: (root, params, context) => {
// Resolve code here.
},
},
},
});
MuscleType.js
export const MuscleType = createType({
name: 'MuscleType',
description: 'Muscle schema',
class: 'GraphQLObjectType',
schema: MuscleSchema,
});
ok can you try new version mongoose-schema-to-graphql@2.7.0
should be fixed, and don't exclude that field if you are using the same name in extend
Cool, that works, thanks for your hard work