Skip to content

Instantly share code, notes, and snippets.

View benawad's full-sized avatar

Ben Awad benawad

View GitHub Profile
app.use("/graphql", (req, res, next) => {
const startHrTime = process.hrtime();
res.on("finish", () => {
if (req.body && req.body.operationName) {
const elapsedHrTime = process.hrtime(startHrTime);
const elapsedTimeInMs =
elapsedHrTime[0] * 1000 + elapsedHrTime[1] / 1e6;
logger.info({
type: "timing",
{
"extends": "react-app",
"rules": {
"no-shadow": "warn"
}
}
module.exports = {
parser: "@typescript-eslint/parser", // Specifies the ESLint parser
extends: [
"plugin:@typescript-eslint/recommended" // Uses the recommended rules from the @typescript-eslint/eslint-plugin
],
parserOptions: {
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
sourceType: "module" // Allows for the use of imports
},
rules: {
CREATE TABLE "posts" (
"id" serial NOT NULL,
"title" character varying(255) NOT NULL UNIQUE,
"body" TEXT NOT NULL,
CONSTRAINT posts_pk PRIMARY KEY ("id")
) WITH (
OIDS=FALSE
);
import { MiddlewareFn } from "type-graphql";
import { redis } from "./redis";
import { MyContext } from "./types/MyContext";
const ONE_DAY = 60 * 60 * 24;
export const rateLimit: (limit?: number) => MiddlewareFn<MyContext> = (
limit = 50
) => async ({ context: { req }, info }, next) => {
const key = `rate-limit:${info.fieldName}:${req.ip}`;
import { MiddlewareFn } from "type-graphql";
import { redis } from "./redis";
import { MyContext } from "./types/MyContext";
const ONE_DAY = 60 * 60 * 24;
export const rateLimit: (limit?: number) => MiddlewareFn<MyContext> = (
limitForAnonUser = 50,
limitForUser = 100
) => async ({ context: { req }, info }, next) => {
import * as Sentry from "@sentry/node";
import imagemin from "imagemin";
import mozjpeg from "imagemin-mozjpeg";
import sharp from "sharp";
import isJpg from "is-jpg";
import * as aws from "aws-sdk";
import { Upload } from "../../types/graphqlUtils";
import { generateFilename } from "./generateFilename";
export const s3 = new aws.S3({
const someFunc = ({ context }: ResolverData) => {
(context as any).value2 = "another value";
};
const addToContext: MiddlewareFn = (data, next) => {
(data.context as any).value = "new value";
someFunc(data);
return next();
};
@benawad
benawad / Resolver.ts
Last active June 1, 2021 15:59
middleware role
@UseMiddleware(hasRole("admin"))
@Query(() => String)
async hello() {
return "Hello World!";
}
{
// Place your snippets for typescript here. Each snippet is defined under a snippet name and has a prefix, body and
// description. The prefix is what is used to trigger the snippet and the body will be expanded and inserted. Possible variables are:
// $1, $2 for tab stops, $0 for the final cursor position, and ${1:label}, ${2:another} for placeholders. Placeholders with the
// same ids are connected.
// Example:
"Type GraphQL Resolver": {
"prefix": "resolver",
"body": [
"import { Resolver } from \"type-graphql\";",