mirror of
https://github.com/aljazceru/landscape-template.git
synced 2026-01-10 09:54:26 +01:00
Hello GraphQL
This commit is contained in:
37
functions/graphql/index.mjs
Normal file
37
functions/graphql/index.mjs
Normal file
@@ -0,0 +1,37 @@
|
||||
const { ApolloServer } = require("apollo-server-lambda");
|
||||
const { PrismaClient } = require("@prisma/client");
|
||||
|
||||
const resolvers = require("./resolvers");
|
||||
const typeDefs = require("./typeDefs");
|
||||
|
||||
const prisma = new PrismaClient();
|
||||
const server = new ApolloServer({
|
||||
resolvers,
|
||||
typeDefs,
|
||||
context: () => {
|
||||
return {
|
||||
prisma,
|
||||
};
|
||||
},
|
||||
});
|
||||
|
||||
const apolloHandler = server.createHandler({
|
||||
cors: {
|
||||
origin: "*",
|
||||
credentials: true,
|
||||
},
|
||||
});
|
||||
|
||||
// https://github.com/vendia/serverless-express/issues/427#issuecomment-924580007
|
||||
const handler = (event, context, ...args) => {
|
||||
return apolloHandler(
|
||||
{
|
||||
...event,
|
||||
requestContext: context,
|
||||
},
|
||||
context,
|
||||
...args
|
||||
);
|
||||
};
|
||||
|
||||
exports.handler = handler;
|
||||
17
functions/graphql/resolvers.js
Normal file
17
functions/graphql/resolvers.js
Normal file
@@ -0,0 +1,17 @@
|
||||
module.exports = {
|
||||
Query: {
|
||||
allProjects: async (_source, args, context) => {
|
||||
return context.prisma.project.findMany();
|
||||
},
|
||||
getProject: async (_source, args, context) => {
|
||||
return context.prisma.project.findUnique({
|
||||
where: {
|
||||
id: args.id,
|
||||
},
|
||||
});
|
||||
},
|
||||
},
|
||||
Mutation: {
|
||||
vote: async (_source, args, context) => {},
|
||||
},
|
||||
};
|
||||
23
functions/graphql/typeDefs.js
Normal file
23
functions/graphql/typeDefs.js
Normal file
@@ -0,0 +1,23 @@
|
||||
const { gql } = require("apollo-server-lambda");
|
||||
|
||||
module.exports = gql`
|
||||
type Project {
|
||||
id: Int!
|
||||
cover_image: String!
|
||||
thumbnail_image: String!
|
||||
title: String!
|
||||
website: String!
|
||||
votes_count: Int!
|
||||
category: Category!
|
||||
}
|
||||
|
||||
type Category {
|
||||
id: Int!
|
||||
title: String!
|
||||
}
|
||||
|
||||
type Query {
|
||||
allProjects: [Project!]!
|
||||
getProject(id: Int!): Project
|
||||
}
|
||||
`;
|
||||
23
migrations/20211128043804_init/migration.sql
Normal file
23
migrations/20211128043804_init/migration.sql
Normal file
@@ -0,0 +1,23 @@
|
||||
-- CreateTable
|
||||
CREATE TABLE "Category" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
|
||||
CONSTRAINT "Category_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- CreateTable
|
||||
CREATE TABLE "Project" (
|
||||
"id" SERIAL NOT NULL,
|
||||
"title" TEXT NOT NULL,
|
||||
"description" TEXT NOT NULL,
|
||||
"website" TEXT NOT NULL,
|
||||
"thumbnail_image" TEXT NOT NULL,
|
||||
"cover_image" TEXT NOT NULL,
|
||||
"category_id" INTEGER NOT NULL,
|
||||
|
||||
CONSTRAINT "Project_pkey" PRIMARY KEY ("id")
|
||||
);
|
||||
|
||||
-- AddForeignKey
|
||||
ALTER TABLE "Project" ADD CONSTRAINT "Project_category_id_fkey" FOREIGN KEY ("category_id") REFERENCES "Category"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
|
||||
3
migrations/migration_lock.toml
Normal file
3
migrations/migration_lock.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
# Please do not edit this file manually
|
||||
# It should be added in your version-control system (i.e. Git)
|
||||
provider = "postgresql"
|
||||
3
netlify.toml
Normal file
3
netlify.toml
Normal file
@@ -0,0 +1,3 @@
|
||||
[build]
|
||||
functions = "functions" # netlify-lambda builds to this folder AND Netlify reads functions from here
|
||||
publish = "build" # create-react-app builds to this folder, Netlify should serve all these files statically
|
||||
1209
package-lock.json
generated
1209
package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -4,6 +4,7 @@
|
||||
"private": true,
|
||||
"homepage": ".",
|
||||
"dependencies": {
|
||||
"@apollo/client": "^3.5.5",
|
||||
"@reduxjs/toolkit": "^1.6.2",
|
||||
"@testing-library/jest-dom": "^5.15.0",
|
||||
"@testing-library/react": "^11.2.7",
|
||||
@@ -26,7 +27,11 @@
|
||||
"react-router-dom": "^6.0.2",
|
||||
"react-scripts": "4.0.3",
|
||||
"typescript": "^4.4.4",
|
||||
"web-vitals": "^1.1.2"
|
||||
"web-vitals": "^1.1.2",
|
||||
"@prisma/client": "3.5.0",
|
||||
"apollo-server": "^3.5.0",
|
||||
"graphql": "^16.0.1",
|
||||
"prisma": "3.5.0"
|
||||
},
|
||||
"scripts": {
|
||||
"start": "craco start",
|
||||
@@ -83,4 +88,4 @@
|
||||
"postcss": "^7.0.39",
|
||||
"tailwindcss": "npm:@tailwindcss/postcss7-compat@^2.2.17"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
25
schema.prisma
Normal file
25
schema.prisma
Normal file
@@ -0,0 +1,25 @@
|
||||
datasource db {
|
||||
provider = "postgresql"
|
||||
url = env("DATABASE_URL")
|
||||
}
|
||||
|
||||
generator client {
|
||||
provider = "prisma-client-js"
|
||||
}
|
||||
|
||||
model Category {
|
||||
id Int @id @default(autoincrement())
|
||||
title String
|
||||
Project Project[]
|
||||
}
|
||||
|
||||
model Project {
|
||||
id Int @id @default(autoincrement())
|
||||
title String
|
||||
description String
|
||||
website String
|
||||
thumbnail_image String
|
||||
cover_image String
|
||||
category Category @relation(fields: [category_id], references: [id])
|
||||
category_id Int
|
||||
}
|
||||
Reference in New Issue
Block a user