Giter Site home page Giter Site logo

Comments (10)

JoviDeCroock avatar JoviDeCroock commented on August 18, 2024

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.

addstar34 avatar addstar34 commented on August 18, 2024

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();

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.

JoviDeCroock avatar JoviDeCroock commented on August 18, 2024

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.

addstar34 avatar addstar34 commented on August 18, 2024

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:

image

from next-urql.

JoviDeCroock avatar JoviDeCroock commented on August 18, 2024

Yes we're probably missing a filter somewhere. This is a bug on urql itself

from next-urql.

addstar34 avatar addstar34 commented on August 18, 2024

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.

JoviDeCroock avatar JoviDeCroock commented on August 18, 2024

@addstar34 I'll pick this up, made a draft PR. Thank you so much for notifying us!

from next-urql.

addstar34 avatar addstar34 commented on August 18, 2024

no prob, thanks for your help @JoviDeCroock

from next-urql.

addstar34 avatar addstar34 commented on August 18, 2024

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.

parkerziegler avatar parkerziegler commented on August 18, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.