Skip to content

Instantly share code, notes, and snippets.

View AugustoCalaca's full-sized avatar

Augusto Calaca AugustoCalaca

View GitHub Profile
@AugustoCalaca
AugustoCalaca / _document.tsx
Created July 21, 2020 17:41
Styled Components and Material UI working on server side with nextjs
import React from 'react';
import { ServerStyleSheet } from 'styled-components';
import { ServerStyleSheets } from '@material-ui/core/styles';
import Document, { Html, Head, Main, NextScript } from 'next/document';
class MyDocument extends Document {
static async getInitialProps(context) {
const styledComponentsSheet = new ServerStyleSheet();
const materialSheets = new ServerStyleSheets();
const originalRenderPage = context.renderPage;
@AugustoCalaca
AugustoCalaca / withData.tsx
Created July 13, 2020 03:07
inject query and variables to relay fectQuery on next ssr
import React from 'react';
import { NextPage } from 'next';
import { DocumentContext } from 'next/document';
import { RelayEnvironmentProvider, fetchQuery } from 'react-relay/hooks';
import { GraphQLTaggedNode, Variables } from 'relay-runtime';
import { initEnvironment } from './createEnvironment';
type OptionsWithData = {
query: GraphQLTaggedNode;
@AugustoCalaca
AugustoCalaca / initEnvironment.tsx
Created June 29, 2020 03:16 — forked from sibelius/initEnvironment.tsx
initEnvironment to be used for SSR
export const initEnvironment = (records = {}) => {
const network = Network.create(cacheHandler);
const source = new RecordSource(records);
const store = new Store(source, {
// This property tells Relay to not immediately clear its cache when the user
// navigates around the app. Relay will hold onto the specified number of
// query results, allowing the user to return to recently visited pages
// and reusing cached data if its available/fresh.
gcReleaseBufferSize: 10,
// Class
componentWillReceiveProps(nextProps) {
if (nextProps.count !== this.props.count) {
console.log('count changed', nextProps.count);
}
}
// Hooks
// Skipping first iteration exactly like componentWillReceiveProps:
@AugustoCalaca
AugustoCalaca / UserAddedSubscription.ts
Created June 13, 2020 19:26
A graphql subscription using the lib graphql-relay-subscription
import { subscriptionWithClientId } from 'graphql-relay-subscription';
import UserType from '../UserType';
import * as UserLoader from '../UserLoader';
import pubSub, { EVENTS } from '../../../channels/pubSub';
const UserAddedSubscription = subscriptionWithClientId({
name: 'UserAdded',
inputFields: {},
subscribe: () => pubSub.asyncIterator(EVENTS.USER.ADDED),
@AugustoCalaca
AugustoCalaca / UserAddedSubscription.spec.ts
Last active March 22, 2023 23:43
An example of how to test graphql subscriptions with jest
import { graphql, subscribe, parse } from 'graphql';
import {
connectMongoose,
disconnectMongoose,
clearDbAndRestartCounters,
getContext,
} from '../../../../../test/helpers';
import { schema } from '../../../../schema/schema';
@AugustoCalaca
AugustoCalaca / UserEditNameMutation.ts
Created May 18, 2020 17:26 — forked from sibelius/UserEditNameMutation.ts
createEditSingleFieldMutation function that generate an edit single field mutation for a given model
const mutation = createEditSingleFieldMutation({
name: 'UserEditName',
model: User,
fieldName: 'title',
fieldType: GraphQLString,
clearCache: UserLoader.clearCache,
notFoundMessage: ({ t }) => t('User not found'),
successMessage: ({ t }) => t('User edited successfully'),
outputFields: {
user: {
@AugustoCalaca
AugustoCalaca / getObjectId.spec.ts
Last active May 1, 2020 02:31 — forked from sibelius/getObjectId.tsx
getObjectId from globalId
import { toGlobalId } from 'graphql-relay';
import { Types } from 'mongoose';
import { getObjectId } from './getObjectId';
import {
clearDbAndRestartCounters,
connectMongoose,
createUser,
disconnectMongoose
} from '../../../test/helpers';
@AugustoCalaca
AugustoCalaca / registerTypeLoader.ts
Last active April 5, 2020 02:16
register types loaders
export type DataLoaderKey = string | Types.ObjectId | Object;
export type Load = (context: GraphQLContext, id: DataLoaderKey) => any;
type TypeLoaders = {
[key: string]: {
type: GraphQLObjectType,
load: Load,
};
};
@AugustoCalaca
AugustoCalaca / registerDataLoaders.ts
Last active April 5, 2020 02:14
register dataloaders, get dataloaders and so on
export type GetLoader = () => DataLoader<string, any>;
type Dataloaders = {
[loader: string]: GetLoader;
};
const dataloaders: Dataloaders = {};
export const registerDataLoader = (name: string, getLoader: GetLoader) => {
dataloaders[name] = getLoader;
};