Getting started
Prerequisites
To use Inversify GraphQL, you need to have an Inversify HTTP server set up. Please refer to the HTTP Getting Started guide for instructions on how to set up an HTTP server.
Install dependencies
Install the required packages:
- npm
- pnpm
- yarn
npm install @inversifyjs/apollo-express @inversifyjs/apollo-core @apollo/server graphql
pnpm install @inversifyjs/apollo-express @inversifyjs/apollo-core @apollo/server graphql
yarn add @inversifyjs/apollo-express @inversifyjs/apollo-core @apollo/server graphql
Create a GraphQL Server
You can create a GraphQL server using ApolloExpressServerContainerModule.
@injectable()
class QueryResolvers {
public hello(): string {
return 'Hello World!';
}
}
@injectable()
class AppResolvers {
// eslint-disable-next-line @typescript-eslint/naming-convention
public readonly Query: QueryResolvers;
constructor(@inject(QueryResolvers) queryResolvers: QueryResolvers) {
this.Query = queryResolvers;
}
}
const typeDefs: string = `
type Query {
hello: String
}
`;
const container: Container = new Container();
await container.load(
ApolloExpressServerContainerModule.graphServerFromOptions(
{
controllerOptions: {
path: '/graphql',
},
getContext: async () => ({}),
},
{
resolverServiceIdentifier: AppResolvers,
typeDefs,
},
),
);
container.bind(QueryResolvers).toSelf();
container.bind(AppResolvers).toSelf();
const adapter: InversifyExpressHttpAdapter = new InversifyExpressHttpAdapter(
container,
);
await adapter.build();
const inversifyApolloProvider: InversifyApolloProvider<http.Server> =
await container.getAsync(inversifyApolloProviderServiceIdentifier);
await new Promise<void>((resolve: () => void) => {
inversifyApolloProvider.server.listen(3000, () => {
resolve();
});
});
console.log(`Server is running on http://localhost:3000`);