【问题标题】:Use virtualAttributes with jsonSchema in objection反对使用带有 jsonSchema 的 virtualAttributes
【发布时间】:2020-08-27 20:18:45
【问题描述】:

我想要一个附加列,其中包含来自另一列的修改值: 现在我有下一个这样的代码,但它不起作用:

const { Model } = require("objection");  
    class TestModel extends Model {
      static get tableName() {
        return "testTable";
      }
    
    
      static get virtualAttributes() {
        return ["someNewField"];
      }
    
      someNewField() {
        return this.someField+'testString';
      }
    
      static get jsonSchema() {
        return {
          type: "object",
          properties: {
            someField:{ type: "string" },
          },
        };
      }
    }
    
    module.exports = {
      TestModel,
    };

我遇到了类似的错误

"message": "Cannot query field \"someNewField\" on type \"testTable\".",

我的 grapql 查询:

{TestModels{
  someNewField,
}

【问题讨论】:

  • 请同时添加引发错误的查询
  • @MikaelLepistö 我添加了这个

标签: graphql graphql-js objection.js


【解决方案1】:

从数据库中获取数据后,在 javascript 端创建虚拟属性。所以DB中没有生成someNewField这样的列。

所以现在您正在尝试创建一个查询,试图从数据库中查找根本不存在的数据。

没有通用的解决方法如何使其工作。如果你能解释你的用例你想在这里实现什么,我可能会建议在这种特定情况下做些什么。

【讨论】:

  • 我使用 objection 和 objection-graphql。我正在寻找一种允许修改使用graphql查询获取客户端的日期的方法,我不了解如何在服务器端进行更改。例如,我得到 100 的值,我想将其更改为 100 美元
  • 不知道graphql + objection是否支持。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 1970-01-01
相关资源
最近更新 更多