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

Getting cannot read property 'subscribeToMore' of undefined #6437

Closed
luco opened this issue Jun 13, 2020 · 15 comments
Closed

Getting cannot read property 'subscribeToMore' of undefined #6437

luco opened this issue Jun 13, 2020 · 15 comments

Comments

@luco
Copy link

luco commented Jun 13, 2020

Intended outcome:

subscribeToMore returning the subscription in every refresh.

Actual outcome:

subscribeToMore works on the first load of component. The time it refreshes or it's saved, it returns subscribeToMore as undefined.

How to reproduce the issue:

const QUERY = gql`
	  query {
	    getEventsSection(id: "trending") {
	      _id,
	      events 
	    }
	  }
	`;

	const SUBSCRIPTION = gql`
		subscription {
		  getEventsSection(id: "trending") {
		    _id,
		    events 
		  }
		}
	`;

	const { loading, error, data, subscribeToMore } = useQuery(QUERY);

	useEffect(() => {
		subscribeToMore({
			document: SUBSCRIPTION,
			updateQuery: (prev, {subscriptionData}) => {
				console.log(subscriptionData);
			}
		});
	}, [subscribeToMore])

Versions

System:
OS: macOS Mojave 10.14.5
Binaries:
Node: 10.20.1 - /usr/local/opt/node@10/bin/node
Yarn: 1.15.2 - /usr/local/bin/yarn
npm: 6.14.4 - /usr/local/opt/node@10/bin/npm
Browsers:
Chrome: 83.0.4103.97
Edge: 83.0.478.45
Firefox: 75.0
Safari: 13.1.1
npmPackages:
@apollo/react-hooks: ^3.1.5 => 3.1.5
apollo-boost: ^0.4.9 => 0.4.9
apollo-cache-inmemory: ^1.6.6 => 1.6.6
apollo-client: ^2.6.10 => 2.6.10
apollo-link: ^1.2.14 => 1.2.14
apollo-link-context: ^1.0.20 => 1.0.20
apollo-link-error: ^1.1.13 => 1.1.13
apollo-link-http: ^1.5.17 => 1.5.17
apollo-link-ws: ^1.0.20 => 1.0.20
apollo-upload-client: ^13.0.0 => 13.0.0
apollo-utilities: ^1.3.4 => 1.3.4

@luco
Copy link
Author

luco commented Jun 14, 2020

Probably related to apollographql/react-apollo#3931.

@sakulstra
Copy link

Facing this as well - might also be related to #6405 (and is still present in apollo v3.0.2)

@nbaua
Copy link

nbaua commented Aug 26, 2020

I am using this with Angular 10,
I tried with declaring

private query: QueryRef<any>; and assigned it as

this.query = this.apollo.watchQuery({
      query: GRAPHQL_GET_SOME_QUERY_DATA,
    });

This way the query reference becomes available to the subscriptions, However sometimes it results as

invariant.esm.js:29 Unhandled GraphQL subscription error Error: Cannot return null for non-nullable field Subscription.wishAdded

Do you guys found some solution?

@nbaua
Copy link

nbaua commented Sep 28, 2020

I was able to fix this:
#6437 (comment)
@nbaua

What was causing problem:
Code First Approach graphql required a type, specified that and the issue occurred when null data arrived.

What fixed the problem: (On server side - NestJs)

Subscription(returns => YOURTYPE, {
    name: 'SOMETYPENAME',
    defaultValue: null,
    nullable: true,
  })

@ajgassner
Copy link

ajgassner commented Nov 11, 2020

Same issue here. I think it has something to do with react fresh which is now the standard refresh mechanism in CRA 4.0.0

@chattling
Copy link

I'm getting this error with React 17.0.1 after removing "import React from 'react'". If I put the import of React back into code, it works. Looks like subscribeToMore does not support new jsx transform.

@detorresvc
Copy link

"react": "^17.0.1",
"@apollo/client": "^3.2.9",

`const subscribeToNewMessage = () => {
return subscribeToMore({
document: MESSAGE_ADDED,
updateQuery: (prev, { subscriptionData }) => {
if (!subscriptionData.data) return prev;

    const newFeedItem = subscriptionData.data.messageAdded;
    
    const isMe = +newFeedItem.user_id === +user_id
    if(isMe){
      setTimeout(() => {
        scrollToBottom()
      }, 100)
    }

    return {
      ...prev,
      roomMessages: {
        ...prev.roomMessages,
        messages: [
          ...prev.roomMessages.messages, 
          newFeedItem
        ]
      }
    }
  }
})

}`

`useEffect(() => {
subscribeToNewMessage()

}, [])`

Cannot read property 'subscribeToMore' of undefined

how to fix?

@chattling
Copy link

chattling commented Dec 2, 2020

@detorresvc I assume as part of upgrading to React 17 you removed "import React from 'react'". I found that subscribeToMore does not always support it. Try putting "import React from 'react'" back as a workaround for now.

@3axap4eHko
Copy link

Still facing the issue

Object.QueryData._this.obsSubscribeToMore [as subscribeToMore]
node_modules/@apollo/client/react/data/QueryData.js (83:55)

@mhaagens
Copy link

mhaagens commented Feb 2, 2021

Having the same issue, I'm already using the import React from "react" statement which doesn't seem to help.

@elisalimli
Copy link

elisalimli commented Feb 24, 2021

Can anybody fix it ?

@DPangerl
Copy link

DPangerl commented Mar 7, 2021

I had the same problem.
Instead of using useEffect i've wrapped it in an useLayoutEffect… this seems to work in my case.

@wilsonmfg
Copy link

switching to useLayoutEffect was my solution also

@ajgassner
Copy link

ajgassner commented Jul 28, 2021

should be fixed with apollo client 3.4.0 now: #7952 and #7399

@nbaua
Copy link

nbaua commented Jul 30, 2021

wow, that looks nice.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Feb 14, 2023
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