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

时间:2017-03-28 15:37:32

标签: react-native graphql babel relay create-react-native-app

我正在尝试设置一个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"]
    }
  }
}

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

  

文件Unknown Type "Viewer".中的GraphQL验证错误.../PokemonList.js。如果最近添加了参数/字段/类型,请尝试更新GraphQL架构。

所以在我看来,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"
  }
}

1 个答案:

答案 0 :(得分:1)

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