Using with typedi 0.10.0 and custom container #1054
-
When using typedi 0.8.0 everything works correctly.
Here is my simplified code: import 'reflect-metadata';
import { Container, Inject } from 'typedi';
import { buildSchema, ObjectType, Field, Query, Resolver } from 'type-graphql';
import { ApolloServer } from 'apollo-server';
// --- Injected controlled ---
class ApiController {}
abstract class Controller {
@Inject('ApiController')
protected readonly api!: ApiController;
constructor(apiController: ApiController) {
this.api = apiController;
}
}
// --- Album resolver ---
@ObjectType()
export class Album {
@Field({ nullable: false })
public id!: number;
}
@Resolver(() => Album)
export class AlbumController extends Controller {
@Query(() => Album, { name: 'album' })
public album(): Album {
return {
id: 666,
};
}
}
async function startServer(): Promise<void> {
Container.set('ApiController', new ApiController());
const schema = await buildSchema({
resolvers: [AlbumController],
emitSchemaFile: true,
// Used to inject `ApiController` into controller instances.
container: Container,
// Avoids warning when class-validator is not used in the code.
validate: false,
// All fields are nullable by default per webapi documentation.
nullableByDefault: true,
});
const server = new ApolloServer({
schema,
debug: true,
playground: true,
});
server.listen().then(({ url }) => {
console.info(`🚀 Server ready at ${url}`);
});
}
startServer(); Repository for easy testing: https://github.com/rchl/typedi-version |
Beta Was this translation helpful? Give feedback.
Answered by
MichalLytek
Sep 30, 2021
Replies: 1 comment 1 reply
-
Resolvers are also classes retrieved from container, so you need to put |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
rchl
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Resolvers are also classes retrieved from container, so you need to put
@Service()
above them.