Skip to content

Commit

Permalink
Invalidate Store (#278)
Browse files Browse the repository at this point in the history
* chore: revert pull-request #255, and use invalidateStore instead

* move config to logout.mutation

Co-authored-by: Seyyed Morteza Moosavi <smmoosavi@users.noreply.github.com>
  • Loading branch information
c0m1t and smmoosavi committed May 17, 2020
1 parent d5fc4c6 commit 10be99a
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 74 deletions.
7 changes: 4 additions & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@ import { ConfirmProvider } from 'src/shared/confirm-provider';
import { CssBaseline, ThemeProvider } from '@material-ui/core';
import { GlobalStyles } from 'src/core/styles/GlobalStyles';
import { MDXProvider } from 'src/shared/mdx-provider/MDXProvider';
import { RelayEnvironmentProvider } from 'react-relay/hooks';
import { RtlSupportProvider } from 'src/core/rtl/RtlSupportProvider';
import { SnackbarProvider } from 'src/core/snackbar';
import { rtlTheme } from 'src/core/theme';

import { AppRouter } from './AppRouter';
import { RelayEnvironmnetContextProvider } from './relay/RelayEnvironmentContext';
import { environment } from './relay';

const App: React.FC = () => {
return (
<RelayEnvironmnetContextProvider>
<RelayEnvironmentProvider environment={environment}>
<RtlSupportProvider>
<ThemeProvider theme={rtlTheme}>
<GlobalStyles />
Expand All @@ -26,7 +27,7 @@ const App: React.FC = () => {
</SnackbarProvider>
</ThemeProvider>
</RtlSupportProvider>
</RelayEnvironmnetContextProvider>
</RelayEnvironmentProvider>
);
};

Expand Down
8 changes: 2 additions & 6 deletions src/containers/main/NavbarUser.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { IconButton, Theme, Typography, makeStyles } from '@material-ui/core';
import { Styles } from 'src/shared/types/Styles';
import { getUserLabel } from 'src/shared/utils/getUserLabel';
import { useAuthGuardUser } from 'src/core/auth';
import { useRelayEnvironmentReset } from 'src/relay';

import { useLogoutMutation } from './logout.mutation';

Expand All @@ -18,13 +17,10 @@ export function NavbarUser(props: Props) {
const classes = useStyles(props);

const logoutMutation = useLogoutMutation();
const resetEnvironment = useRelayEnvironmentReset();

const handleLogout = useCallback(() => {
return logoutMutation({ input: {} }).then(() => {
resetEnvironment();
});
}, [logoutMutation, resetEnvironment]);
return logoutMutation();
}, [logoutMutation]);

const user = useAuthGuardUser();

Expand Down
27 changes: 18 additions & 9 deletions src/containers/main/logout.mutation.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import graphql from 'babel-plugin-relay/macro';
import { useCallback } from 'react';
import { useMutation } from 'src/relay';

export const useLogoutMutation = () =>
useMutation(graphql`
mutation logoutMutation($input: LogoutMutationInput!) {
logout(input: $input) {
viewer {
me {
...AuthGuard_user
}
import { logoutMutation } from './__generated__/logoutMutation.graphql';

const mutation = graphql`
mutation logoutMutation($input: LogoutMutationInput!) {
logout(input: $input) {
viewer {
me {
...AuthGuard_user
}
}
}
`);
}
`;

export const useLogoutMutation = () => {
const logout = useMutation<logoutMutation>(mutation);
return useCallback(() => {
logout({ input: {} }, { updater: (store) => store.invalidateStore() });
}, [logout]);
};
12 changes: 12 additions & 0 deletions src/relay-runtime.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import 'relay-runtime';

declare module 'relay-runtime' {
interface RecordSourceSelectorProxy {
/**
* Globally invalidates the Relay store. This will cause any data that was written to the store before invalidation occurred to be
* considered stale, and will be considered to require refetch the next time a query is
* checked with `environment.check()`.
*/
invalidateStore(): void;
}
}
50 changes: 0 additions & 50 deletions src/relay/RelayEnvironmentContext.tsx

This file was deleted.

File renamed without changes.
6 changes: 1 addition & 5 deletions src/relay/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
export { commitMutation } from './helpers/commitMutation';
export { useMutation } from './helpers/useMutation';
export {
RelayEnvironmnetContextProvider,
useRelayEnvironmentReset,
RelayEnvironmentResetContext,
} from './RelayEnvironmentContext';
export { environment } from './environments';
2 changes: 1 addition & 1 deletion src/stories/decorators/RelayDecorator.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { FullPageSpinner } from 'src/shared/loading';
import { RelayEnvironmentProvider } from 'react-relay/hooks';
import { StoryFn } from '@storybook/addons';
import { StoryFnReactReturnType } from '@storybook/react/dist/client/preview/types';
import { environment } from 'src/relay';

import { StoryDummy } from './StoryDummy';
import { environment } from '../environment';

export const relayDecorator = () => (storyFn: StoryFn<StoryFnReactReturnType>) => (
<RelayEnvironmentProvider environment={environment}>
Expand Down

0 comments on commit 10be99a

Please sign in to comment.