@brainly/use-suspense-query
TypeScript icon, indicating that this package has built-in type declarations

1.0.3 • Public • Published

Apollo Client - useSuspenseQuery

How to use

$ npm install @brainly/use-suspense-query

To use hook in your app you have to provide apollo client via. SuspenseQueryProvider i.e:

import { SuspenseQueryProvider } from 'use-suspense-query';

const apolloClient = new ApolloClient({
  link,
  cache
});

export function App() {
  return (
    <SuspenseQueryProvider client={apolloClient}>
        <div>
          <OtherComponent />
        </div>
    </SuspenseQueryProvider>
  );
}

Now you're able to use hook in your app. Example usage:

import { qgl } from '@apollo/client';
import { useSuspenseQuery } from 'use-suspense-query';

const myQuery = gql`
  query getSth($id: ID!) {
    data(id: $id) {
      sth
    }
  }
`

function DataComponent() {
  const data = useSuspenseQuery(myQuery, {
    variables: {
      id: '1234'
    }
  });

  return (<div>display {data} here</div>);
}

export function App() {
  return (
    <Suspense fallback={...YourFallbackComponent}>
      <DataComponent />
    </Suspense>
  )
}

Hook API

useSuspenseQuery(query: GraphqlQuery, options: Options): Response of ApolloClient.query()

Hook Options

Option Description Type
variables query variables Object
fetchPolicy fetch policy option passing to ApolloClient fetchPolicy field String
uniqueKey In default that key is generated based on variables and query, but to be sure that your request is totally unique, you should pass this field with some unique value - recommended to add uniqueKey! String

Readme

Keywords

none

Package Sidebar

Install

npm i @brainly/use-suspense-query

Weekly Downloads

1

Version

1.0.3

License

MIT

Unpacked Size

10.8 kB

Total Files

14

Last publish

Collaborators

  • thomsa
  • krzotki
  • przytrzask
  • dawidel
  • tlukasik
  • andrzej_sala
  • jakpaw
  • katarzynatobis
  • bartoszlorek
  • brainly-user
  • sebastian.czoch
  • the_clx
  • dominika_p
  • matzimowski
  • elzbieta
  • malinicz
  • wro
  • fasosnql
  • patrycja.batko