简体   繁体   中英

Relay: use constant inside Fragment instead of graphql`...`

Relay createFragmentContainer is useful feature and it is easy to use:

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'.

Is there a way to separate createFragmentContainer and graphql ?

This seems to be a known issue with 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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