Skip to content

Instantly share code, notes, and snippets.

View PatrikValkovic's full-sized avatar

Patrik Valkovic PatrikValkovic

View GitHub Profile
export const newMessage = subscriptionField('newMessage', {
type: nonNull('Message'),
subscribe(_, __, ctx) {
return ctx.sub.subscribe(`message-${ctx.userId}`);
},
resolve(obj: NewMessageEvent) {
return obj.message;
},
});
export const handler = async (event: SQSEvent) => {
const body = JSON.parse(event.body);
const subscriptions = await subscriptionRepository.getByRoutingKey(body.routingKey);
for(subscription of subscriptions) {
const document = parse(subscription.subscription);
const subscriptionResult = await subscribe({
schema,
document,
variableValues: subscription.variables,
export const handler = async (event: APIGatewayEvent): Promise<APIGatewayResult> => {
const body = JSON.parse(event.body);
// ...
if (body.type === 'subscribe') {
const parsed = parse(body.payload.query);
const validated = validate(schema, parsed);
if (validated.length > 0)
await connection.send(event.requestContext.connectionId, { errors: validated });
await subscribe({
headers: {
'Connection': 'upgrade',
'Upgrade': 'websocket',
'Sec-WebSocket-Protocol': 'graphql-transport-ws',
'Sec-WebSocket-Accept': base64(
sha1(
headers['Web-Socket-Key'] + '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'
)
),
},
import { parse } from 'graphql/language';
import { execute } from 'graphql/execution';
import { validate } from 'graphql/validation';
import { schema } from '../graphql';
export const handler = async (event: APIGatewayEvent): Promise<ApiGatewayProxyResult> => {
try {
const body = JSON.parse(event.body || '');
const parsed = parse(body.query);
@PatrikValkovic
PatrikValkovic / RAII.h
Created February 27, 2020 22:23
Simple RAII (resource acquisition is initialization) paradigm implementation. Only for scalar types!
template<typename T>
class RAII
{
static_assert(std::is_scalar<T>::value, "RAII can handle only scalars");
private:
const T _resource;
std::function<void(T)> _destroy;
public:
RAII(std::function<void(T)> destroy, T resource) noexcept : _resource(resource), _destroy(destroy)
{}
// Example program
#include <iostream>
#include <string>
using namespace std;
template<typename T>
class A
{
public:
#!/usr/bin/env python
"""
:Author Patrik Valkovic
:Created 17.04.2019 12:35
:Licence MIT
Part of ML
"""
import math
from random import Random
@PatrikValkovic
PatrikValkovic / prime_numbers.cpp
Created November 18, 2018 22:13
Compute prime number by C++ compiler.
#include <iostream>
struct true_type {static const bool value = true;};
struct false_type {static const bool value = false;};
template<bool, bool ...Args>
struct and_ : false_type {};
@PatrikValkovic
PatrikValkovic / BiGrams.java
Last active September 26, 2018 21:16
Hadoop cartesian product using MapReduce
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.DoubleWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;