prismake / typegql

Create GraphQL schema with TypeScript classes.
https://prismake.github.io/typegql/
MIT License
423 stars 21 forks source link

Field decorator does not support getters #26

Closed capaj closed 6 years ago

capaj commented 6 years ago

I've been using typegql with objection.js for a little while now and I have couple of getters in my codebase. Would it be possible to support them? Currently typegql is throwing when I try to put @Field on a getter.

pie6k commented 6 years ago

Could you paste some example code that is throwing?

capaj commented 6 years ago
@ObjectType({ description: 'Simple product object type' })
class Product {
  @Field()
  get throws(): number {
    return 10
  }
}

the error is:

Error: Expected undefined to be a GraphQL nullable type.
    at invariant (/home/capaj/git_projects/tests/type-gql/node_modules/graphql/jsutils/invariant.js:19:11)
    at assertNullableType (/home/capaj/git_projects/tests/type-gql/node_modules/graphql/type/definition.js:327:51)
    at new GraphQLNonNull (/home/capaj/git_projects/tests/type-gql/node_modules/graphql/type/definition.js:294:19)
    at enhanceType (/home/capaj/git_projects/tests/type-gql/node_modules/typegql/lib/index.js:444:21)
    at /home/capaj/git_projects/tests/type-gql/node_modules/typegql/lib/index.js:463:25
    at Array.forEach (<anonymous>)
    at convertArgsArrayToArgsMap (/home/capaj/git_projects/tests/type-gql/node_modules/typegql/lib/index.js:456:14)
    at compileFieldArgs (/home/capaj/git_projects/tests/type-gql/node_modules/typegql/lib/index.js:482:12)
    at compileFieldConfig$$1 (/home/capaj/git_projects/tests/type-gql/node_modules/typegql/lib/index.js:624:16)
    at /home/capaj/git_projects/tests/type-gql/node_modules/typegql/lib/index.js:658:39

I tried to specify the type using {type: Number} but that does not help.

pie6k commented 6 years ago

Ok I think I've found something. It's treating it as function and trying to find arguments list, while it's getter. I'll hopefully fix it today.

pie6k commented 6 years ago

I've added support for getter based fields together with needed test: https://github.com/prismake/typegql/commit/85d017e98d0c8d216e70dcff484b05d4acfb301f

Try to update to 0.5.2 and let me know if it's working now. If not - I'll re-open.

capaj commented 6 years ago

@pie6k awesome :tada: so fast :tornado: As soon as NPM let's me install that version I'll try it out. Been having issues whole day.

capaj commented 6 years ago

works :+1: