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

with-apollo #8770

Closed
tstenel opened this issue Sep 17, 2019 · 3 comments
Closed

with-apollo #8770

tstenel opened this issue Sep 17, 2019 · 3 comments

Comments

@tstenel
Copy link

tstenel commented Sep 17, 2019

Bug report

I have an initiator used to set a subscription that needs to be initiated before any pageComponent loading, depending on API authentication, but the last update made it so that I can't use my initiator where it was placed before (in _app.js).
I would like to access apollo client in my Initiator (that is not a page)

Describe the bug

apollo-client isn't accessible before _app.js, causing errors like

TypeError: _useSubscription.data is undefined

and

Error while running `getDataFromTree` { Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: undefined

To Reproduce

Try and set subscriptions in a component called before ApolloProvider in _app.js

old _app.js exemple :

 <Container>
   <ApolloProvider client={apolloClient}>
     <Initiator>
       <Content />        
    </Initiator>
  </ApolloProvider>
</Container>

with initiator not being a page component

now, since i can't use withApollo on my Initiator component, i cannot access apollo within it, and i can't set my subscriptions.

Expected behavior

Be able to access apollo in components higher than a page

Be able to set subscriptions in the Initiator

System information

  • OS: Linux
  • Browser: all
  • Version of Next.js: last
@kachkaev
Copy link
Contributor

What if you move your Initiator's logic into initApolloClient function?

@lfades
Copy link
Member

lfades commented Oct 7, 2019

@tstenel You don't need _app to do this, a better alternative is to create a Higher Order Component (HOC) for the pages that require the subscription, then create the subscription inside useEffect and then save the subscription into window, that way you can re-use the subscription if the page changes.

A bare bones example of what I mean:

const withSubscription = Page => props => {
  useEffect(() => {
    if (!window.subscription) {
      window.subscription = props.apollo.subscription()
    }
  }, [])

  return <Page {...props} />
}

// Then in some page
export default withSubscription(Page)

@lfades lfades closed this as completed Oct 7, 2019
@balazsorban44
Copy link
Member

This issue has been automatically locked due to no recent activity. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

@vercel vercel locked as resolved and limited conversation to collaborators Jan 31, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants