Comments (10)
Hey @addstar34
On the client
you can also use the client.query
method, this returns an observable with an extra method named toPromise
, I think that might help the executeUrqlMethod
out:
const { data } = await urqlClient
.query(myUserQuery, variables, { requestPolicy: 'cache-only' })
.toPromise();
What do you mean with:
In withUrqlClient HOC it would be nicer if we put urqlClient on appCtx
It's on ctx
, I'm probably misunderstanding the question
from next-urql.
Hey @addstar34
On the
client
you can also use theclient.query
method, this returns an observable with an extra method namedtoPromise
, I think that might help theexecuteUrqlMethod
out:const { data } = await urqlClient .query(myUserQuery, variables, { requestPolicy: 'cache-only' }) .toPromise();
Oh this is nice and means I won't need that executeUrqlMethod
. I just tested it out and while it works setting the request policy doesn't seem to work. I set the requestPolicy as cache-only
but the request is still hitting the network, any ideas why that might be the case?
What do you mean with:
In withUrqlClient HOC it would be nicer if we put urqlClient on appCtx
It's on
ctx
, I'm probably misunderstanding the question
In _app.js
I have to do const ctxWithUrql = { ...ctx, ctx: { ...ctx.ctx, urqlClient: ctx.urqlClient } }
to make urqlClient available in other pages getInitialProps
but it looks like @parkerziegler has a fix for this coming in #38
from next-urql.
I just ran the tests and it does seem to set the requestPolicy
correctly on the Operation
, was this network request the case before as well? If so we might just be missing a filter
in one of our exchanges.
from next-urql.
Do you mean was it the case before when I was using my executeUrqlQuery
fn? If I use that function as per my first post and set cache-only
yeah it still hits the network and thats why I thought I was setting it incorrectly.
Doing it as you suggested this is the code I ran:
const response = await urqlClient
.query(`{ users { email } }`, {}, { requestPolicy: 'cache-only' })
.toPromise()
console.log('response', response)
and here you can see the request hits the network:
from next-urql.
Yes we're probably missing a filter somewhere. This is a bug on urql
itself
from next-urql.
oh wow I'm glad we spotted that. Do you need me to do anything to get this over to the urql
repo? I see you work there 😄
from next-urql.
@addstar34 I'll pick this up, made a draft PR. Thank you so much for notifying us!
from next-urql.
no prob, thanks for your help @JoviDeCroock
from next-urql.
Got another issue which I think might be related to this.
When I do the same query in getInitialProps
and in the Component I get an error ReactDOMServer does not yet support Suspense.
Here's my code:
// authenticatedPage.js
import React, { useEffect } from 'react'
import Router from 'next/router'
function authenticatedPage(WrappedComponent) {
const authenticatedPageWrapper = ({ redirect, ...rest }) => {
useEffect(() => {
if (redirect) {
Router.replace('/sign-in')
}
}, [])
if (redirect) {
return null
}
return <WrappedComponent {...rest} />
}
authenticatedPageWrapper.getInitialProps = async ctx => {
const { urqlClient } = ctx
const { data, error } = await urqlClient
.query(`{ users { id, email } }`, {}, { requestPolicy: 'cache-first' })
.toPromise()
if (error || !data?.users) {
return { redirect: true }
}
let pageProps
if (typeof WrappedComponent.getInitialProps === 'function') {
pageProps = await WrappedComponent.getInitialProps.call(
WrappedComponent,
ctx
)
}
return { ...pageProps }
}
return authenticatedPageWrapper
}
export default authenticatedPage
// pages/account.js
function Account() {
const [result] = useQuery({
query: `{ users { id, email } }`,
requestPolicy: 'cache-only',
})
if (result.fetching) return <div>fetching</div>
if (result.error) return <div>error fetching</div>
const { data: { users: [{ id, email }] } } = result;
return (
<div>
<h1>Account ID: {id} Email: {email}</h1>
</div>
)
}
export default authenticatedPage(Account)
As you can see I do this same query { users { id, email } }
in both authenticatedPageWrapper.getInitialProps
and within the page account
. If I remove the email from the query in authenticatedPageWrapper.getInitialProps
but leave it in account
then the error is gone.
from next-urql.
@addstar34 if you're still struggling with second issue with duplicate queries, do you mind writing it up in a new issue description over in the urql
monorepo? It's an interesting bug and one I'd love to see a minimal reproduction in a CodeSandbox for. Thanks!
from next-urql.
Related Issues (20)
- SyntaxError: Unexpected token * HOT 1
- useSubscription hook usage example HOT 2
- Official ReasonML binding HOT 2
- Best Practice to access urqlClient in getInitialProps HOT 2
- Client options not updated such as fetchOptions HOT 12
- Update examples to use TypeScript. HOT 1
- Upgrade Next types HOT 1
- Usage Questions HOT 4
- TypeError: Cannot read property 'replace' of undefined HOT 1
- How to set res.statusCode? HOT 2
- Error when wrapping withUrqlClient with another HOC HOT 3
- cacheExchange with next-urql HOT 1
- Why graphQLErrors always empty array? HOT 1
- runtime error with [email protected] + react-ssr-prepass HOT 5
- pages getInitialProps no longer gets called HOT 4
- Bug: pageProps doesn't pass into Component when custom _app.js HOT 3
- Incompatible with URQL >=1.9 HOT 2
- Typings seem to be broken in 0.3.0 release HOT 2
- Update withUrqlClient typings to reference new NextUrqlClientConfig type HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next-urql.