Skip to content

Commit

Permalink
Fix injection to functions
Browse files Browse the repository at this point in the history
Signed-off-by: Arda TANRIKULU <ardatanrikulu@gmail.com>
  • Loading branch information
ardatan committed Nov 28, 2018
1 parent 58a9d20 commit fcbf2a8
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/di/injector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ export class Injector {
}
} else if (this._factoryMap.has(serviceIdentifier)) {
const factory = this._factoryMap.get(serviceIdentifier);
const instance = factory(this);
const instance = this.call(factory, this);
if (this.scopeSet.has(serviceIdentifier)) {
this._instanceMap.set(serviceIdentifier, instance);
}
Expand Down
42 changes: 41 additions & 1 deletion packages/core/tests/graphql-module.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,15 @@ describe('GraphQLModule', () => {
@Injectable()
class ProviderA {
doSomething() {
return 'Test';
return 'Test1';
}
}

// B
@Injectable()
class ProviderB {
doSomethingElse() {
return 'Test2';
}
}

Expand Down Expand Up @@ -177,6 +185,38 @@ describe('GraphQLModule', () => {
expect(app.schema).toBeDefined();
});

it('should inject dependencies to factory functions using Inject', async () => {
const { schema, context } = new GraphQLModule({
typeDefs: gql`
type Query {
something: String
somethingElse: String
}
`,
providers: [ProviderA, ProviderB],
resolvers: Inject(ProviderA, ProviderB)((providerA, providerB) => ({
Query: {
something: () => providerA.doSomething(),
somethingElse: () => providerB.doSomethingElse(),
},
})),
});
const contextValue = await context({ req: {} });
const result = await execute({
schema,
document: gql`
query {
something
somethingElse
}
`,
contextValue,
});
expect(result.errors).toBeFalsy();
expect(result.data.something).toBe('Test1');
expect(result.data.somethingElse).toBe('Test2');
});

describe('Schema merging', () => {
it('should merge types and directives correctly', async () => {
const m1 = new GraphQLModule({
Expand Down

0 comments on commit fcbf2a8

Please sign in to comment.