-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(packages/react): infinite fetch via stale proxy #1588
- Loading branch information
Showing
13 changed files
with
725 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
"extends": ["next", "next/core-web-vitals"] | ||
"extends": ["next/babel", "next/core-web-vitals"] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/** | ||
* GQty: You can safely modify this file based on your needs. | ||
*/ | ||
|
||
import { createLogger } from '@gqty/logger'; | ||
import { createReactClient } from '@gqty/react'; | ||
import type { QueryFetcher } from 'gqty'; | ||
import { Cache, GQtyError, createClient } from 'gqty'; | ||
import type { GeneratedSchema } from './schema.generated'; | ||
import { generatedSchema, scalarsEnumsHash } from './schema.generated'; | ||
|
||
const queryFetcher: QueryFetcher = async function ( | ||
{ query, variables, operationName }, | ||
fetchOptions | ||
) { | ||
// Modify "https://rickandmortyapi.com/graphql" if needed | ||
const response = await fetch('https://rickandmortyapi.com/graphql', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
query, | ||
variables, | ||
operationName, | ||
}), | ||
mode: 'cors', | ||
...fetchOptions, | ||
}); | ||
|
||
if (response.status >= 400) { | ||
throw new GQtyError( | ||
`GraphQL endpoint responded with HTTP ${response.status}: ${response.statusText}.` | ||
); | ||
} | ||
|
||
const text = await response.text(); | ||
|
||
try { | ||
return JSON.parse(text); | ||
} catch { | ||
throw new GQtyError( | ||
`Malformed JSON response: ${ | ||
text.length > 50 ? text.slice(0, 50) + '...' : text | ||
}` | ||
); | ||
} | ||
}; | ||
|
||
const cache = new Cache( | ||
undefined, | ||
/** | ||
* Default option is immediate cache expiry but keep it for 5 minutes, | ||
* allowing soft refetches in background. | ||
*/ | ||
{ | ||
maxAge: 0, | ||
staleWhileRevalidate: 5 * 60 * 1000, | ||
normalization: true, | ||
} | ||
); | ||
|
||
export const client = createClient<GeneratedSchema>({ | ||
schema: generatedSchema, | ||
scalars: scalarsEnumsHash, | ||
cache, | ||
fetchOptions: { | ||
fetcher: queryFetcher, | ||
}, | ||
}); | ||
|
||
if (typeof window !== 'undefined') { | ||
createLogger(client, {}).start(); | ||
} | ||
|
||
// Core functions | ||
export const { resolve, subscribe, schema } = client; | ||
|
||
// Legacy functions | ||
export const { | ||
query, | ||
mutation, | ||
mutate, | ||
subscription, | ||
resolved, | ||
refetch, | ||
track, | ||
} = client; | ||
|
||
export const { | ||
graphql, | ||
useQuery, | ||
usePaginatedQuery, | ||
useTransactionQuery, | ||
useLazyQuery, | ||
useRefetch, | ||
useMutation, | ||
useMetaState, | ||
prepareReactRender, | ||
useHydrateCache, | ||
prepareQuery, | ||
} = createReactClient<GeneratedSchema>(client, { | ||
defaults: { | ||
// Enable Suspense, you can override this option at hooks. | ||
suspense: false, | ||
}, | ||
}); | ||
|
||
export * from './schema.generated'; |
Oops, something went wrong.