Skip to content

Instantly share code, notes, and snippets.

View alfari16's full-sized avatar
🏠
Working from home

Alif Irfan Anshory alfari16

🏠
Working from home
View GitHub Profile
@alfari16
alfari16 / worker.js
Created January 18, 2025 01:41
cloudflare worker early hints implementation
export default {
async fetch(request, env) {
switch (request.method) {
case 'HEAD':
case 'GET':
const url = new URL(request.url);
const key = decodeURIComponent(url.pathname);
// replace suffix / after concating cause raw key still needed for regex
let objKey = key.replace(/\/+$/gm, '');
// resolvers.ts
// ...other codes
export default {
// ...other codes
Merchant: {
products({id: merchantId}: Merchant, {pagination: input}: {pagination: IPagination}, {loaders}: IGraphQLSchemaContext) {
const paginationCollector = {
limit: input?.limit || 10,
// graphql.ts
// ...other codes
const loaders = () => {
return {
// ...other loaders
getProductsByMerchantId(pagination: IPagination){
const {limit, offset, orderBy, sortBy, search} = pagination;
return new Dataloader(async merchantIds => {
@alfari16
alfari16 / graphql.ts
Last active December 14, 2020 16:51
// graphql.ts
// ...other codes
enum PaginationSortByEnum {
ASC = 'asc',
DESC = 'desc'
}
interface IPagination {
#typeDefs.graphql
type Query{
allMerchants: [Merchant!]!
}
type Merchant {
id: ID!
name: String!
products(pagination: PaginationInput!): [Product!]!
// resolvers.ts
import Knex from 'knex';
import {IGraphQLSchemaContext, Merchant} from '../../types';
export default {
Query: {
allMerchants() {
return Knex(config.get('database'))('merchants');
},
// graphql.ts
import 'graphql-import-node';
import {makeExecutableSchema} from 'graphql-tools';
import {ApolloServer} from 'apollo-server-express';
import Knex from 'knex';
import Dataloader from 'dataloader';
import config from 'config';
import resolvers from './resolvers';
import * as typeDefs from './typeDefs.graphql';
#typeDefs.graphql
type Query{
allMerchants: [Merchant!]!
}
type Merchant {
id: ID!
name: String!
products: [Product!]!
const router = require('express').Router();
const { Product } = require('../models/')
router.post('order', (req, res) => {
try {
let outOfStock = {
bool: false,
data: []
}
let allProduct = await Product.findAll({
const router = require('express').Router()
const kue = require('kue')
const queue = kue.createQueue()
queue.process('order', async (job, done) => {
try {
let outOfStock = {
bool: false,
data: [],
}