From 96f161994e387e377725fd9d79404e0bb03a8d79 Mon Sep 17 00:00:00 2001 From: alea Date: Fri, 9 Oct 2026 23:54:32 +0200 Subject: [PATCH] Satte opp database-queries for Techniques --- backend/pnpm-workspace.yaml | 2 +- backend/src/graphql/resolvers/index.ts | 2 + .../graphql/resolvers/techniqueResolver.ts | 26 ++++++++++- backend/src/graphql/typeDefs.ts | 15 +++++++ .../src/repositories/techniqueRepository.ts | 44 +++++++++++++++++++ 5 files changed, 87 insertions(+), 2 deletions(-) diff --git a/backend/pnpm-workspace.yaml b/backend/pnpm-workspace.yaml index 1c93733..e21f80f 100644 --- a/backend/pnpm-workspace.yaml +++ b/backend/pnpm-workspace.yaml @@ -1,3 +1,3 @@ allowBuilds: - "@apollo/protobufjs": set this to true or false + "@apollo/protobufjs": true esbuild: true diff --git a/backend/src/graphql/resolvers/index.ts b/backend/src/graphql/resolvers/index.ts index 33c0752..5443653 100644 --- a/backend/src/graphql/resolvers/index.ts +++ b/backend/src/graphql/resolvers/index.ts @@ -13,11 +13,13 @@ */ import { threatActorResolvers } from "./threatActorResolver.js"; +import { techniqueResolvers } from "./techniqueResolver.js"; export const resolvers = { Query: { ...threatActorResolvers.Query, + ...techniqueResolvers.Query, }, }; \ No newline at end of file diff --git a/backend/src/graphql/resolvers/techniqueResolver.ts b/backend/src/graphql/resolvers/techniqueResolver.ts index c0d3458..d4d1fe2 100644 --- a/backend/src/graphql/resolvers/techniqueResolver.ts +++ b/backend/src/graphql/resolvers/techniqueResolver.ts @@ -9,4 +9,28 @@ * * * GraphQL -> Resolver -> Repository -> Drizzle -> MySQL - */ \ No newline at end of file + */ +import { + getAllTechniques, + getTechniqueByMitreId, +} from "../../repositories/techniqueRepository.js"; + +export const techniqueResolvers = { + Query: { + /** + * Henter alle Techniques. + */ + allTechniques: async () => { + return getAllTechniques(); + }, + + /** + * Henter én Technique basert på MITRE-ID + */ + techniqueByMitreId: async (_parent: unknown,args: { mitreId: string },) => { + return getTechniqueByMitreId( + args.mitreId, + ); + }, + }, +}; \ No newline at end of file diff --git a/backend/src/graphql/typeDefs.ts b/backend/src/graphql/typeDefs.ts index 6c618dc..d83fb7d 100644 --- a/backend/src/graphql/typeDefs.ts +++ b/backend/src/graphql/typeDefs.ts @@ -25,4 +25,19 @@ export const typeDefs = `#graphql # Henter én threat actor basert på MITRE-ID threatActorByMitreId(mitreId: String!): ThreatActor } + + type Technique { + mitreId: String! + name: String! + description: String! + } + + type Query { + # Henter alle Techniques + allTechniques: [Technique!]! + + # Henter én Technique basert på MITRE-ID + techniqueByMitreId(mitreId: String!): Technique + } + `; \ No newline at end of file diff --git a/backend/src/repositories/techniqueRepository.ts b/backend/src/repositories/techniqueRepository.ts index b8faa23..2cb9733 100644 --- a/backend/src/repositories/techniqueRepository.ts +++ b/backend/src/repositories/techniqueRepository.ts @@ -15,3 +15,47 @@ * !!!ALTSÅ HER SKRIVER VI KODEN SOM FAKTISK HENTER UT DATA FRA DB VIA DRIZZLE!!!! */ +import { eq } from "drizzle-orm"; + +import { db } from "../db/connection.js"; +import { technique } from "../db/schema.js"; + +/** + * Henter alle Techniques fra databasen. + */ +export async function getAllTechniques() { + return db + .select({ + mitreId: technique.mitre_id, + name: technique.name, + description: technique.description, + }) + .from(technique); +} + +/** + * Henter én Technique basert på MITRE-ID. + * + * Eksempel: + * T1566 -> Phishing + */ +export async function getTechniqueByMitreId( + mitreId: string, +) { + const [result] = await db + .select({ + mitreId: technique.mitre_id, + name: technique.name, + description: technique.description, + }) + .from(technique) + .where( + eq( + technique.mitre_id, + mitreId, + ), + ) + .limit(1); + + return result ?? null; +} \ No newline at end of file