Skip to content

Instantly share code, notes, and snippets.

@jstejada
Forked from bcherny/fragments.js
Last active February 11, 2019 06:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jstejada/355a8bb874395ac5f7f36de69e7312ef to your computer and use it in GitHub Desktop.
Save jstejada/355a8bb874395ac5f7f36de69e7312ef to your computer and use it in GitHub Desktop.
/*
Nice things about this approach:
- No need to expose $refs to users. Just use regular IDs instead.
- No need for double-composition (just compose React components, not queries).
- Composition becomes less magical (since we're passing regular IDs around).
- Collapse Query and Fragment concepts into Query.
- Take advantage of existing template strings to pass in query variables.
Drawbacks:
- Requires that every node has a globally unique ID (do we already impose this constraint?)
*/
// TodoItem.js
import type {Todo$DataID} from 'schema.graphql';
type Props = {
todoDataID: Todo$DataID,
}
function TodoItem(props: Props) {
let item = useQuery(
graphql`
query TodoItem_item on Todo {
text
isComplete
}
`,
todoDataID,
{} /* Optional variables */
),
return (
<View>
<Checkbox checked={item.isComplete} />
<Text>{item.text}</Text>
</View>
);
}
// TodoList.js
import {getDataID} from 'relay'
type Props = {
userDataID: User$DataID
}
function TodoList(props: Props) {
let data = useQuery(
graphql`
query TodoList_list on User {
todos(first: $first) {
count
edges {
node {
id
}
}
}
}
`,
props.userDataID,
{first: props.initialCount ?? 10} /* Optional variables */
);
return data.edges.map(edge =>
<TodoItem todoDataID={getDataID(edge?.node)} />
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment