prismake / typegql

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

Add Interface support #55

Open jdolle opened 6 years ago

jdolle commented 6 years ago

Closes https://github.com/prismake/typegql/issues/11

Hey there. First off, great package. I'm really enjoying it. I was hoping this PR would be a great jumping off point for interfaces.

Using extend very nicely adds functionality to classes. Therefore, it seemed natural to allow an InterfaceType to be implemented on an ObjectType via extend.

As for allowing Types to implement multiple interfaces, I was thinking this new InterfaceType class could be leveraged somehow via new helper like the following:

import { InterfaceType, ObjectType, Field } from 'typegql';

@InterfaceType()
class Entity {
  @Field() x: number;
  @Field() y: number;
}

@InterfaceType()
class Movable {
  @Field() dx: number;
  @Field() dy: number;
}

@InterfaceType()
class Vehicle {
  @Field() vin: string;
}

@Implements([Entity, Movable, Vehicle])
@ObjectType()
class Car {
  @Field({ type: () => Person })
  owner() {
    return db.findPersonByCarId(this.id);
  }
}

If you are open to it, I'd be happy to "implement @Implements" in a separate PR.

pie6k commented 6 years ago

Seems good. I've just got married and we're chillouting in Italy so I'll take a look next week :)

codecov[bot] commented 6 years ago

Codecov Report

Merging #55 into master will decrease coverage by 0.11%. The diff coverage is 80.76%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #55      +/-   ##
==========================================
- Coverage   87.44%   87.33%   -0.12%     
==========================================
  Files          61       66       +5     
  Lines        1107     1192      +85     
  Branches      194      207      +13     
==========================================
+ Hits          968     1041      +73     
- Misses        138      150      +12     
  Partials        1        1
Impacted Files Coverage Δ
src/domains/objectType/index.ts 76.19% <ø> (-1.09%) :arrow_down:
src/domains/index.ts 100% <100%> (ø) :arrow_up:
src/domains/objectType/registry.ts 100% <100%> (ø) :arrow_up:
src/domains/interfaceType/compiler/index.ts 100% <100%> (ø)
src/domains/interfaceType/registry.ts 100% <100%> (ø)
src/domains/interfaceType/index.ts 76.19% <70.58%> (ø)
src/domains/objectType/compiler/objectType.ts 86.48% <72.72%> (+3.15%) :arrow_up:
src/domains/interfaceType/error.ts 89.47% <75%> (ø)
...rc/domains/interfaceType/compiler/interfaceType.ts 83.33% <83.33%> (ø)
... and 7 more

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update 1827de8...fbebdb2. Read the comment docs.