Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 35 forks source link

Use virtualAttributes for GraphQL schema #44

Closed timbaas closed 6 years ago

timbaas commented 6 years ago

Hi there,

I can't find any way to get around the following.

I've got a Model ("Exchange") with a db column ("name") and a virtual attribute ("marketCount"):

class Exchange extends Model {
  static get tableName () {
    return 'exchanges';
  }

  static get virtualAttributes() {
    return ['marketCount'];
  }

  static get jsonSchema () {
    return {
      type: 'object',
      required: ['name'],
      properties: {
        id: {type: 'integer'},
        name: {type: 'string'},
        // marketCount: {type: 'number' }
      }
    };
  }

  marketCount () {
    return 1;
  }
}

When defining "marketCount" in jsonSchema properties it tries to select the column from the db, which of course doesn't exist and when leaving it out it isn't available in the GraphQL Schema, so the query fails.

I find it hard to believe this wouldn't be possible but didn't find the answer either.

Anything I don't know of?

nasushkov commented 6 years ago

Hi @timbaas . This library utilizes Objection Model JSON schema to generate GraphQL fields. On the other hand, as you mentioned, if the field is a part of the schema it is supposed to exist in the db. So, virtual attributes are not supported yet, but I think it's worth implementing in the future.

timhuff commented 6 years ago

I just submitted a pull request which should facilitate this need: https://github.com/Vincit/objection-graphql/pull/55

DaKaZ commented 5 years ago

@timhuff why was PR #55 reverted? Is there another way to handle virtual attributes?

DaKaZ commented 5 years ago

Ignore... I found PR #57