facebook / relay

Relay is a JavaScript framework for building data-driven React applications.
https://relay.dev
MIT License
18.41k stars 1.83k forks source link

Relay classic to relay modern migration #4253

Open adammo94 opened 1 year ago

adammo94 commented 1 year ago

Hey, I'm working on migrating super-duper old project made in relay which apparently has no documentation (0.9.4). Since project is made with class components, I made the decision to move to relay modern without hooks (legacy API). I saw a migration guide, but not everything is clear to me.

here's an example how it is currently working, React-router (v3) route:

<Route
  name="LogIn"
  path={LinkService.login()}
  component={Login} // 2.
  queries={ViewerQueries} // 1.
  render={renderOrLoading(Login)}
/>

ViewerQueries

const ViewerQueries = {
  viewer: () => Relay.QL`query { viewer }`,
}

Login Component

export class _Login extends React.Component {
  render() {
    return <LoginPageContent viewer={this.props.viewer} /> // 3.
  }
}

export default Relay.createContainer(_Login, {
  fragments: {
    viewer: () => Relay.QL`
      fragment on Viewer {
        ${LoginPageContent.getFragment('viewer')},
      }
    `,
  },
})

LoginPageContent

export default Relay.createContainer(Login, {
  initialVariables: {},

  fragments: {
    viewer: () => Relay.QL`
      fragment on Viewer {
        user {
          id
          username
          userProfile {
            showTermsOfServiceModal
            isAmbassador
            isInternal
            isLite
            isExternal
          }
        }
        ${TermsOfServiceModal.getFragment('viewer')}
        ${AuthMutation.getFragment('viewer')},
      }
    `,
  },
})

How do I put ${SomeComponent.getFragment('someFragment') in relay modern? Currently it says it came across dollar sign and won't process.

What I've done:

edvinerikson commented 1 year ago

I think your best choice is to look at the old docs. Eg here: https://github.com/facebook/relay/blob/v1.5.0/docs/Modern-ConversionPlaybook.md

adammo94 commented 1 year ago

@edvinerikson as I've said, I tried updating according to migration guide, however I do not know if changing from fragments to queries is a good way? Also I do not know how do include fragments that are inside (classic) mutations in (modern) queries/mutations

edvinerikson commented 1 year ago

You probably want the compat api. Eg createFragmentContainer from the compat module. These fragments can be referenced from classic and modern. Regarding mutation you can probably just query all fields that may change, this how it's done in modern. Otherwise I suppose mutation might still be able to reference compat fragments.

https://github.com/facebook/relay/blob/v1.5.0/docs/Modern-RelayCompat.md

The suggested approach in the docs is to:

  1. migrate all fragments to the compat api (createFragmentContainer)
  2. Switch Relay.Renderer to QueryRenderer.

Don't start at the top of the component tree, start at the bottom/leaf components instead and work yourself up to the query.

edvinerikson commented 1 year ago

The docs also states this which might clear things up regarding your mutation question:

* Modern API doesn't support mutation fragments. You might have to inline the mutation fragments from your legacy mutation in the fragment of the component.

edvinerikson commented 1 year ago

You can run the codemod for the fragments: https://github.com/facebookarchive/relay-codemod/blob/master/transforms/migrate-to-modern-1.0.js