prismake / typegql

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

createSchema doesn't call constructor method #29

Closed snaquaye closed 6 years ago

snaquaye commented 6 years ago

Constructor function is not called

// Import statements

@Schema()
class RegistrationSchema {

  constructor (private registrationService: RegistrationService) { }

  @Query()
  hello(me: string): string {
    return 'hello world!';
  }

  @Mutation({ type: RegistrationType })
  async registerUser(@Arg() newRegistrantData: RegistrationInputType): Promise<RegistrationType> {
    const errors = await validate(newRegistrantData);

    if (errors.length) {
      throw SevenBoom.badRequest('Data sent to the sever failed validation', errors);
    }

    try {
      await this.registrationService.registerUser(newRegistrantData);
      return newRegistrantData;
    } catch (error) {
      throw SevenBoom.badImplementation('Internal Server Error', error);
    }
  }
}

const compiledSchema = compileSchema(RegistrationSchema);

export default compiledSchema;
capaj commented 6 years ago

Why would you expect constructor to be called? If you need it called, instantiate your service using new keyword. It would be bad if Schema decorator had such side effects such as instantiating the class.

MichalLytek commented 6 years ago

It might be related to #16 as you need dependency injection. If you need this, you might need to find a library that supports it: https://github.com/chentsulin/awesome-graphql#lib-ts

snaquaye commented 6 years ago

Can you kindly give an example using services or DI?

pie6k commented 6 years ago

Thats good point! I'll try to add it tomorrow!

I think it'd be useful for other entities like ObjectType too.

pie6k commented 6 years ago

It's now fixed and possible with https://github.com/prismake/typegql/pull/39 - I'll create example of DI soon.