使用 Relay + React Native (create-react-native-app) 时出错:GraphQL 验证错误 `Unknown Type "Viewer".`

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

我正在尝试设置 RN 应用程序(使用 create-react-native-app 创建)并将其与 Relay 连接。

我的 App.js 中已经进行了以下设置:

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>
    )
  }
}

PokemonList 到目前为止没有做任何事情,除了打印我希望从 GraphQL API:

加载的 viewer.id
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
      }
    `
  }
})

我正在使用 this babel 插件使我的模式在构建时可用于 Relay。

它需要使用 three methods 之一提供 GraphQL 端点。我在 .graphqlrc 文件中这样配置它:

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

这就是我的 .babelrc 的样子:

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

但是,当我 运行 应用程序时,我收到此错误:

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

所以在我看来,Relay 根本无法访问我的架构。谁能告诉我我的设置中缺少什么或者我可以做些什么来进一步调试?

如果对任何人有帮助,仅供参考,这是我的 package.json 的样子:

{
  "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"
  }
}

好的,所以答案很简单,看来我的设置已经正确,但我还必须删除 crna 缓存,所以 运行 rm -rf $TMPDIR/react* 并重新启动 iOS模拟器实际上已经做到了。