Git Product home page Git Product logo

Comments (13)

ellioseven avatar ellioseven commented on July 18, 2024

Just to add to this, If I do something like:

static async getInitialProps ({ apolloClient }) {
  await apolloClient.writeQuery({
    query: MUTATION_WRITE_FACET
  })
  return {}
}

I'll get:

Error writing result to store for query: mutation AddFacet { AddFacet @client } Cannot read property 'AddFacet' of undefined

from next-with-apollo.

lfades avatar lfades commented on July 18, 2024

It should be possible to query and mutate local state inside getInitialProps, I'm personally doing it but with the API, I think you should not see any change with Apollo dev tools, except for client side navigations, that's because for the first render there's no browser instance so Apollo dev tools will not be aware of what happens in getInitialProps, as a personal recommendation try moving queries and mutations out of getInitialProps, for most cases you should only use the apollo client there to do requests that will decide if the page should be rendered or just do a redirect or show an error, something like that.

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

I tried to avoid using the dev tools by using the Query component:

<Query query={ QUERY_GET_FACET }>
  { ({ data }) => (
    <p>{ data.facet.foo }</p>
  ) }
</Query>

However this doesn't seem to change.

My use case is that I want to build a store of breadcrumbs that can be built dynamically, which doesn't really make sense to be used in some kind of container component.

...I'm personally doing it but with the API...

I'm not entirely sure what this means?

from next-with-apollo.

lfades avatar lfades commented on July 18, 2024

It means that I'm using the apollo client inside getInitialProps but not for apollo-link-state - @client.
You can also use the graphql() HOC instead, will that work for you ?

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

HOC won't work as I need to build data dynamically. In redux, this was achieved by dispatching various redux actions in the initial props, which I was hoping to achieve with the apollo client. I'm not sure if this is related to apollographql/apollo-link-state#185

from next-with-apollo.

lfades avatar lfades commented on July 18, 2024

I just read that issue, in short words apollo-link-state doesn't supports SSR, this comment can be a solution.

I didn't know about that issue with apollo-link-state, but I don't think that ignoring the queries only for SSR is a bad idea, seems like a good solution, at least for the meantime until someone tackles the issue.

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

Unfortunately I need SSR for SEO purposes :( So ignoring the query is not an option for me. Anyway, I don't think this is an issue with this project, so I will close this ticket then hope and pray for apollographql/apollo-link-state#185

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

Just to quickly add, it looks like writeData will work in getIntialProps:

apolloClient.writeData({
  data: {
    breadcrumbs: ["one", "two", "three"]
  }
})

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

I am reopening as I have had some progress:

static async getInitialProps ({ apolloClient }) {
  apolloClient.mutate({
    mutation: MUTATION_WRITE_FACET
  })
  return {}
}
<Query query={ QUERY_GET_FACET }>
  { ({ data }) => {
    console.log(data.facet.foo)
    return <p>Hello</p>
  } }
</Query>

My console shows me:

node_1  | baasdz
node_1  | null

It might be that my page is rendering twice for some reason AND the default cache is overriding the built cache.

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

After removing defaults from the client, I have to do something like so, and then things seem to work correctly:

static async getInitialProps ({ apolloClient }) {
  let facet
  
  try {
    apolloClient.readQuery({ query: QUERY_GET_FACET })
  } catch (e) {
    apolloClient.writeData({
      data: {
        facet: {
          __typename: "Facet",
          foo: null
        }
      }
    })
  }

  apolloClient.mutate({
    mutation: MUTATION_WRITE_FACET
  })

  return {}
}

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

Related?

from next-with-apollo.

lfades avatar lfades commented on July 18, 2024

For SSR all changes to the cache will happen to a cache in the server, only the state of that ApolloClient is sent to the browser, and then a new ApolloClient is created (see this line) using that state, that's how we get the SSR for apollo queries (and by using getDataFromTree).

The double render is because of an issue with react-apollo. I managed to get to the root of the problem some months ago (here), but I didn't have enough time to build a solution, it was more complicated than what I thought.

from next-with-apollo.

ellioseven avatar ellioseven commented on July 18, 2024

I can't thank you enough @lfades for your help. Thank you so much for your hard work. I'm going to close this issue in favour of the upstream issue you linked: #25

from next-with-apollo.

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.