question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

react-relay createFragmentContainer props types

See original GitHub issue

Is the createFragmentContainer types correct? Because it is typed as taking and returning TBase, which, if I’m correct, implies that their props are identical. However, for a container taking a viewer fragment, the parent will pass viewer={||}, but the “contained” will receive viewer: {/* whatever is define in the fragment*/}, no? I’m on flow 0.63.1

Because it seem that I have errors on all my fragment since updating my libdefs. Since I’m in the middle of big flow version upgrade, maybe the errors are coming from somewhere else (I still have > 150 errors), so feel free to correct me.

const Raw = (props: {todo: {id: number, text: string}}) => {/**/}
const Child = createFragmentContainer(Raw, {
  todo: graphql`
    fragment todo_todo on Todo {
      id, text
    }
  `
})

const ParentComp = (props: {viewer: {todo: {||}}}) => {
  return <Child todo={viewer.todo}} />
}

In this example, both the viewer and the todo props would be typed by the relay-compiler. Their content is different (since the fragment container takes care of finding the correct Todo in the store, but it seems to me that <Child /> expects the id and text keys.

This is copied from an answer to @sibelius’s PR #1520.

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:5
  • Comments:9 (8 by maintainers)

github_iconTop GitHub Comments

3reactions
sibeliuscommented, Feb 25, 2018

we can’t consume flow directly from node_modules yet,

so we need to upgrade this defs to match new code of realy 1.5.0

2reactions
mandxcommented, Feb 5, 2018

There’s another problem with defining createFragmentContainer and createPaginationContainer as taking and returning TBase: The returned HOC class doesn’t need the relay prop, it will supply it to the wrapped component. But Flow still complains about the missing prop if I omit it:

// MyPagingComponent.js
// ...
import { createPaginationContainer } from 'react-relay';
import type { RelayPaginationProp } from 'react-relay';
// ...

type MyPagingComponentProps = {
  relay: RelayPaginationProp,
};

class MyPagingComponent extends Component<MyPagingComponentProps> {
  // Use `this.props.relay.hasMore()` somewhere
}

export default createPaginationComponent(MyPagingComponent, /* ... */)
// MyFragmentComponent.js
// ...
import { createFragmentContainer } from 'react-relay';
import type { RelayProp } from 'react-relay';
// ...
import MyPagingComponent from './MyPagingComponent';
// ...

type MyFragmentComponentProps = {
  relay: RelayProp,
};

class MyFragmentComponent extends Component<MyFragmentComponentProps> {
  
  render() {
    return (
      <div>
        {/* stuff */}
        <MyPagingComponent relay={/* Here flow expects a RelayPaginationProp instance */}/>
      </div>
    );
  }
}

In this case I can’t even pass the parent’s relay prop because is the wrong type, and I could make it optional, but then I would have to pointlessly ask all the time if the relay prop is set.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Fragment Container - Relay
The Component resulting from createFragmentContainer will receive the following props : type Props = { relay: { environment: Environment, } ...
Read more >
React Relay createFragmentContainer and QueryRenderer ...
I tried to pass the todoList data explicitly in by changing return to return (the same prop name passed in by createFragmentContainer, I...
Read more >
Fetching Data using GraphQL Queries with React & Relay ...
This is a simple React component that expects a link in its props and renders the link's ... import { createFragmentContainer, graphql }...
Read more >
Higher-Order Components - React
Whereas a component transforms props into UI, a higher-order component transforms a ... such as Redux's connect and Relay's createFragmentContainer .
Read more >
Upgrading our React app to GraphQL Relay Hooks - Parabol
Aside from hooks, it lets us use React's Suspense API instead of the render props pattern, allows for fine-grain control of query invalidation,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found