首页 > 解决方案 > 反对使用带有 jsonSchema 的 virtualAttributes

问题描述

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

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,
}

标签: graphqlgraphql-jsobjection.js

解决方案


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

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

没有通用的解决方法如何使这项工作。如果您可以在这里解释您的用例,您可能会建议在该特定情况下做些什么。


推荐阅读