Search code examples
graphqlrelayjs

Using graphql` in Relay.createContainer


I have a specific use case where I want to use new graphqlbabel transform since it provides graphql query name lookup but without createFragmentContainer. Is there any way to write a converter from gaphql to Relay.QL`?

I looked at the output of both Relay.QLand graphql but couldn't figure out a good way to convert graphqlto Relay.QL


Solution

  • I figured this out here is the sample of code I have written

    const convertToClassicQuery = query => query.classic(RelayQL_GENERATED).node;

    Here is small library to transform the createContainer to accept relay modern queries

    import Relay from 'react-relay/classic';
    
    const RelayQL_GENERATED = require('react-relay/classic').QL; // eslint-disable-line
    
    const convertToClassicQuery = query => query.classic(RelayQL_GENERATED).node;
    
    const convertFragments = fragments => _.mapValues(fragments, fragment => () => convertToClassicQuery(fragment()));
    
    const convertFragmentProps = (fragmentProps) => {
      fragmentProps.fragments = convertFragments(fragmentProps.fragments); // eslint-disable-line
      return fragmentProps;
    };
    const createContainer = (Component, fragmentProps) => Relay.createContainer(Component, convertFragmentProps(fragmentProps));
    
    
    module.exports = {
      createContainer,
    };