Urigo / graphql-modules

Enterprise Grade Tooling For Your GraphQL Server
https://graphql-modules.com/
MIT License
1.31k stars 114 forks source link

Module Configuration not working #379

Closed abhijeet1403 closed 5 years ago

abhijeet1403 commented 5 years ago

(@Inject(ModuleConfig(MyModule) method in the Provider constructor is throwing an error.

Reproduction :

Just followed the steps mentioned at https://graphql-modules.com/docs/introduction/configuration

Issue: Either the documentation is incomplete or there's an issue with module configuration

my-provider.ts

@Injectable()
export class MyProvider {
    constructor(@Inject(ModuleConfig(MyModule)) private config: MyModuleConfig) {
    }
}

my-module.ts

export const MyModule = new GraphQLModule<MyModuleConfig>({
    providers: () => [
        MyProvider
    ]
});

app.ts

export const AppModule = new GraphQLModule({
  imports: [
    UserModule,
    BlogModule,
    MyModule.forRoot({
      secretKey: '123',
      remoteEndpoint: 'http://my-other-service.com',
    })
  ],
});

Error:

TypeError: Cannot read property 'name' of undefined
    at Object.exports.ModuleConfig (/home/walkin/Projects/Testbed/gqmodules/graphql-modules/examples/basic-with-dependency-injection/node_modules/@graphql-modules/core/src/graphql-module.ts:98:75)
    at Object.<anonymous> (/home/walkin/Projects/Testbed/gqmodules/graphql-modules/examples/basic-with-dependency-injection/src/modules/testModule/my-provider.ts:8:25)
    at Module._compile (module.js:652:30)
    at Module.m._compile (/home/walkin/Projects/Testbed/gqmodules/graphql-modules/examples/basic-with-dependency-injection/node_modules/ts-node/src/index.ts:439:23)
    at Module._extensions..js (module.js:663:10)
    at Object.require.extensions.(anonymous function) [as .ts] (/home/walkin/Projects/Testbed/gqmodules/graphql-modules/examples/basic-with-dependency-injection/node_modules/ts-node/src/index.ts:442:12)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Module.require (module.js:596:17)
error Command failed with exit code 1.
abhijeet1403 commented 5 years ago

Also if anyone can provide an ES6 version of the same documentation it'll be very helpful.

ardatan commented 5 years ago

Because you have a circular imports, so MyModule is undefined in ModuleConfig.

I can recommend you to use module names

@Injectable()
export class MyProvider {
    constructor(@Inject(ModuleConfig('mymodule')) private config: MyModuleConfig) {
    }
}

export const MyModule = new GraphQLModule<MyModuleConfig>({
    name: 'my-module',
    providers: () => [
        MyProvider
    ]
});

I think we need to have a warning and documentation for this. I will keep this open until documentation and better error handling done.

ardatan commented 5 years ago

I added the usage like below to ModuleConfig;

@Inject(ModuleConfig(module => MyModule))

You can try it with canary versions until we release a new version; @graphql-modules/core -> 0.7.0-alpha.c85943e2

ardatan commented 5 years ago

It is available in 0.7.0.