How to use the @apollo/react-hooks.useApolloClient function in @apollo/react-hooks

To help you get started, we’ve selected a few @apollo/react-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 ian13456 / mine.js / src / components / Forms / LoginForm / LoginForm.js View on Github external
const LoginForm = ({ history, loading: authLoading }) => {
  const client = useApolloClient()

  const [login, { error, loading }] = useMutation(LOGIN_MUTATION, {
    onCompleted: data => {
      setCookie(data.login.token)

      // Force a reload of all current queries now that user is
      // logged in
      client.cache.reset().then(() => {
        history.push('/home')
      })
    },
    onError: err => console.error(err)
  })

  if (loading || authLoading) {
    return
github tlambert03 / FPbase / static / src / fpbase-spectra / useCachedQuery.jsx View on Github external
const useCachedQuery = (query, cacheKey, maxAge) => {
  // fetch data no more than once every maxAge
  const [stash, setStash] = useState(getStorageWithExpire(cacheKey, maxAge))
  const client = useApolloClient()
  useEffect(() => {
    async function fetchData() {
      const { data } = await client.query({ query })
      if (data) {
        setStash(data)
        setStorageWithTimeStamp(cacheKey, data)
      }
    }
    if (!stash) fetchData()
  }, []) // eslint-disable-line
  return stash
}
github hasura / learn-graphql / tutorials / frontend / typescript-react-apollo / app-final / src / components / Todo / TodoPublicList.tsx View on Github external
const TodoPublicList = (props: publicListProps) => {
  const [olderTodosAvailable, setOlderTodosAvailable] = useState(props.latestTodo ? true : false)
  const [newTodosCount, setNewTodosCount] = useState(0)
  const [error, setError] = useState(false);
  const [todos, setTodos] = useState([]);

  let oldestTodoId = useRef(props.latestTodo && props.latestTodo.id ? props.latestTodo.id + 1 : 0);
  let newestTodoId = useRef(props.latestTodo && props.latestTodo.id ? props.latestTodo.id : 0);
  if(todos && todos.length) {
    oldestTodoId.current = todos[todos.length - 1].id
    newestTodoId.current = todos[0].id;
  }

  const client = useApolloClient();

  const loadOlder = async () => {
    const GET_OLD_PUBLIC_TODOS = gql`
      query getOldPublicTodos($oldestTodoId: Int!) {
        todos(
          where: { is_public: { _eq: true }, id: { _lt: $oldestTodoId } }
          limit: 7
          order_by: { created_at: desc }
        ) {
          id
          title
          created_at
          user {
            name
          }
        }
github connect-foundation / 2019-02 / web / src / hooks / channel / chat / useInitChat.js View on Github external
const useInitChat = (channelId) => {
  const client = useApolloClient();
  const [loadChats, query] = useLazyQuery(GET_CHAT_LOGS, {
    variables: { channelId },
    fetchPolicy: 'no-cache',
  });
  const cleanChatCache = () => {
    client.writeQuery({
      query: GET_CHAT_CACHED,
      data: {
        chatLogs: {
          __typename: 'chatLogs',
          logs: [],
          changeType: null,
          sortType: CHAT_SORT_BY_RECENT,
        },
      },
    });
github guardian / editions / projects / Mallard / src / screens / weather-geolocation-consent-screen.tsx View on Github external
const WeatherGeolocationConsentScreen = ({
    navigation,
}: NavigationInjectedProps) => {
    const apolloClient = useApolloClient()
    const onConsentPress = async () => {
        const result = await requestLocationPermission(apolloClient)
        if (result === RESULTS.BLOCKED) {
            Alert.alert(
                'Location permission',
                'Location permission is blocked in the device ' +
                    'settings. Allow the app to access location to ' +
                    'see location-based weather.',
                [
                    {
                        text: 'OK',
                        onPress: () => {
                            Linking.openSettings()
                        },
                    },
                ],
github connect-foundation / 2019-03 / web / src / containers / App / index.js View on Github external
function App({ cookies }) {
  const client = useApolloClient();
  const myInfo = cookies.get('myInfo');
  client.writeData({
    data: {
      isLoggedIn: !!myInfo,
    },
  });
  const { data } = useQuery(IS_LOGGED_IN);

  return (
github helloncanella / useAppState / src / useAppState.js View on Github external
function useWriteQuery({ query, variables }) {
  const client = useApolloClient()
  const queryField = getQueryField(query)
  return function writeQuery(data) {
    client.writeQuery({
      query,
      ...(variables ? { variables } : {}),
      data: { [queryField]: data }
    })
  }
}