Skip to content

Commit

Permalink
Add Apollo Server test back
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Oct 18, 2023
1 parent f59bbae commit 36199b6
Showing 1 changed file with 95 additions and 0 deletions.
95 changes: 95 additions & 0 deletions packages/graphql-modules/tests/di-providers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
testkit,
} from '../src';
import { ExecutionContext } from '../src/di';
import { ApolloServer } from '@apollo/server';

const Test = new InjectionToken<string>('test');

Expand Down Expand Up @@ -541,6 +542,100 @@ test('Operation scoped provider should be created once per GraphQL Operation', a
expect(loadSpy).toHaveBeenCalledWith(1);
});

test('Operation scoped provider should be created once per GraphQL Operation (Apollo Server)', async () => {
const constructorSpy = jest.fn();
const loadSpy = jest.fn();

@Injectable({
scope: Scope.Operation,
})
class Dataloader {
constructor(@Inject(CONTEXT) context: GraphQLModulesGlobalContext) {
constructorSpy(context);
}

load(id: number) {
loadSpy(id);
return {
id,
title: 'Sample Title',
};
}
}

const postsModule = createModule({
id: 'posts',
providers: [Dataloader],
typeDefs: gql`
type Post {
id: Int!
title: String!
}
type Query {
post(id: Int!): Post!
}
`,
resolvers: {
Query: {
post(
_parent: {},
args: { id: number },
{ injector }: GraphQLModulesModuleContext
) {
return injector.get(Dataloader).load(args.id);
},
},
},
});

const app = createApplication({
modules: [postsModule],
});

const apolloServer = new ApolloServer({
gateway: app.createApolloGateway(),
});

const query = gql`
{
foo: post(id: 1) {
id
title
}
bar: post(id: 1) {
id
title
}
}
`;

const result = await apolloServer.executeOperation({
query,
});

if (result.body.kind === 'incremental') {
throw new Error('Expected non-incremental response');
}

// Should resolve data correctly
expect(result.body.singleResult.errors).toBeUndefined();
expect(result.body.singleResult.data).toEqual({
foo: {
id: 1,
title: 'Sample Title',
},
bar: {
id: 1,
title: 'Sample Title',
},
});

expect(constructorSpy).toHaveBeenCalledTimes(1);

expect(loadSpy).toHaveBeenCalledTimes(2);
expect(loadSpy).toHaveBeenCalledWith(1);
});

test('Singleton scoped provider should be created once', async () => {
const constructorSpy = jest.fn();

Expand Down

0 comments on commit 36199b6

Please sign in to comment.