Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createSchema doesn't call constructor method #29

Closed
snaquaye opened this issue May 24, 2018 · 5 comments
Closed

createSchema doesn't call constructor method #29

snaquaye opened this issue May 24, 2018 · 5 comments
Assignees
Labels
enhancement New feature or request

Comments

@snaquaye
Copy link

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
Copy link
Contributor

capaj commented May 24, 2018

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
Copy link

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
Copy link
Author

Can you kindly give an example using services or DI?

@pie6k
Copy link
Collaborator

pie6k commented May 24, 2018

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

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

@pie6k pie6k added the enhancement New feature or request label May 24, 2018
@pie6k pie6k self-assigned this May 24, 2018
@pie6k
Copy link
Collaborator

pie6k commented May 25, 2018

It's now fixed and possible with #39 - I'll create example of DI soon.

@pie6k pie6k closed this as completed May 25, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants