Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Graphql layer #42

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions api/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"kafkajs": "^2.2.4",
"koa": "^2.14.2",
"koa-bodyparser": "^4.4.0",
"koa-graphql": "^0.12.0",
"koa-logger": "^3.2.1",
"koa-mount": "^4.0.0",
"koa-static": "^5.0.0",
Expand Down
4 changes: 4 additions & 0 deletions api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ import updateHandler from '@pokemon/handlers/update.handler';
import healthcheckHandler from '@pokemon/handlers/healthcheck.handler';
import { setupSequelize } from '@pokemon/utils/db';
import { instrumentRoute } from '@pokemon/middlewares/instrumentation';
import { graphqlHTTP } from 'koa-graphql';
import schema from './schema';
import resolvers from './graphql/resolvers';

const { APP_PORT = 8081 } = process.env;

Expand Down Expand Up @@ -53,6 +56,7 @@ async function startApp() {

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

console.log(`Starting server on port ${APP_PORT}`);
app.listen(APP_PORT);
Expand Down
9 changes: 9 additions & 0 deletions api/src/graphql/create.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getPokemonRepository, Pokemon } from '@pokemon/repositories';

const create = async (raw: Pokemon): Promise<Pokemon> => {
const repository = getPokemonRepository();

return repository.create(new Pokemon(raw));
};

export default create;
20 changes: 20 additions & 0 deletions api/src/graphql/get.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { getPokemonRepository, Pokemon } from '@pokemon/repositories';
import { SearchOptions } from '../repositories/pokemon.repository';

type PokemonList = {
items: Pokemon[];
totalCount: number;
};

const get = async (query: SearchOptions): Promise<PokemonList> => {
const repository = getPokemonRepository();

const [items, totalCount] = await Promise.all([repository.findMany(query), repository.count()]);

return {
items,
totalCount,
};
};

export default get;
13 changes: 13 additions & 0 deletions api/src/graphql/import.resolver.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import PokeAPIService from '@pokemon/services/pokeApi.service';
import PokemonSyncronizer from '@pokemon/services/pokemonSyncronizer.service';

const pokeApiService = new PokeAPIService();
const pokemonSyncronizer = PokemonSyncronizer(pokeApiService);

const importPokemon = async ({ id = 0 }) => {
await pokemonSyncronizer.queue({ id });

return { id };
};

export default importPokemon;
9 changes: 9 additions & 0 deletions api/src/graphql/resolvers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import createPokemon from './create.resolver';
import getPokemonList from './get.resolver';
import importPokemon from './import.resolver';

export default {
getPokemonList,
createPokemon,
importPokemon,
};
2 changes: 0 additions & 2 deletions api/src/repositories/pokemon.repository.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { PokemonModel } from './pokemon.sequelize.repository';

export class Pokemon {
public id?: number;
public name: string;
Expand Down
31 changes: 31 additions & 0 deletions api/src/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { buildSchema } from 'graphql';

const schema = buildSchema(`
type Pokemon {
id: Int
name: String!
type: String!
isFeatured: Boolean!
imageUrl: String
}

type PokemonList {
items: [Pokemon]
totalCount: Int
}

type ImportPokemon {
id: Int!
}

type Query {
getPokemonList(where: String, skip: Int, take: Int): PokemonList
}

type Mutation {
createPokemon(name: String!, type: String!, isFeatured: Boolean!, imageUrl: String): Pokemon!
importPokemon(id: Int!): ImportPokemon!
}
`);

export default schema;
Loading