Ad

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;
    });
},
Ad

Answer

According to Sequelize Documentation:

  • The create method returns a Promise<Model>, in your case it returns something that matches type Sequence.
  • The update method instead, returns a Promise<Array<number, number>> and that does not match type 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.

Ad
source: stackoverflow.com
Ad