I'm trying to combine two dependent GraphQL queries.
The first one should get an ID and the second one should take that ID. I read that compose behaves like flowRight(), but no matter in what order I put the queries, if queryId is below queryDetails, queryDetail's is always skipped (as expected). No matter how I put my code together the variable is undefined.
import { graphql, compose } from 'react-apollo'
import gql from 'graphql-tag'
class Home extends Component {
constructor(props) {
super(props)
console.log("Where's my data?")
console.log(props)
}
render() {
return(
<div />
)
}
}
export const queryIdConst = gql`
query IdQuery {
account(name:"SuperAccount")
{
lists {
edges {
id
}
}
}
}
`
export const queryDataConst = gql`
query DataQuery($id: ID!) {
account(name:"SuperAccount")
{
list(id: $id) {
displayTitle
}
}
}
`
export default compose(
graphql(queryIdConst, {
name: 'listId',
}),
graphql(queryDataConst, {
name: 'data',
skip: ({ listId }) => !listId.data,
options: ({ listId }) => ({
variables: {
id: list.data.account.lists.edges[0].id
}
})
})
)(Home)
I have already tried to change the compose functions order, but anyway this is not working, as I expected it to work.
Thanks for any help!
Edit: Switched the two graphql() in compose() to be inline with AbsoluteSith's comment link
With hints and help from Daniel Rearden and AbsoluteSith I implemented the following solution:
Changed the compose():
export default compose(
graphql(queryIdConst, {
name: 'listId',
}),
graphql(queryDataConst, {
name: 'dataHome', // changed to 'dataHome' to avoid confusion
skip: ({ listId }) => !listId.account,
options: ({ listId }) => ({
variables: {
id: listId.account.lists.edges[0].id
}
})
})
)(Home)
And my render():
return(
<div>
{ dataHome && !dataHome.loading &&
<div>{dataHome.account.list.displayTitle}</div>
}
</div>
)
Multiple OperationsIf a request has two or more operations, then each operation must have a name. A request can only execute one operation, so you must also include the operation name to execute in the request (see the “operations” field for requests). Every operation name in a request must be unique.
Multiple arguments can be used together in the same query. For example, you can use the where argument to filter the results and then use the order_by argument to sort them.
The multiple queries are executed in the same requested order. ✳️ Note: This is different from query batching, in which the GraphQL server also executes multiple queries in a single request, but those queries are merely executed one after the other, independently from each other. This feature improves performance.
The GraphiQL client allows you to create variables for use in your queries. To add a query variable, click the Query Variables pane and enter a JSON object that defines your variable. To use a variable in your query, prepend the $ character to your variable name and use it to replace the desired value.
When using the graphql
HOC, by default, the wrapped component receives a prop called data
(or mutate
if passing in a mutation). Given a query like
query IdQuery {
account(name:"SuperAccount") {
lists {
edges {
id
}
}
}
}
once the query loads, the query result is available under this.props.data.account
. When you use the name
configuration option, you're telling the HOC to use something other than data
for the prop name. So if you set name to listId
, then your query result will be available at
this.props.listId.account
That means the second HOC inside of compose should look more like this:
graphql(queryDataConst, {
skip: ({ listId }) => !listId.account, // <--
options: ({ listId }) => ({
variables: {
id: listId.account.lists.edges[0].id // <--
}
})
})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With