Return Null On Update Mutation GraphQL And Sequelize
I would like to know why my response is "null" when I execute my mutation please.
My query works : the state is_active become true or false in the database SQL when I execute the request.
When I create, I have a correct response, but not on the update.
Graphql request :
mutation {
updateSequence(id: 4, , input: {is_active: true}) {
is_active
}
}
The response :
{
"data": {
"updateSequence": {
"is_active": null
}
}
}
resolvers.js
Mutation : {
createSequence(_, {input}) {
return models.Sequence.create(input);
},
updateSequence(_, {id, input}) {
return models.Sequence.update(input, {where: {id: id}});
}
}
schema.js
# Sequence
type Sequence {
id: Int!,
code: String,
buckets: [Bucket],
sequence_types : [SequenceType]
is_active: Boolean,
}
# SequenceInput
input SequenceInput {
is_active: Boolean
}
...
type Query {
sequences: [Sequence]
sequence(id: Int): Sequence
}
type Mutation {
createSequence(input: SequenceInput): Sequence,
updateSequence(id: ID!, input: SequenceInput): Sequence
}
schema {
query: Query,
mutation: Mutation
}
SOLVED : Migrate MySQL to Postegres to use returning option (only Postegres)
I migrate my database to use the returning option from Sequelize.
createSequence(_, {input}) {
return models.Sequence.create(input);
},
updateSequence(_, {id, input}) {
return models.Sequence.update(input, {where: {id: id}, returning: true}).then((sequence) => {
return sequence[1][0].dataValues;
});
},
Answer
According to Sequelize Documentation:
- The
create
method returns aPromise<Model>
, in your case it returns something that matchestype Sequence
. - The
update
method instead, returns aPromise<Array<number, number>>
and that does not matchtype Sequence
.
The promise returns an array with one or two elements. The first element is always the number of affected rows, while the second element is the actual affected rows (only supported in postgres with options.returning true.)
So either you change the return type of updateSequence
into something that matches the return type from models.Sequence.update
, OR, you have to build an object that matches type Sequence
after the update
.
Hope it helps.
Related Questions
- → I can't do a foreign key, constraint error
- → How to implement DbDongle::convertTimestamps as workaround of invalid timestamps with MySql strict
- → MySQL error "Foreign key constraint is incorrectly formed"
- → Eloquent Multitable query
- → "Laravel 5.1" add user and project with userId
- → Database backup with custom code in laravel 5 and get the data upto 10 rows from per table in database
- → Laravel 5.1 QueryException when trying to delete a project
- → Using Array in '->where()' for Laravel Query Building
- → Chaining "Count of Columns" of a Method to Single Query Builder
- → Laravel Eloquent Joining Strange query
- → convert time using mysql laravel 5
- → How to update a column after an expiration date in MySQL?
- → Foreign key constraint fails on existing key