简体   繁体   English

中继:在 Fragment 中使用常量而不是 graphql`...`

[英]Relay: use constant inside Fragment instead of graphql`...`

Relay createFragmentContainer is useful feature and it is easy to use:中继createFragmentContainer是一个有用的功能,它很容易使用:

const MyComponent = createFragmentContainer(
    MyFragmentComponent,
    {
        job: graphql`
            fragment MyComponent_job on Job {
                id
            }
        `
    }
);

The problem is it really hard to read code when query is at the end of my file.问题是当查询位于我的文件末尾时,真的很难阅读代码。 I prefer to have it at the top right after imports.我更喜欢在导入后将它放在右上角。 Like this:像这样:

const QUERY_FRAGMENT = graphql`
    fragment MyComponent_job on Job {
        id
    }     
`
// Main code here

const MyComponent = createFragmentContainer(
    MyFragmentComponent,
    {
        job: QUERY_FRAGMENT
    }
);

But relay compiler throws error in that case: FindGraphQLTags: 'createFragmentContainer' expects fragment definitions to be 'key: graphql'.但在这种情况下,中继编译器会抛出错误: FindGraphQLTags: 'createFragmentContainer' expects fragment definitions to be 'key: graphql'.

Is there a way to separate createFragmentContainer and graphql ?有没有办法将createFragmentContainergraphql分开?

This seems to be a known issue with babel-plugin-relay .这似乎是babel-plugin-relay一个已知问题。 As noted in this issue , the workaround is to change your imports:本期所述,解决方法是更改​​您的导入:

import Relay, { graphql } from 'react-relay'

const fragment = graphql`...`

...

Relay.createFragmentContainer(Component, fragment)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM