[英]Relay: use constant inside Fragment instead of graphql`...`
中繼createFragmentContainer是一個有用的功能,它很容易使用:
const MyComponent = createFragmentContainer(
MyFragmentComponent,
{
job: graphql`
fragment MyComponent_job on Job {
id
}
`
}
);
問題是當查詢位於我的文件末尾時,真的很難閱讀代碼。 我更喜歡在導入后將它放在右上角。 像這樣:
const QUERY_FRAGMENT = graphql`
fragment MyComponent_job on Job {
id
}
`
// Main code here
const MyComponent = createFragmentContainer(
MyFragmentComponent,
{
job: QUERY_FRAGMENT
}
);
但在這種情況下,中繼編譯器會拋出錯誤: FindGraphQLTags: 'createFragmentContainer' expects fragment definitions to be 'key: graphql'.
createFragmentContainer
和graphql
分開?這似乎是babel-plugin-relay
一個已知問題。 如本期所述,解決方法是更改您的導入:
import Relay, { graphql } from 'react-relay'
const fragment = graphql`...`
...
Relay.createFragmentContainer(Component, fragment)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.