Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(react): useFragment hook (BETA) #3570

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/with-defer-stream-directives/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const cache = cacheExchange({
});

const client = new Client({
suspense: true,
url: 'http://localhost:3004/graphql',
exchanges: [cache, fetchExchange],
});
Expand Down
21 changes: 13 additions & 8 deletions examples/with-defer-stream-directives/src/Songs.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { gql, useQuery } from 'urql';
import React, { Suspense } from 'react';
import { gql, useQuery, useFragment } from 'urql';

const SecondVerseFragment = gql`
fragment secondVerseFields on Song {
Expand All @@ -13,9 +13,6 @@ const SONGS_QUERY = gql`
firstVerse
...secondVerseFields @defer
}
alphabet @stream(initialCount: 3) {
char
}
}

${SecondVerseFragment}
Expand All @@ -25,11 +22,22 @@ const Song = React.memo(function Song({ song }) {
return (
<section>
<p>{song.firstVerse}</p>
<Suspense fallback={'Loading song 2...'}>
<DeferredSong data={song} />
</Suspense>
<p>{song.secondVerse}</p>
</section>
);
});

const DeferredSong = ({ data }) => {
JoviDeCroock marked this conversation as resolved.
Show resolved Hide resolved
const result = useFragment({
query: SecondVerseFragment,
data,
});
return <p>{result.secondVerse}</p>;
};

const LocationsList = () => {
const [result] = useQuery({
query: SONGS_QUERY,
Expand All @@ -42,9 +50,6 @@ const LocationsList = () => {
{data && (
<>
<Song song={data.song} />
{data.alphabet.map(i => (
<div key={i.char}>{i.char}</div>
))}
</>
)}
</div>
Expand Down
1 change: 1 addition & 0 deletions packages/react-urql/src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './useMutation';
export * from './useQuery';
export * from './useFragment';
export * from './useSubscription';
Loading