Skip to content

Commit

Permalink
Avoid non-configured modules to prevent conflict
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Dec 4, 2018
1 parent a568383 commit 2b36c20
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
8 changes: 7 additions & 1 deletion packages/core/src/graphql-module.ts
Expand Up @@ -655,7 +655,13 @@ export class GraphQLModule<Config = any, Request = any, Context = any> {
throw new DependencyModuleUndefinedError(module.name);
}
if (typeof subModule !== 'string') {
visitModule(subModule);
if (subModule._options.configRequired) {
if (subModule.config) {
visitModule(subModule);
}
} else {
visitModule(subModule);
}
}
}
}
Expand Down
42 changes: 42 additions & 0 deletions packages/core/tests/graphql-module.spec.ts
Expand Up @@ -945,4 +945,46 @@ describe('GraphQLModule', () => {
expect(result.errors).toBeFalsy();
expect(result.data['foo']).toBeTruthy();
});
it('should avoid getting non-configured module', async () => {
const FOO = Symbol('FOO');
const moduleA = new GraphQLModule<{ foo: string }>({
providers: ({config}) => [
{
provide: FOO,
useValue: config.foo,
},
],
configRequired: true,
});
const moduleB = new GraphQLModule({
typeDefs: gql`
type Query {
foo: String
}
`,
resolvers: {
Query: {
foo: (_, __, { injector }) => injector.get(FOO),
},
},
imports: [
moduleA,
],
});
const { schema, context } = new GraphQLModule({
imports: [
moduleB,
moduleA.forRoot({
foo: 'FOO',
}),
],
});
const result = await execute({
schema,
document: gql`query { foo }`,
contextValue: await context({ req: {} }),
});
expect(result.errors).toBeFalsy();
expect(result.data['foo']).toBe('FOO');
});
});

0 comments on commit 2b36c20

Please sign in to comment.