Skip to content

Commit

Permalink
Added graphql server
Browse files Browse the repository at this point in the history
  • Loading branch information
Prabhat Pal committed Feb 2, 2021
1 parent 9836736 commit 9fd3e01
Show file tree
Hide file tree
Showing 12 changed files with 543 additions and 24 deletions.
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"@apollo/client": "^3.3.7",
"@hookform/resolvers": "1.3.0",
"@prisma/client": "2.12.0",
"apollo-server-micro": "^2.19.2",
"firebase": "^8.2.4",
"firebase-admin": "^9.4.2",
"framer-motion": "^3.2.2-rc.1",
Expand All @@ -30,7 +31,9 @@
"react-icons": "^4.1.0",
"react-map-gl": "5.2.10",
"react-select": "^4.0.2",
"reflect-metadata": "^0.1.13",
"sass": "^1.32.5",
"type-graphql": "^1.1.1",
"use-debounce": "^5.2.0",
"use-google-maps-script": "^0.1.2",
"use-places-autocomplete": "^1.8.1",
Expand Down
29 changes: 29 additions & 0 deletions pages/api/graphql.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import "reflect-metadata";
import { ApolloServer } from "apollo-server-micro";
import { Context } from "src/schema/context";
import { NextApiRequest } from "next";
import { loadIdToken } from "src/auth/firebaseAdmin";
import { prisma } from "src/prisma";
import { schema } from "src/schema";

const server = new ApolloServer({
schema,
context: async ({ req }: { req: NextApiRequest }): Promise<Context> => {
const uid = await loadIdToken(req);

return {
uid,
prisma,
};
},
});

const handler = server.createHandler({ path: "/api/graphql" });

export const config = {
api: {
bodyParser: false,
},
};

export default handler;
4 changes: 2 additions & 2 deletions pages/auth.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetServerSideProps } from "next";
import { GetServerSideProps, NextApiRequest } from "next";
import FirebaseAuth from "src/components/FirebaseAuth";
import { loadIdToken } from "src/auth/firebaseAdmin";

Expand All @@ -11,7 +11,7 @@ const Auth = () => {
};

export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
const uid = await loadIdToken(req.cookies?.token);
const uid = await loadIdToken(req as NextApiRequest);

if (uid) {
res.setHeader("location", "/");
Expand Down
4 changes: 2 additions & 2 deletions pages/places/add.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useEffect, useState } from "react";
import { GetServerSideProps } from "next";
import { GetServerSideProps, NextApiRequest } from "next";
import { loadIdToken } from "src/auth/firebaseAdmin";
import {
FileInput,
Expand Down Expand Up @@ -156,7 +156,7 @@ const Add = () => {
};

export const getServerSideProps: GetServerSideProps = async ({ req, res }) => {
const uid = await loadIdToken(req.cookies?.token);
const uid = await loadIdToken(req as NextApiRequest);

if (!uid) {
res.setHeader("location", "/auth");
Expand Down
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ datasource db {

generator client {
provider = "prisma-client-js"
output = "../node_modules/.prisma/client"
}

model Place {
Expand Down
8 changes: 8 additions & 0 deletions schema.gql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------

type Query {
hello: String!
}
9 changes: 6 additions & 3 deletions src/auth/firebaseAdmin.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as admin from "firebase-admin";
import { NextApiRequest } from "next";

const verifyIdToken = (token: string) => {
const firebasePvtKey: string = process.env.FIREBASE_PRIVATE_KEY.replace(
Expand All @@ -22,10 +23,12 @@ const verifyIdToken = (token: string) => {
.catch(() => null);
};

export const loadIdToken = async (token: string): Promise<string | null> => {
if (!token) return null;
export const loadIdToken = async (
req: NextApiRequest
): Promise<string | null> => {
if (!req.cookies.token) return null;

const decoded = await verifyIdToken(token);
const decoded = await verifyIdToken(req.cookies.token);

if (!decoded) return null;
return decoded.uid;
Expand Down
1 change: 0 additions & 1 deletion src/hooks/index.ts

This file was deleted.

7 changes: 7 additions & 0 deletions src/schema/authChecker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { AuthChecker } from "type-graphql";
import { Context } from "./context";

export const authChecker: AuthChecker<Context> = ({ context }) => {
const { uid } = context;
return !!uid;
};
10 changes: 10 additions & 0 deletions src/schema/context.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { PrismaClient } from "../prisma";

export interface Context {
uid: string | null;
prisma: PrismaClient;
}

export interface AuthorizedContext extends Context {
uid: string;
}
16 changes: 16 additions & 0 deletions src/schema/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { buildSchemaSync, Resolver, Query } from "type-graphql";
import { authChecker } from "./authChecker";

@Resolver()
class DummyResolver {
@Query((_returns) => String)
hello() {
return "Hello world";
}
}

export const schema = buildSchemaSync({
resolvers: [DummyResolver],
emitSchemaFile: process.env.NODE_ENV === "development",
authChecker,
});
Loading

1 comment on commit 9fd3e01

@vercel
Copy link

@vercel vercel bot commented on 9fd3e01 Feb 2, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.