From d03eef262437fb8bd4b85aa2872d7fdd4f5e5c2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adnan=20Rahi=C4=87?= Date: Wed, 28 Aug 2024 16:58:24 +0200 Subject: [PATCH 1/4] Guides: Add simplified K8s manifests (#41) * k8s: added basic manifest files * fix postgres manifest * cleanup * add tt agent * simplify manifests * add readme * improve readme * add port env * delete kompose file --------- Co-authored-by: Matheus Nogueira --- k8s/api.yaml | 65 +++++++++++++++++++++ k8s/cache.yaml | 49 ++++++++++++++++ k8s/db.yaml | 89 +++++++++++++++++++++++++++++ k8s/jaeger.yaml | 70 +++++++++++++++++++++++ k8s/otel-collector.yaml | 119 +++++++++++++++++++++++++++++++++++++++ k8s/queue.yaml | 53 +++++++++++++++++ k8s/readme.md | 19 +++++++ k8s/rpc.yaml | 66 ++++++++++++++++++++++ k8s/tracetest-agent.yaml | 59 +++++++++++++++++++ k8s/worker.yaml | 36 ++++++++++++ 10 files changed, 625 insertions(+) create mode 100644 k8s/api.yaml create mode 100644 k8s/cache.yaml create mode 100644 k8s/db.yaml create mode 100644 k8s/jaeger.yaml create mode 100644 k8s/otel-collector.yaml create mode 100644 k8s/queue.yaml create mode 100644 k8s/readme.md create mode 100644 k8s/rpc.yaml create mode 100644 k8s/tracetest-agent.yaml create mode 100644 k8s/worker.yaml diff --git a/k8s/api.yaml b/k8s/api.yaml new file mode 100644 index 0000000..1f34f8c --- /dev/null +++ b/k8s/api.yaml @@ -0,0 +1,65 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: api + name: api +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: api + template: + metadata: + annotations: + kompose.cmd: kompose convert -f docker-compose.yaml + kompose.version: 1.34.0 (HEAD) + labels: + io.kompose.service: api + spec: + containers: + - env: + - name: COLLECTOR_ENDPOINT + value: http://otel-collector.default.svc.cluster.local:4317 + - name: DATABASE_URL + value: postgresql://ashketchum:squirtle123@db:5432/pokeshop?schema=public + - name: POKE_API_BASE_URL + value: https://pokeapi.co/api/v2 + - name: RABBITMQ_HOST + value: guest:guest@queue + - name: REDIS_URL + value: cache + image: kubeshop/demo-pokemon-api:latest + imagePullPolicy: Always + livenessProbe: + exec: + command: + - wget + - --spider + - localhost:8081/pokemon/healthcheck + failureThreshold: 60 + periodSeconds: 1 + timeoutSeconds: 3 + name: api + ports: + - containerPort: 8081 + protocol: TCP + restartPolicy: Always + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: api + name: api +spec: + ports: + - name: "8081" + port: 8081 + targetPort: 8081 + selector: + io.kompose.service: api diff --git a/k8s/cache.yaml b/k8s/cache.yaml new file mode 100644 index 0000000..563703d --- /dev/null +++ b/k8s/cache.yaml @@ -0,0 +1,49 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: cache + name: cache +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: cache + template: + metadata: + labels: + io.kompose.service: cache + spec: + containers: + - image: redis:6 + livenessProbe: + exec: + command: + - redis-cli + - ping + failureThreshold: 60 + periodSeconds: 1 + timeoutSeconds: 3 + name: cache + ports: + - containerPort: 6379 + protocol: TCP + restartPolicy: Always + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: cache + name: cache +spec: + ports: + - name: "6379" + port: 6379 + targetPort: 6379 + selector: + io.kompose.service: cache diff --git a/k8s/db.yaml b/k8s/db.yaml new file mode 100644 index 0000000..e8d08b2 --- /dev/null +++ b/k8s/db.yaml @@ -0,0 +1,89 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: db + name: db +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: db + template: + metadata: + labels: + io.kompose.service: db + spec: + containers: + - env: + - name: POSTGRES_DB + value: pokeshop + - name: POSTGRES_PASSWORD + value: squirtle123 + - name: POSTGRES_USER + value: ashketchum + image: postgres:14 + name: db + ports: + - containerPort: 5432 + protocol: TCP + volumeMounts: + - mountPath: /var/lib/postgresql/data + name: dbdata + volumes: + - name: dbdata + persistentVolumeClaim: + claimName: db-volume-claim + restartPolicy: Always + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: db + name: db +spec: + ports: + - name: "5432" + port: 5432 + targetPort: 5432 + selector: + io.kompose.service: db + +# Persistent Volume +--- +apiVersion: v1 +kind: PersistentVolume +metadata: + name: db-volume + labels: + type: local + io.kompose.service: db +spec: + storageClassName: manual + capacity: + storage: 10Gi + accessModes: + - ReadWriteMany + hostPath: + path: /data/postgresql + +# Persistent Volume Claim +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: db-volume-claim + labels: + io.kompose.service: db +spec: + storageClassName: manual + accessModes: + - ReadWriteMany + resources: + requests: + storage: 10Gi diff --git a/k8s/jaeger.yaml b/k8s/jaeger.yaml new file mode 100644 index 0000000..6d3d840 --- /dev/null +++ b/k8s/jaeger.yaml @@ -0,0 +1,70 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: jaeger + name: jaeger +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: jaeger + template: + metadata: + labels: + io.kompose.service: jaeger + spec: + containers: + - env: + - name: COLLECTOR_OTLP_ENABLED + value: "true" + - name: COLLECTOR_ZIPKIN_HOST_PORT + value: :9411 + image: jaegertracing/all-in-one:latest + livenessProbe: + exec: + command: + - wget + - --spider + - localhost:16686 + failureThreshold: 60 + periodSeconds: 1 + timeoutSeconds: 3 + name: jaeger + ports: + - containerPort: 4317 + protocol: TCP + - containerPort: 14250 + protocol: TCP + - containerPort: 16685 + protocol: TCP + - containerPort: 16686 + protocol: TCP + restartPolicy: Always + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: jaeger + name: jaeger +spec: + ports: + - name: "4317" + port: 4317 + targetPort: 4317 + - name: "14250" + port: 14250 + targetPort: 14250 + - name: "16685" + port: 16685 + targetPort: 16685 + - name: "16686" + port: 16686 + targetPort: 16686 + selector: + io.kompose.service: jaeger diff --git a/k8s/otel-collector.yaml b/k8s/otel-collector.yaml new file mode 100644 index 0000000..1e2c460 --- /dev/null +++ b/k8s/otel-collector.yaml @@ -0,0 +1,119 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: otel-collector + name: otel-collector +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: otel-collector + strategy: + type: Recreate + template: + metadata: + labels: + io.kompose.service: otel-collector + spec: + containers: + - args: + - --config + - /config/otel-local-config.yaml + env: + - name: JAEGER_ENDPOINT + value: jaeger.default.svc.cluster.local:4317 + - name: TRACETEST_AGENT_ENDPOINT + value: tracetest-agent.default.svc.cluster.local:4317 + image: otel/opentelemetry-collector-contrib:0.107.0 + name: otel-collector + ports: + - containerPort: 55679 + protocol: TCP + - containerPort: 8888 + protocol: TCP + - containerPort: 4317 + protocol: TCP + - containerPort: 4318 + protocol: TCP + volumeMounts: + - mountPath: /config + name: otel-collector-cm0 + restartPolicy: Always + volumes: + - configMap: + items: + - key: collector.config.yaml + path: otel-local-config.yaml + name: otel-collector-cm0 + name: otel-collector-cm0 + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: otel-collector + name: otel-collector +spec: + ports: + - name: "55679" + port: 55679 + targetPort: 55679 + - name: "8888" + port: 8888 + targetPort: 8888 + - name: "4317" + port: 4317 + targetPort: 4317 + - name: "4318" + port: 4318 + targetPort: 4318 + selector: + io.kompose.service: otel-collector + +# ConfigMap +--- +apiVersion: v1 +data: + collector.config.yaml: | + receivers: + otlp: + protocols: + grpc: + endpoint: 0.0.0.0:4317 + http: + endpoint: 0.0.0.0:4318 + processors: + batch: + exporters: + logging: + loglevel: debug + otlp/jaeger: + endpoint: ${JAEGER_ENDPOINT} + tls: + insecure: true + otlp/tracetest: + endpoint: ${TRACETEST_AGENT_ENDPOINT} + tls: + insecure: true + service: + pipelines: + traces/jaeger: + receivers: [otlp] + processors: [] + exporters: [logging, otlp/jaeger] + traces/tracetest: + receivers: [otlp] + processors: [batch] + exporters: [otlp/tracetest] +kind: ConfigMap +metadata: + annotations: + use-subpath: "true" + labels: + io.kompose.service: otel-collector + name: otel-collector-cm0 diff --git a/k8s/queue.yaml b/k8s/queue.yaml new file mode 100644 index 0000000..535111b --- /dev/null +++ b/k8s/queue.yaml @@ -0,0 +1,53 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: queue + name: queue +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: queue + template: + metadata: + labels: + io.kompose.service: queue + spec: + containers: + - image: rabbitmq:3.12 + livenessProbe: + exec: + command: + - rabbitmq-diagnostics -q check_running + failureThreshold: 60 + periodSeconds: 1 + timeoutSeconds: 5 + name: queue + ports: + - containerPort: 5672 + protocol: TCP + - containerPort: 15672 + protocol: TCP + restartPolicy: Always + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: queue + name: queue +spec: + ports: + - name: "5672" + port: 5672 + targetPort: 5672 + - name: "15672" + port: 15672 + targetPort: 15672 + selector: + io.kompose.service: queue diff --git a/k8s/readme.md b/k8s/readme.md new file mode 100644 index 0000000..c20fb20 --- /dev/null +++ b/k8s/readme.md @@ -0,0 +1,19 @@ +# Install Pokeshop with K8s Manifests + +1. [Get your Tracetest API key and env id](https://app.tracetest.io/retrieve-token) +2. Add your API key and env id in the `tracetest-agent.yaml` +3. Apply all resources + + ```bash + kubectl apply -f . + ``` +4. Create and run a test by going to [`app.tracetest.io`](https://app.tracetest.io) and using the internal Kubernetes service networking: + + - **POST** `http://api.default.svc.cluster.local:8081/pokemon/import` - Body: `{ "id": 1 }` + - **GET** `http://api.default.svc.cluster.local:8081/pokemon` + + ![](https://res.cloudinary.com/djwdcmwdz/image/upload/v1724764008/docs/app.tracetest.io_organizations_ttorg_e66318ba6544b856_environments_ttenv_4b0e8945dbe5045a_test_tTFZ453Ig_run_9_selectedSpan_bb8ba205b42a8619_nylqid.png) + +5. View the trace and create test specs by going to the `Test` tab. + + ![](https://res.cloudinary.com/djwdcmwdz/image/upload/v1724764098/docs/app.tracetest.io_organizations_ttorg_e66318ba6544b856_environments_ttenv_4b0e8945dbe5045a_test_tTFZ453Ig_run_9_selectedSpan_bb8ba205b42a8619_1_xaxlbi.png) \ No newline at end of file diff --git a/k8s/rpc.yaml b/k8s/rpc.yaml new file mode 100644 index 0000000..aeebb4e --- /dev/null +++ b/k8s/rpc.yaml @@ -0,0 +1,66 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: rpc + name: rpc +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: rpc + template: + metadata: + labels: + io.kompose.service: rpc + spec: + containers: + - env: + - name: NPM_RUN_COMMAND + value: rpc + - name: COLLECTOR_ENDPOINT + value: http://otel-collector.default.svc.cluster.local:4317 + - name: DATABASE_URL + value: postgresql://ashketchum:squirtle123@db:5432/pokeshop?schema=public + - name: POKE_API_BASE_URL + value: https://pokeapi.co/api/v2 + - name: RABBITMQ_HOST + value: guest:guest@queue + - name: REDIS_URL + value: cache + - name: RPC_PORT + value: "8082" + image: kubeshop/demo-pokemon-api:latest + imagePullPolicy: Always + livenessProbe: + exec: + command: + - wget + - --spider + - 0.0.0.0:8081/pokemon/healthcheck + failureThreshold: 60 + periodSeconds: 1 + timeoutSeconds: 3 + name: rpc + ports: + - containerPort: 8082 + protocol: TCP + restartPolicy: Always + +# Service +--- +apiVersion: v1 +kind: Service +metadata: + labels: + io.kompose.service: rpc + name: rpc +spec: + ports: + - name: "8082" + port: 8082 + targetPort: 8082 + selector: + io.kompose.service: rpc diff --git a/k8s/tracetest-agent.yaml b/k8s/tracetest-agent.yaml new file mode 100644 index 0000000..b95380f --- /dev/null +++ b/k8s/tracetest-agent.yaml @@ -0,0 +1,59 @@ +--- +# Service +apiVersion: v1 +kind: Service +metadata: + name: tracetest-agent + labels: + app.kubernetes.io/name: tracetest-agent + app.kubernetes.io/instance: tracetest-agent +spec: + selector: + app.kubernetes.io/name: tracetest-agent + app.kubernetes.io/instance: tracetest-agent + ports: + - name: grpc-collector-entrypoint + protocol: TCP + port: 4317 + targetPort: 4317 + - name: http-collector-entrypoint + protocol: TCP + port: 4318 + targetPort: 4318 +--- +# Deployment +apiVersion: apps/v1 +kind: Deployment +metadata: + name: tracetest-agent + labels: + app: tracetest-agent + app.kubernetes.io/name: tracetest-agent + app.kubernetes.io/instance: tracetest-agent +spec: + selector: + matchLabels: + app.kubernetes.io/name: tracetest-agent + app.kubernetes.io/instance: tracetest-agent + template: + metadata: + labels: + app.kubernetes.io/name: tracetest-agent + app.kubernetes.io/instance: tracetest-agent + spec: + containers: + - name: agent + image: "kubeshop/tracetest-agent:latest" + imagePullPolicy: Always + args: [ + "--environment", + "", # Add your env id + "--api-key", + "$TRACETEST_API_KEY", + "--server-url", + "https://app.tracetest.io", + "--mode='verbose'", + ] + env: + - name: TRACETEST_API_KEY + value: "" # Add your API key diff --git a/k8s/worker.yaml b/k8s/worker.yaml new file mode 100644 index 0000000..a600a69 --- /dev/null +++ b/k8s/worker.yaml @@ -0,0 +1,36 @@ +# Deployment +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + labels: + io.kompose.service: worker + name: worker +spec: + replicas: 1 + selector: + matchLabels: + io.kompose.service: worker + template: + metadata: + labels: + io.kompose.service: worker + spec: + containers: + - env: + - name: NPM_RUN_COMMAND + value: worker + - name: COLLECTOR_ENDPOINT + value: http://otel-collector.default.svc.cluster.local:4317 + - name: DATABASE_URL + value: postgresql://ashketchum:squirtle123@db:5432/pokeshop?schema=public + - name: POKE_API_BASE_URL + value: https://pokeapi.co/api/v2 + - name: RABBITMQ_HOST + value: guest:guest@queue + - name: REDIS_URL + value: cache + image: kubeshop/demo-pokemon-api:latest + imagePullPolicy: Always + name: worker + restartPolicy: Always From 87dbde95525b3b2d618b20df45ceb1dc169a9b0a Mon Sep 17 00:00:00 2001 From: Oscar Reyes Date: Wed, 28 Aug 2024 11:45:56 -0600 Subject: [PATCH 2/4] feat: Graphql layer (#42) --- api/package-lock.json | 67 ++++++++++++++++++++++ api/package.json | 1 + api/src/api.ts | 4 ++ api/src/graphql/create.resolver.ts | 9 +++ api/src/graphql/get.resolver.ts | 20 +++++++ api/src/graphql/import.resolver.ts | 13 +++++ api/src/graphql/resolvers.ts | 9 +++ api/src/repositories/pokemon.repository.ts | 2 - api/src/schema.ts | 31 ++++++++++ 9 files changed, 154 insertions(+), 2 deletions(-) create mode 100644 api/src/graphql/create.resolver.ts create mode 100644 api/src/graphql/get.resolver.ts create mode 100644 api/src/graphql/import.resolver.ts create mode 100644 api/src/graphql/resolvers.ts create mode 100644 api/src/schema.ts diff --git a/api/package-lock.json b/api/package-lock.json index 8d0c2c3..97fc45a 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -33,6 +33,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", @@ -5123,6 +5124,47 @@ "integrity": "sha512-Tpp60P6IUJDTuOq/5Z8cdskzJujfwqfOTkrwIwj7IRISpnkJnT6SyJ4PCPnGMoFjC9ddhal5KVIYtAt97ix05A==", "dev": true }, + "node_modules/express-graphql": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/express-graphql/-/express-graphql-0.12.0.tgz", + "integrity": "sha512-DwYaJQy0amdy3pgNtiTDuGGM2BLdj+YO2SgbKoLliCfuHv3VVTt7vNG/ZqK2hRYjtYHE2t2KB705EU94mE64zg==", + "deprecated": "This package is no longer maintained. We recommend using `graphql-http` instead. Please consult the migration document https://github.com/graphql/graphql-http#migrating-express-grpahql.", + "dependencies": { + "accepts": "^1.3.7", + "content-type": "^1.0.4", + "http-errors": "1.8.0", + "raw-body": "^2.4.1" + }, + "engines": { + "node": ">= 10.x" + }, + "peerDependencies": { + "graphql": "^14.7.0 || ^15.3.0" + } + }, + "node_modules/express-graphql/node_modules/http-errors": { + "version": "1.8.0", + "resolved": "https://registry.npmjs.org/http-errors/-/http-errors-1.8.0.tgz", + "integrity": "sha512-4I8r0C5JDhT5VkvI47QktDW75rNlGVsUf/8hzjCC/wkWI/jdTRmBb9aI7erSG82r1bjKY3F6k28WnsVxB1C73A==", + "dependencies": { + "depd": "~1.1.2", + "inherits": "2.0.4", + "setprototypeof": "1.2.0", + "statuses": ">= 1.5.0 < 2", + "toidentifier": "1.0.0" + }, + "engines": { + "node": ">= 0.6" + } + }, + "node_modules/express-graphql/node_modules/toidentifier": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/toidentifier/-/toidentifier-1.0.0.tgz", + "integrity": "sha512-yaOH/Pk/VEhBWWTlhI+qXxDFXlejDGcQipMlyxda9nthulaxLZUNcUqFxokp0vcYnvteJln5FNQDRrxj3YcbVw==", + "engines": { + "node": ">=0.6" + } + }, "node_modules/extend": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.2.tgz", @@ -5512,6 +5554,15 @@ "integrity": "sha512-9ByhssR2fPVsNZj478qUUbKfmL0+t5BDVyjShtyZZLiK7ZDAArFFfopyOTj0M05wE2tJPisA4iTnnXl2YoPvOA==", "dev": true }, + "node_modules/graphql": { + "version": "15.9.0", + "resolved": "https://registry.npmjs.org/graphql/-/graphql-15.9.0.tgz", + "integrity": "sha512-GCOQdvm7XxV1S4U4CGrsdlEN37245eC8P9zaYCMr6K1BG0IPGy5lUwmJsEOGyl1GD6HXjOtl2keCP9asRBwNvA==", + "peer": true, + "engines": { + "node": ">= 10.x" + } + }, "node_modules/has": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/has/-/has-1.0.3.tgz", @@ -6290,6 +6341,22 @@ "node": ">= 10" } }, + "node_modules/koa-graphql": { + "version": "0.12.0", + "resolved": "https://registry.npmjs.org/koa-graphql/-/koa-graphql-0.12.0.tgz", + "integrity": "sha512-c1G5qcE7hIBCP+21fiUX7Z0CjbI3+mn4vDuAxQVP9sfpTDW8O+7Mckei/Ar7dY/w6smrHcMxmt8/KXc1d76ONg==", + "dependencies": { + "@types/koa": "^2.13.4", + "express-graphql": "0.12.0", + "http-errors": "^1.7.3" + }, + "engines": { + "node": ">= 10.x" + }, + "peerDependencies": { + "graphql": "^14.7.0 || ^15.3.0" + } + }, "node_modules/koa-logger": { "version": "3.2.1", "resolved": "https://registry.npmjs.org/koa-logger/-/koa-logger-3.2.1.tgz", diff --git a/api/package.json b/api/package.json index 1b92a90..6be6fef 100644 --- a/api/package.json +++ b/api/package.json @@ -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", diff --git a/api/src/api.ts b/api/src/api.ts index 4529130..eaf9e4c 100644 --- a/api/src/api.ts +++ b/api/src/api.ts @@ -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; @@ -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); diff --git a/api/src/graphql/create.resolver.ts b/api/src/graphql/create.resolver.ts new file mode 100644 index 0000000..c23ded2 --- /dev/null +++ b/api/src/graphql/create.resolver.ts @@ -0,0 +1,9 @@ +import { getPokemonRepository, Pokemon } from '@pokemon/repositories'; + +const create = async (raw: Pokemon): Promise => { + const repository = getPokemonRepository(); + + return repository.create(new Pokemon(raw)); +}; + +export default create; diff --git a/api/src/graphql/get.resolver.ts b/api/src/graphql/get.resolver.ts new file mode 100644 index 0000000..5ff7658 --- /dev/null +++ b/api/src/graphql/get.resolver.ts @@ -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 => { + const repository = getPokemonRepository(); + + const [items, totalCount] = await Promise.all([repository.findMany(query), repository.count()]); + + return { + items, + totalCount, + }; +}; + +export default get; \ No newline at end of file diff --git a/api/src/graphql/import.resolver.ts b/api/src/graphql/import.resolver.ts new file mode 100644 index 0000000..c1eea4b --- /dev/null +++ b/api/src/graphql/import.resolver.ts @@ -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; diff --git a/api/src/graphql/resolvers.ts b/api/src/graphql/resolvers.ts new file mode 100644 index 0000000..84161de --- /dev/null +++ b/api/src/graphql/resolvers.ts @@ -0,0 +1,9 @@ +import createPokemon from './create.resolver'; +import getPokemonList from './get.resolver'; +import importPokemon from './import.resolver'; + +export default { + getPokemonList, + createPokemon, + importPokemon, +}; diff --git a/api/src/repositories/pokemon.repository.ts b/api/src/repositories/pokemon.repository.ts index 91fe8cc..fdad108 100644 --- a/api/src/repositories/pokemon.repository.ts +++ b/api/src/repositories/pokemon.repository.ts @@ -1,5 +1,3 @@ -import { PokemonModel } from './pokemon.sequelize.repository'; - export class Pokemon { public id?: number; public name: string; diff --git a/api/src/schema.ts b/api/src/schema.ts new file mode 100644 index 0000000..c892e6d --- /dev/null +++ b/api/src/schema.ts @@ -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; From 3f02f684514870eff25e1c46d8aec5d131e4670b Mon Sep 17 00:00:00 2001 From: Oscar Reyes Date: Thu, 29 Aug 2024 08:54:27 -0600 Subject: [PATCH 3/4] updating token --- test/artillery/import-pokemon.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/artillery/import-pokemon.yml b/test/artillery/import-pokemon.yml index d9ed412..4e21104 100644 --- a/test/artillery/import-pokemon.yml +++ b/test/artillery/import-pokemon.yml @@ -22,7 +22,7 @@ config: environment: "test" tool: "Artillery" tracetest: - token: tttoken_a2d67564c92023b6 + token: s_CIbKCIR definition: ./test/artillery/import-pokemon-definition.yml serverUrl: https://app.tracetest.io/ runInfo: From e04430ad7c86ceedbfd2df5f7c93699505c776a4 Mon Sep 17 00:00:00 2001 From: Oscar Reyes Date: Thu, 29 Aug 2024 09:01:57 -0600 Subject: [PATCH 4/4] updating token --- test/artillery/import-pokemon.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/artillery/import-pokemon.yml b/test/artillery/import-pokemon.yml index 4e21104..33c1e27 100644 --- a/test/artillery/import-pokemon.yml +++ b/test/artillery/import-pokemon.yml @@ -22,7 +22,7 @@ config: environment: "test" tool: "Artillery" tracetest: - token: s_CIbKCIR + token: 0_3A703Sg definition: ./test/artillery/import-pokemon-definition.yml serverUrl: https://app.tracetest.io/ runInfo: