Skip to content

Commit

Permalink
Also works
Browse files Browse the repository at this point in the history
  • Loading branch information
kamilkisiela committed Feb 15, 2023
1 parent 53672f9 commit 78bbc97
Showing 1 changed file with 76 additions and 0 deletions.
76 changes: 76 additions & 0 deletions packages/graphql-modules/tests/di-providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1463,3 +1463,79 @@ test('accessing global singleton provider (app) from global singleton provider (
expect(result.errors).not.toBeDefined();
expect(result.data?.uppercase).toBe('GIL');
});

test('accessing global singleton provider (app) from global singleton provider (module) that depends on non-global singleton (app)', async () => {
@Injectable({
scope: Scope.Singleton,
})
class TextTransforms {
uppercase(text: string) {
return text.toUpperCase();
}
}

@Injectable({
scope: Scope.Singleton,
global: true,
})
class AppData {
constructor(private textTransforms: TextTransforms) {}

public async uppercase(text: string) {
return this.textTransforms.uppercase(text);
}
}

@Injectable({
scope: Scope.Singleton,
global: true,
})
class ModuleData {
constructor(private appData: AppData) {}

public async uppercase(text: string) {
return this.appData.uppercase(text);
}
}

const fooModule = createModule({
id: 'foo',
providers: [ModuleData],
typeDefs: gql`
type Query {
uppercase(text: String!): String!
}
`,
resolvers: {
Query: {
uppercase(
_: {},
{ text }: { text: string },
{ injector }: GraphQLModules.ModuleContext
) {
return injector.get(ModuleData).uppercase(text);
},
},
},
});

const app = createApplication({
modules: [fooModule],
providers: [AppData, TextTransforms],
});

const result = await testkit.execute(app, {
contextValue: {},
variableValues: {
text: 'gil',
},
document: gql`
query up($text: String!) {
uppercase(text: $text)
}
`,
});

expect(result.errors).not.toBeDefined();
expect(result.data?.uppercase).toBe('GIL');
});

0 comments on commit 78bbc97

Please sign in to comment.