morrys / react-relay-offline

TypeScript library files for Relay Modern Offline
https://morrys.github.io/react-relay-offline/docs/react-relay-offline.html
MIT License
224 stars 15 forks source link

_this._store.setCheckGC is not a function #95

Closed akinncar closed 2 years ago

akinncar commented 2 years ago

I have this error in trying to follow the example of README with React Native.

 ERROR  TypeError: _this._store.setCheckGC is not a function. (In '_this._store.setCheckGC(function () {
        return _this.isOnline();
      })', '_this._store.setCheckGC' is undefined)

My Environment:

"react-native": "0.64.2",
"react-relay-offline": "^4.0.0",
"relay-hooks": "^5.0.0",
"@react-native-community/netinfo": "^6.0.2",
morrys commented 2 years ago

Hi @akinncar, can you send the configuration of the environment?

akinncar commented 2 years ago

it is something just like this:

import { RecordSource, Store } from 'relay-runtime'
import { Environment } from 'react-relay-offline'
import {
  RelayNetworkLayer,
  urlMiddleware,
  loggerMiddleware,
  errorMiddleware,
  perfMiddleware,
  authMiddleware,
  retryMiddleware,
  cacheMiddleware
} from 'react-relay-network-modern'
import AsyncStorage from '@react-native-community/async-storage'

import { Config } from './'
import { Constants } from './constants'

const source = new RecordSource()
const store = new Store(source)

const network = new RelayNetworkLayer([
  cacheMiddleware({
    size: 100, // max requests
    ttl: 5 * 1000
  }),
  urlMiddleware({
    url: req => Promise.resolve(`${Config.apiUrl}/api/graphql`),
    headers: req => ({
      Accept: 'application/json',
      'Accept-Encoding': 'gzip, deflate',
      'Content-Type': 'application/json'
    })
  }),
  authMiddleware({
    token: async () => AsyncStorage.getItem(Constants.settingsJwtToken)
  }),
  Config.environment === 'development' ? errorMiddleware() : null,
  retryMiddleware({
    fetchTimeout: 5000,
    retryDelays: attempt => Math.pow(2, attempt + 4) * 100, // or simple array [3200, 6400, 12800, 25600, 51200, 102400, 204800, 409600],
    beforeRetry: ({ forceRetry, abort, delay, attempt, lastError, req }) => {
      if (attempt > 2) abort('Retry requests failed')
    },
    statusCodes: [500, 503, 504]
  })
])

const manualExecution = false

const environment = new Environment({
  network,
  store
})
environment.setOfflineOptions({
  manualExecution // optional
})

export default environment
morrys commented 2 years ago

You need to import store and recordsource from react-relay-offline.

Look here: https://github.com/morrys/react-relay-offline#environment

akinncar commented 2 years ago

that's it! I ended up not realizing that this could the problem as I was using the store in another file, thank you