0

我正在尝试设置一个 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到目前为止,它没有做任何事情来打印我viewer.id希望从 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
      }
    `
  }
})

我正在使用这个babel 插件使我的架构在构建时可用于 Relay。

它需要使用三种方法之一提供 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"
  }
}
4

1 回答 1

1

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

于 2017-03-28T17:00:17.280 回答