Skip to content

Commit 87dbde9

Browse files
authored
feat: Graphql layer (#42)
1 parent d03eef2 commit 87dbde9

File tree

9 files changed

+154
-2
lines changed

9 files changed

+154
-2
lines changed

api/package-lock.json

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"kafkajs": "^2.2.4",
6363
"koa": "^2.14.2",
6464
"koa-bodyparser": "^4.4.0",
65+
"koa-graphql": "^0.12.0",
6566
"koa-logger": "^3.2.1",
6667
"koa-mount": "^4.0.0",
6768
"koa-static": "^5.0.0",

api/src/api.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ import updateHandler from '@pokemon/handlers/update.handler';
1717
import healthcheckHandler from '@pokemon/handlers/healthcheck.handler';
1818
import { setupSequelize } from '@pokemon/utils/db';
1919
import { instrumentRoute } from '@pokemon/middlewares/instrumentation';
20+
import { graphqlHTTP } from 'koa-graphql';
21+
import schema from './schema';
22+
import resolvers from './graphql/resolvers';
2023

2124
const { APP_PORT = 8081 } = process.env;
2225

@@ -53,6 +56,7 @@ async function startApp() {
5356

5457
ui.use(serve(resolve(__dirname, './ui')));
5558
app.use(mount('/', ui));
59+
app.use(mount('/graphql', graphqlHTTP({ schema, rootValue: resolvers, graphiql: true })));
5660

5761
console.log(`Starting server on port ${APP_PORT}`);
5862
app.listen(APP_PORT);

api/src/graphql/create.resolver.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { getPokemonRepository, Pokemon } from '@pokemon/repositories';
2+
3+
const create = async (raw: Pokemon): Promise<Pokemon> => {
4+
const repository = getPokemonRepository();
5+
6+
return repository.create(new Pokemon(raw));
7+
};
8+
9+
export default create;

api/src/graphql/get.resolver.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { getPokemonRepository, Pokemon } from '@pokemon/repositories';
2+
import { SearchOptions } from '../repositories/pokemon.repository';
3+
4+
type PokemonList = {
5+
items: Pokemon[];
6+
totalCount: number;
7+
};
8+
9+
const get = async (query: SearchOptions): Promise<PokemonList> => {
10+
const repository = getPokemonRepository();
11+
12+
const [items, totalCount] = await Promise.all([repository.findMany(query), repository.count()]);
13+
14+
return {
15+
items,
16+
totalCount,
17+
};
18+
};
19+
20+
export default get;

api/src/graphql/import.resolver.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import PokeAPIService from '@pokemon/services/pokeApi.service';
2+
import PokemonSyncronizer from '@pokemon/services/pokemonSyncronizer.service';
3+
4+
const pokeApiService = new PokeAPIService();
5+
const pokemonSyncronizer = PokemonSyncronizer(pokeApiService);
6+
7+
const importPokemon = async ({ id = 0 }) => {
8+
await pokemonSyncronizer.queue({ id });
9+
10+
return { id };
11+
};
12+
13+
export default importPokemon;

api/src/graphql/resolvers.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import createPokemon from './create.resolver';
2+
import getPokemonList from './get.resolver';
3+
import importPokemon from './import.resolver';
4+
5+
export default {
6+
getPokemonList,
7+
createPokemon,
8+
importPokemon,
9+
};

api/src/repositories/pokemon.repository.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
import { PokemonModel } from './pokemon.sequelize.repository';
2-
31
export class Pokemon {
42
public id?: number;
53
public name: string;

api/src/schema.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { buildSchema } from 'graphql';
2+
3+
const schema = buildSchema(`
4+
type Pokemon {
5+
id: Int
6+
name: String!
7+
type: String!
8+
isFeatured: Boolean!
9+
imageUrl: String
10+
}
11+
12+
type PokemonList {
13+
items: [Pokemon]
14+
totalCount: Int
15+
}
16+
17+
type ImportPokemon {
18+
id: Int!
19+
}
20+
21+
type Query {
22+
getPokemonList(where: String, skip: Int, take: Int): PokemonList
23+
}
24+
25+
type Mutation {
26+
createPokemon(name: String!, type: String!, isFeatured: Boolean!, imageUrl: String): Pokemon!
27+
importPokemon(id: Int!): ImportPokemon!
28+
}
29+
`);
30+
31+
export default schema;

0 commit comments

Comments
 (0)