Skip to content

Instantly share code, notes, and snippets.

View AugustoCalaca's full-sized avatar

Augusto Calaca AugustoCalaca

View GitHub Profile
@sibelius
sibelius / getObjectId.tsx
Created April 22, 2020 11:29
getObjectId from globalId
import { fromGlobalId } from 'graphql-relay';
import { Model, Types } from 'mongoose';
// returns an ObjectId given an param of unknown type
export const getObjectId = (target: string | Model<any> | Types.ObjectId): Types.ObjectId | null => {
if (target instanceof Types.ObjectId) {
return new Types.ObjectId(target.toString());
}
if (typeof target === 'object') {
@sibelius
sibelius / usePageView.tsx
Created April 20, 2020 14:58
usePageView using react-ga
import { usePrevious } from '@app/hooks';
import { useEffect } from 'react';
import ReactGA from 'react-ga';
import { useLocation } from 'react-router-dom';
export const usePageView = () => {
const location = useLocation();
const lastLocation = usePrevious(location);
@tcodes0
tcodes0 / react-native-0.62-upgrade-tips.txt
Last active March 31, 2020 12:17
react-native 0.62 upgrade tips
BLOCKERS:
If you use react-native-fbads, it doesn't build on android 0.62.
Either wait fix or remove.
I've openned an issue already: https://github.com/callstack/react-native-fbads/issues/244
Some libs are also giving the Error "Super expression must either be null or a function"
You should open an issue and look for a PR that fixes it, or fix it yourself.
Example of a PR fixing this issue: https://github.com/expo/react-native-action-sheet/pull/162
To install a dep from a github PR, add to package.json:
@sibelius
sibelius / testingLibraryRelay.tsx
Created March 25, 2020 15:50
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';
@sibelius
sibelius / _usage.tsx
Created March 19, 2020 20:16
timestamps 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,
},
@gsasouza
gsasouza / useAuth.js
Created March 2, 2020 21:25
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';
@sibelius
sibelius / createLoader.tsx
Created February 26, 2020 18:53
createLoader used on react europe relay workshop
// eslint-disable-next-line
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 { validateContextUser } from './validateContextUser';
import { withConnectionCursor } from './withConnectionCursor';
@sibelius
sibelius / useAuth.tsx
Created February 26, 2020 14:46
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
@sibelius
sibelius / initEnvironment.tsx
Created February 26, 2020 12:32
Relay Environment to work well in SSR
let relayEnvironment = null;
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
@Andy-set-studio
Andy-set-studio / Heading.js
Created February 17, 2020 09:54
Heading React Component
import React from 'react'
import PropTypes from 'prop-types'
const Heading = ({children, cssClass, level}) => {
const Tag = `h${level}`
return <Tag className={cssClass}>{children}</Tag>
}
Heading.propTypes = {