Skip to content

Instantly share code, notes, and snippets.

View AugustoCalaca's full-sized avatar

Augusto Calaca AugustoCalaca

View GitHub Profile
import { useNavigationParam } from 'react-navigation-hooks';
import { graphql, usePreloadedQuery } from 'react-relay/hooks';
const query = graphql`
query TaskDetailQuery($nodeId: ID!) {
task: node(id: $nodeId) {
... on Task {
title
}
}
@AugustoCalaca
AugustoCalaca / defer-array.md
Created December 31, 2019 17:05 — forked from robrichard/defer-array.md
Relay Compatible `@defer` & `@stream`

Using @defer under an array field will return multiple patches. Patches are unique combinations of label and path

{
  items {
    id
    ...frag @defer(label: "my-label")
  }
}
@AugustoCalaca
AugustoCalaca / useAuth.tsx
Created February 26, 2020 17:34 — forked from sibelius/useAuth.tsx
useAuth hook to handle authentication, use @inline to consume Relay/GraphQL data outside components
import { useEffect } from 'react';
import { graphql, readInlineData } from 'react-relay';
import { useHistory } from '../routing/useHistory';
import { useAuth_user } from './__generated__/useAuth_user.graphql';
const useAuthFragment = graphql`
fragment useAuth_user on User @inline {
id
@AugustoCalaca
AugustoCalaca / useAuth.js
Created March 5, 2020 21:51 — forked from gsasouza/useAuth.js
Relay hook to handle authenticantion
import { ROOT_ID } from 'relay-runtime';
import { useRelayEnvironment } from 'react-relay/hooks';
import { useLocation, useHistory } from 'react-router-dom';
import { commitLocalUpdate } from 'react-relay'
import { useMutation } from 'relay-hooks/lib';
import { AuthUserMutation } from 'mutations/AuthUserMutation';
export const TOKEN_KEY = 'KEY';
@AugustoCalaca
AugustoCalaca / testingLibraryRelay.tsx
Created March 25, 2020 18:37 — forked from sibelius/testingLibraryRelay.tsx
how to properly test relay mutation with testing library
it('should call mutation properly', async () => {
// eslint-disable-next-line
const { debug, getByText, getByTestId } = render(<MyComponent />);
const customMockResolvers = {
...mockResolvers,
};
const name = 'myName';
@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 / 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 / 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,