How to use relay-hooks - 10 common examples

To help you get started, we’ve selected a few relay-hooks examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github relay-tools / relay-hooks / examples / relay-hook-example / todo / js / mutations / RemoveCompletedTodosMutation.js View on Github external
function commit(
  environment: Environment,
  todos: Todos,
  user: TodoListFooter_user,
): Disposable {
  const input: RemoveCompletedTodosInput = {
    userId: user.userId,
  };

  return useMutation(mutation, {
    variables: {
      input,
    },
    updater: (store: RecordSourceSelectorProxy) => {
      const payload = store.getRootField('removeCompletedTodos');
      const deletedIds = payload.getValue('deletedTodoIds');

      // $FlowFixMe `payload.getValue` returns mixed, not sure how to check refinement to $ReadOnlyArray
      sharedUpdater(store, user, deletedIds);
    },
    optimisticUpdater: (store: RecordSourceSelectorProxy) => {
      // Relay returns Maybe types a lot of times in a connection that we need to cater for
      const completedNodeIds: $ReadOnlyArray = todos.edges
        ? todos.edges
            .filter(Boolean)
            .map((edge: Edge): ?Node => edge.node)
github relay-tools / relay-hooks / examples / relay-hook-example / todo / js / app.js View on Github external
const LayoutTodo = ({userId}) => {
  console.log('LayoutTodo', userId, isServer);
  const {props, error, retry, cached} = useQuery(
    QueryApp,
    {userId},
    {
      fetchPolicy: 'store-and-network',
    },
  );
  /*const { props, error, retry, cached } = useQuery({
    query: QueryApp,
    variables: {
      // Mock authenticated ID that matches database
      userId,
    },
    dataFrom: "STORE_THEN_NETWORK"
  });*/

  console.log('renderer', props, cached);
github morrys / react-relay-offline / src / hooks / useQueryOffline.ts View on Github external
...ref.current,
          rehydrated: true,
          error: null
        };
        const { props } = ref.current;
        ref.current = current;
        if (!props) forceUpdate(current);
      })
      .catch(error => {
        throw error; //
      });
  }

  const query = useMemoOperationDescriptor(gqlQuery, variables);

  const queryFetcher = useQueryFetcher();

  const { fetchPolicy, networkCacheConfig, ttl } = options;

  const { props, error, ...others } = queryFetcher.execute(
    environment,
    query,
    {
      networkCacheConfig,
      fetchPolicy:
        rehydrated && environment.isOnline() ? fetchPolicy : STORE_ONLY
    },
    (environment, query) => environment.retain(query.root, { ttl }) // TODO new directive
  );

  const current = {
    ...others,
github morrys / react-relay-offline / src / hooks / useQueryOffline.ts View on Github external
.then(() => {
        const current = {
          ...ref.current,
          rehydrated: true,
          error: null
        };
        const { props } = ref.current;
        ref.current = current;
        if (!props) forceUpdate(current);
      })
      .catch(error => {
        throw error; //
      });
  }

  const query = useMemoOperationDescriptor(gqlQuery, variables);

  const queryFetcher = useQueryFetcher();

  const { fetchPolicy, networkCacheConfig, ttl } = options;

  const { props, error, ...others } = queryFetcher.execute(
    environment,
    query,
    {
      networkCacheConfig,
      fetchPolicy:
        rehydrated && environment.isOnline() ? fetchPolicy : STORE_ONLY
    },
    (environment, query) => environment.retain(query.root, { ttl }) // TODO new directive
  );
github relay-tools / relay-hooks / examples / relay-hook-example / todo / js / components / TodoList.js View on Github external
const TodoList = (props) => {
  const { refetch, user } = props;
  //const { refetch } = props;
  //const user = useFragment(fragmentSpec, props.user);
  const  { todos, completedCount, totalCount, userId } = user;
  const [mutate] = useMutation(mutation);
  const handleMarkAllChange = (e: SyntheticEvent) => {
    const complete = e.currentTarget.checked;

    if (todos) {
      MarkAllTodosMutation.commit(mutate, complete, todos, user);
    }
  };

  const handlerRefetch = () => {
    const response = refetch(QueryApp,
      {userId: "you"},  
      null,  
      () => { console.log('Refetch done') },
      {force: true, fetchPolicy: "store-and-network"},  
    );
    //response.dispose();
github relay-tools / relay-hooks / examples / relay-hook-example / todo / js / components / TodoListFooter.js View on Github external
const TodoListFooter = props => {
  const [user, functions] = useOssFragment(fragmentSpec, props.user);
  const {todos, completedCount, totalCount} = user;
  const completedEdges: $ReadOnlyArray =
    todos && todos.edges
      ? todos.edges.filter(
          (edge: ?Edge) => edge && edge.node && edge.node.complete,
        )
      : [];

  const [mutate] = useMutation(mutation);
  const handleRemoveCompletedTodosClick = () => {
    RemoveCompletedTodosMutation.commit(
      mutate,
      {
        edges: completedEdges,
      },
      user,
    );
  };

  const numRemainingTodos = totalCount - completedCount;

  console.log('completedCount', completedCount);

  return (
    <footer></footer>
github morrys / offline-examples / relay / nextjs / components / Todo.tsx View on Github external
export const Todo = (props: Props) =&gt; {
  const [isEditing, setIsEditing] = useState(false);

  const {disabled} = props;
  const environment = useRelayEnvironment();
  const user = useFragment(fragmentSpecUser, props.user);
  const todo = useFragment(fragmentSpecTodo, props.todo);

  console.log('renderer');

  const handleCompleteChange = (e: SyntheticEvent) =&gt; {
    const complete = e.currentTarget.checked;
    ChangeTodoStatusMutation.commit(environment, complete, todo, user);
  };

  const handleDestroyClick = () =&gt; removeTodo();
  const handleLabelDoubleClick = () =&gt; {
    if (!disabled) setIsEditing(true);
  };
  const handleTextInputCancel = () =&gt; setIsEditing(false);

  const handleTextInputDelete = () =&gt; {
    setIsEditing(false);
github morrys / offline-examples / relay / nextjs / components / Todo.tsx View on Github external
export const Todo = (props: Props) =&gt; {
  const [isEditing, setIsEditing] = useState(false);

  const {disabled} = props;
  const environment = useRelayEnvironment();
  const user = useFragment(fragmentSpecUser, props.user);
  const todo = useFragment(fragmentSpecTodo, props.todo);

  console.log('renderer');

  const handleCompleteChange = (e: SyntheticEvent) =&gt; {
    const complete = e.currentTarget.checked;
    ChangeTodoStatusMutation.commit(environment, complete, todo, user);
  };

  const handleDestroyClick = () =&gt; removeTodo();
  const handleLabelDoubleClick = () =&gt; {
    if (!disabled) setIsEditing(true);
  };
  const handleTextInputCancel = () =&gt; setIsEditing(false);

  const handleTextInputDelete = () =&gt; {
github morrys / offline-examples / relay / nextjs / components / TodoList.tsx View on Github external
const TodoList = (props: Props) =&gt; {
  console.log("props", props);

  const environment = useRelayEnvironment();
  const user = useFragment(TodoList_user, props.user);
  const { todos, completedCount, totalCount, userId } = user;
  const handleMarkAllChange = (e: SyntheticEvent) =&gt; {
    const complete = e.currentTarget.checked;

    if (todos) {
      MarkAllTodosMutation.commit(environment, complete, todos, user);
    }
  };

  const nodes: ReadonlyArray =
    todos &amp;&amp; todos.edges
      ? todos.edges
          .filter(Boolean)
          .map((edge: any) =&gt; edge.node)
          .filter(Boolean)
      : [];
github morrys / offline-examples / relay / nextjs / components / Todo.tsx View on Github external
export const Todo = (props: Props) =&gt; {
  const [isEditing, setIsEditing] = useState(false);

  const {disabled} = props;
  const environment = useRelayEnvironment();
  const user = useFragment(fragmentSpecUser, props.user);
  const todo = useFragment(fragmentSpecTodo, props.todo);

  console.log('renderer');

  const handleCompleteChange = (e: SyntheticEvent) =&gt; {
    const complete = e.currentTarget.checked;
    ChangeTodoStatusMutation.commit(environment, complete, todo, user);
  };

  const handleDestroyClick = () =&gt; removeTodo();
  const handleLabelDoubleClick = () =&gt; {
    if (!disabled) setIsEditing(true);
  };
  const handleTextInputCancel = () =&gt; setIsEditing(false);