Search code examples
react-nativegraphqlbabeljsrelaycreate-react-native-app

Error when using Relay + React Native (create-react-native-app): GraphQL validation error `Unknown Type "Viewer".`


I'm trying to set up a RN app (that was created with create-react-native-app) and connect it with Relay.

I've got the following setup in my App.js already:

import { StyleSheet, Text, View } from 'react-native'
import Relay, {
  Route,
  RootContainer,
} from 'react-relay'
import PokemonList from './components/PokemonList'

Relay.injectNetworkLayer(
  new Relay.DefaultNetworkLayer('https://api.graph.cool/relay/v1/ciyeih9590fhl0162e5zh1z4h', {
    headers: {
    },
  })
)

class IndexRoute extends Route {
  static queries = {
    viewer: () => Relay.QL`query { viewer }`
  }
  static routeName = 'IndexRoute'
}

export default class App extends React.Component {
  render() {
    return (
      <View style={styles.container}>
        <RootContainer
          Component={PokemonList}
          route={new IndexRoute()}
          renderFetched={(data) => <PokemonList {...this.props} {...data} />}
        />
      </View>
    )
  }
}

The PokemonList doesn't do anything so far expect for printing the viewer.id that I expect to be loaded from the GraphQL API:

import Relay from 'react-relay'

class PokemonList extends React.Component {

  render() {
    console.log(this.props.viewer.id)
    return (
      <View>
        <Text>Hi</Text>
      </View>
    )
  }

}

export default Relay.createContainer(PokemonList, {
  fragments: {
    viewer: () => Relay.QL`
      fragment on Viewer {
        id
      }
    `
  }
})

I'm using this babel plugin to make my schema available to Relay at build time.

It requires to provide a GraphQL endpoint using one of three methods. I configured it in a .graphqlrc file like so:

{
  "request": {
    "url": "https://api.graph.cool/relay/v1/ciyeih9590fhl0162e5zh1z4h",
    "headers": {
      "Authorization": "xxxxx"
    }
  }
}

And this is what my .babelrc looks like:

{
  "presets": ["babel-preset-expo"],
  "env": {
    "development": {
      "plugins": ["transform-react-jsx-source", "react-relay"]
    }
  }
}

However, when I'm running the app I'm getting this error:

GraphQL validation error Unknown Type "Viewer". in file .../PokemonList.js. Try updating your GraphQL schema if an argument/field/type was recently added.

So it seems to me that Relay doesn't get access to my schema at all. Can anyone tell what I'm missing in my setup or what I can do to further debug?

And just for reference if that helps anyone, here is what my package.json looks like:

{
  "name": "test",
  "version": "0.1.0",
  "private": true,
  "devDependencies": {
    "babel-plugin-react-relay": "^0.10.0",
    "jest-expo": "^0.3.0",
    "react-native-scripts": "0.0.25",
    "react-test-renderer": "~15.4.1"
  },
  "main": "./node_modules/react-native-scripts/build/bin/crna-entry.js",
  "scripts": {
    "start": "react-native-scripts start",
    "eject": "react-native-scripts eject",
    "android": "react-native-scripts android",
    "ios": "react-native-scripts ios",
    "test": "node node_modules/jest/bin/jest.js --watch"
  },
  "jest": {
    "preset": "jest-expo"
  },
  "dependencies": {
    "expo": "^15.1.0",
    "react": "~15.4.0",
    "react-native": "0.42.3",
    "react-relay": "^0.10.0"
  }
}

Solution

  • Ok, so the answer was rather simple, it seems my setup was already correct but I had to also delete the crna cache, so running rm -rf $TMPDIR/react* and restarting the iOS simulator actually already did it.