A Apollo Server boilerplate with TypeScript support. Including development setup to ensure clean and working code before pushing.
The following development dependencies are included:
First clone the repository and install dependencies.
$ git clone https://github.com/markuswind/apollo-server-boilerplate
$ npm ci
$ npm run start
Because we want to have type safe resolvers, it's the easiest to create new resolvers in order described below.
When adding new resolvers you should start with adding the typeDefs
in your (new) resolver.ts
file like following:
export const typeDefs = grapql`
type Example {
id: Int!
}
type Query {
example(id: Int!): Example!
}
`;
The next step is updating the generated types like following:
$ npm run generate:types
After this you could create your update your (new) provider.ts
file like following:
import { QueryExampleArgs } from './generated/graphql';
export class ExampleAPI extends RestDataSource {
// ... constructor
public async getExample(args: QueryExampleArgs) {
// ... use typed args to return result
}
}
NOTE: When adding a new provider you'll have to update the context in the index.ts
file.
Finally you could create your typed resolvers in the resolvers.ts
file like following:
import { IResolvers } from './generated/grapql';
export const resolvers: IResolvers = {
Query: {
example: (_, args, ctx) => ctx.dataSources.ExampleAPI.getExample(args)
}
};