简体   繁体   English

在更新突变 GraphQL 和 Sequelize 时返回 null

[英]Return null on update mutation GraphQL and Sequelize

I would like to know why my response is "null" when I execute my mutation please.我想知道为什么当我执行我的突变时我的响应是“null”。

My query works : the state is_active become true or false in the database SQL when I execute the request.我的查询有效:当我执行请求时,数据库 SQL 中的状态 is_active 变为 true 或 false。

When I create, I have a correct response, but not on the update.创建时,我有正确的响应,但没有更新。

Graphql request : Graphql 请求:

mutation {
  updateSequence(id: 4, , input: {is_active: true}) {
  is_active
  }
}

The response :响应 :

{
  "data": {
    "updateSequence": {
      "is_active": null
    }
  }
}

resolvers.js解析器.js

Mutation    : {
    createSequence(_, {input}) {
        return models.Sequence.create(input);
    },
    updateSequence(_, {id, input}) {
        return models.Sequence.update(input, {where: {id: id}});
    }
}

schema.js 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)已解决:将 MySQL 迁移到 Postegres 以使用返回选项(仅限 Postegres)

I migrate my database to use the returning option from Sequelize.我迁移我的数据库以使用 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;
    });
},

According to Sequelize Documentation :根据续集文档

  • The create method returns a Promise<Model> , in your case it returns something that matches type Sequence . create方法返回一个Promise<Model> ,在您的情况下,它返回与type Sequence匹配的内容。
  • The update method instead, returns a Promise<Array<number, number>> and that does not match type Sequence .相反, update方法返回一个Promise<Array<number, number>>并且不匹配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.)第一个元素始终是受影响的行数,而第二个元素是实际受影响的行(仅在带有 options.returning true 的 postgres 中支持。)

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 .因此,要么将updateSequence的返回类型更改为与models.Sequence.update的返回类型匹配的东西,要么在update之后构建一个与type Sequence匹配的对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM