graphql-compose-mongoose
graphql-compose-mongoose copied to clipboard
Using a Map field
I have a schema that looks like -
let jobSchema = mongoose.Schema({
jobuuid: { type: String },
type: { type: String },
jobDetails: { type: Types.Mixed },
jobOptions: {
type: Map,
of: JobOptionsSchema
},
})
And trying to use a mutation that looks like -
mutation{
createJob(record:{jobOptions:{name:"demo"}}){
recordId
}
}
But im getting an error-
{
"errors": [
{
"message": "job validation failed: jobOptions: Cast to Map failed for value \"[Object: null prototype] { name: 'matthew' }\" at path \"jobOptions\"",
"locations": [
{
"line": 2,
"column": 3
}
],
"stack": [],
"path": [
"createJob"
]
}
],
"data": {
"createJob": null
}
}
Did something change with how to use the Map field?
Nope, everything stays the same from the last PR https://github.com/graphql-compose/graphql-compose-mongoose/commit/eecf9eab1b09b05f19ec67d24b7ccb35d06e00f8
Maybe something changed in mongoose?! But how I see today tests passed successfully - https://travis-ci.org/github/graphql-compose/graphql-compose-mongoose/builds/716152379
@MDrooker can you open PR with a broken test in this folder for your issue https://travis-ci.org/github/graphql-compose/graphql-compose-mongoose/builds/716152379 ?
Is it just that the GraphQL syntax for declaring the Map is wrong? Here's a barebones use of Mongoose's Map
SchemaType, adapted from the big group example in the Mongoose docs:
const schema = new Schema({
map: Map
});
const Thing = mongoose.model('Thing', schema);
const m = new Thing;
m.map = new Map([['key', 'value']]);
Note the list-of-tuples style of initialisation. I was having the same problem as MDrooker, but then found that the list-of-tuple syntax works from the GraphQL side, as opposed to object syntax:
mutation {
createJob( record: { jobOptions: [["name", "demo"]] }) {
recordId
}
}
The only problem I noticed then was that I could give any value, as it doesn't seem to validate against the desired schema for the of
field. It only fails to cast on bad values if I use a simple type like String
or Number
. But this schema validation issue seems like it would be a Mongoose problem, rather than one with graphql-compose-mongoose.