diff --git a/backend/typescript/graphql/types/testType.ts b/backend/typescript/graphql/types/testType.ts new file mode 100644 index 00000000..840a9a97 --- /dev/null +++ b/backend/typescript/graphql/types/testType.ts @@ -0,0 +1,5 @@ +export const typeDefs = gql` + type Query { + hello: String + } +`; diff --git a/backend/typescript/prisma/schema.prisma b/backend/typescript/prisma/schema.prisma index abfb9d6b..8932f79a 100644 --- a/backend/typescript/prisma/schema.prisma +++ b/backend/typescript/prisma/schema.prisma @@ -132,6 +132,7 @@ model notification { } model notification_user { + id Int @id @default(autoincrement()) notification notification @relation(fields: [notification_id], references: [id]) notification_id Int recipient resident @relation(fields: [recipient_id], references: [id]) diff --git a/backend/typescript/server.ts b/backend/typescript/server.ts index 4d18206c..7ece2fc0 100644 --- a/backend/typescript/server.ts +++ b/backend/typescript/server.ts @@ -3,10 +3,14 @@ import cors from "cors"; import express from "express"; import * as firebaseAdmin from "firebase-admin"; -import { ApolloServer } from "apollo-server-express"; +import { ApolloServer, gql } from "apollo-server-express"; // Import gql from apollo-server-express import { sequelize } from "./models"; +import { PrismaClient } from "@prisma/client" import schema from "./graphql"; +const prisma = new PrismaClient(); +const { staff } = prisma; + const CORS_ALLOW_LIST = [ "http://localhost:3000", "https://uw-blueprint-starter-code.firebaseapp.com", @@ -25,9 +29,63 @@ app.use(cors(CORS_OPTIONS)); app.use(express.json()); app.use(express.urlencoded({ extended: true })); + +// Define your GraphQL schema +const typeDefs = gql` + type Query { + hello: String + getData: String + } + + type Profile { + role_id: Int, + first_name: String, + last_name: String, + email: String, + } + + type Mutation { + addProfile(input: StaffInput!): Profile + } + + input StaffInput { + role_id: Int + first_name: String + last_name: String + email: String! + + } +`; + +const resolvers = { + Query: { + hello: () => 'testing', + getData: async () => { + const data = await prisma.staff.count(); + await prisma.$disconnect(); + return data; + } + }, + Mutation: { + addProfile: async (parent: any, args: any) => { + const { data } = args; + try { + const newProfile = await prisma.profile.create({ + data: data, + }); + return newProfile; + } catch (error: any) { + throw new Error(`failed to create new profile: ${error.message}`) + } + } + }, + +}; + const server = new ApolloServer({ - schema, - context: ({ req, res }) => ({ req, res }), + typeDefs, // Use your GraphQL schema + resolvers, + context: ({ req, res }) => ({ req, res, prisma }), playground: { settings: { "request.credentials": "include", @@ -55,6 +113,5 @@ firebaseAdmin.initializeApp({ }); app.listen({ port: process.env.PORT || 5000 }, () => { - /* eslint-disable-next-line no-console */ console.info(`Server is listening on port ${process.env.PORT || 5000}!`); });