Skip to content

Instantly share code, notes, and snippets.

View AugustoCalaca's full-sized avatar

Augusto Calaca AugustoCalaca

View GitHub Profile
@AugustoCalaca
AugustoCalaca / createLoader.ts
Last active April 4, 2020 17:49 — forked from sibelius/createLoader.tsx
createLoader used on react europe relay workshop
import { mongooseLoader } from '@entria/graphql-mongoose-loader';
import DataLoader from 'dataloader';
import { ConnectionArguments } from 'graphql-relay';
import { Model, Types } from 'mongoose';
import { buildMongoConditionsFromFilters } from '@entria/graphql-mongo-helpers';
import { withConnectionCursor } from './withConnectionCursor';
const defaulViewerCanSee = (context, data) => {
@AugustoCalaca
AugustoCalaca / UserType.ts
Last active April 4, 2020 21:48 — forked from sibelius/_usage.tsx
timestamps and monooseIDResolver to be spread in any GraphQL Object Type
const UserType = new GraphQLObjectType<IUser, GraphQLContext>({
name: 'User',
description: 'User data',
fields: () => ({
id: globalIdField('User'),
...mongooseIDResolver,
name: {
type: GraphQLString,
resolve: user => user.name,
},
@AugustoCalaca
AugustoCalaca / nodeRegistry.ts
Created April 5, 2020 01:26
Node definitions
// How to return an object given its globalId
// Is used to take the ID argument of the node field and use it to resolve an object
export const idFetcher = async (globalId, context: GraphQLContext) => {
const { type, id } = fromGlobalId(globalId);
const data = await typesLoaders[type].load(context, id);
return data;
};
// How to return a type given an object
@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;
};
@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 / 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 / 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 / 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 / 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),
// Class
componentWillReceiveProps(nextProps) {
if (nextProps.count !== this.props.count) {
console.log('count changed', nextProps.count);
}
}
// Hooks
// Skipping first iteration exactly like componentWillReceiveProps: