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 authored and enisdenjo committed Nov 6, 2023
1 parent a92e7c3 commit 1af4b22
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 1af4b22

Please sign in to comment.