Giter Site home page Giter Site logo

Comments (5)

devknoll avatar devknoll commented on May 8, 2024

But I assume that calling getFragment directly in Relay.Route also from other components is also reasonable?

At the moment, there's a 1:1 mapping between a route root query and a fragment. You should also check #20 (comment), because the Component argument is going away too. So, multiple routes probably aren't what you're looking for.

Digging through the source code, you might be able to achieve what you're looking for using variables, doing something like this:

var MyComponentContainer = Relay.createContainer(MyComponent, {
  initialVariables: {
    showPopup: false,
  },
  fragments: {
    album: (variables) => Relay.QL`
      fragment on Album {
        name,
        ${SongList.getFragment('songs').if(variables.showPopup)}
      }
    }``
  }
});

And then running setVariables when you need to get the songs. Note: this is untested, and I have no idea if this will be a supported feature.

from relay.

devknoll avatar devknoll commented on May 8, 2024

Another possibility would be to just use connections, and do something like:

var MyComponentContainer = Relay.createContainer(MyComponent, {
  initialVariables: {
    songsToShow: 0,
  },
  fragments: {
    album: (variables) => Relay.QL`
      fragment on Album {
        name,
        songs(first: $songsToShow) {
          edges {
            node {
              ${SongList.getFragment('songs')}
            }
          }
        }
      }
    }`
  }
});

and use setVariables again to grab the list.

from relay.

jardakotesovec avatar jardakotesovec commented on May 8, 2024

These conditional fragments would be great pattern for these use cases.

   ${SongList.getFragment('songs').if(variables.showPopup)}

Could someone from fb please comment if thats pattern we can use at the moment/future?

Thanks

from relay.

josephsavona avatar josephsavona commented on May 8, 2024

@jardakotesovec Sorry for missing this: yes, ${Child.getFragment(..).if(variables.foo)} is supported. Be sure to check the value of variables.showPopup in your render code and skip rendering <SongList> when it's false, otherwise you'll get errors about missing data.

from relay.

josephsavona avatar josephsavona commented on May 8, 2024

@jardakotesovec did that solution work for you? I'm going to close this issue since the question is answered, but feel free to reopen if you have any additional questions!

from relay.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.