Skip to content

Commit

Permalink
got query working but not mutation yet
Browse files Browse the repository at this point in the history
  • Loading branch information
benymng committed Nov 4, 2023
1 parent 8a8e4c8 commit 95a177f
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 4 deletions.
5 changes: 5 additions & 0 deletions backend/typescript/graphql/types/testType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export const typeDefs = gql`
type Query {
hello: String
}
`;
1 change: 1 addition & 0 deletions backend/typescript/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down
65 changes: 61 additions & 4 deletions backend/typescript/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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}!`);
});

0 comments on commit 95a177f

Please sign in to comment.