-
Describe the Bug When using scoped containers with ApolloServer, TypeGraphQL, and TypeORM, no resolver works. Everything is great for global containers, just not scoped. The error I get back is
To Reproduce /*...other imports*/
import { ApolloServer } from 'apollo-server-express';
import { buildSchema, ResolverData } from 'type-graphql';
import { getConnectionManager, useContainer } from 'typeorm';
import { Container as TypeOrmContainer } from 'typeorm-typedi-extensions';
import Container from 'typedi';
useContainer(TypeOrmContainer);
async function main() {
// ============================================================
// SETUP
// ============================================================
const app = Express();
app.set('trust proxy', 1);
// ============================================================
// DB
// ============================================================
try {
const dbConnection = await DB.connect();
console.log('TypeORM Connected:', dbConnection.isConnected);
} catch (e) {
console.warn('TypeORM Connection Error');
console.log(e);
process.exit(1);
}
// ============================================================
// APOLLO SERVER
// ============================================================
const apolloServer = new ApolloServer({
schema: await buildSchema({
resolvers: [path.join(process.cwd(), 'build/modules/**/resolver*.js')],
container: ({ context }: ResolverData<CTX>) => context.scopedContainer, //GRAB SCOPE CONTAINER HERE
validate: { validationError: { target: false, value: false } },
}),
context: ({ req, res }): CTX => {
const requestId: string = cuid();
const scopedContainer = Container.of(requestId); //CREATE SCOPE CONTAINER HERE
const context: CTX = {
requestId,
req,
res,
session: req.session,
scopedContainer,
};
scopedContainer.set<CTX>('CTX', context);
return context;
},
playground: { settings: { 'request.credentials': 'include' } },
});
apolloServer.applyMiddleware({
app,
cors: { origin: ENV.FRONTEND.URL, credentials: true },
});
// ============================================================
// START SERVER
// ============================================================
app.listen(ENV.PORT, () => console.log(`Server started on http://localhost:${ENV.PORT}/graphql`));
} Everything builds as expected, but try to query any resolver class that has @InjectRepository() or anything TypeORM related, and GQL returns the above error message. I have a suspicion this is a "typeorm-typedi-extensions" issue with the type-di changes from 0.8 to 0.10, but if there's any help you could provide on fixes that would be amazing. Full repo HERE Expected Behavior Connect to DB and query repository as intended. Everything works without scopes, and scoped containers work great without typeorm, just no go on the combo application. Environment (please complete the following information):
|
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
Moving to discussion as it's not TypeGraphQL issue. |
Beta Was this translation helpful? Give feedback.
-
TypeGraphQL has nothing to do with the DI containers. It just calls
Ask on their repo how to configure TypeDI, TypeORM and scoped containers. |
Beta Was this translation helpful? Give feedback.
-
FYI I Found the error. Its in the way typeorm-typedi-extensions sets up the ConnectionManager in TypeDI. Deep dive into results here: typestack/typeorm-typedi-extensions#51 |
Beta Was this translation helpful? Give feedback.
FYI I Found the error. Its in the way typeorm-typedi-extensions sets up the ConnectionManager in TypeDI.
Deep dive into results here: typestack/typeorm-typedi-extensions#51