I have a queries file that looks like this:
import {gql} from 'react-apollo';
const queries = {
getApps: gql`
{
apps {
id
name
}
}
`,
getSubjects: gql`
{
subjects {
id
name
}
}
`
};
export default queries;
I then import this file to my React component:
import React, {Component} from 'react'
import queries from './queries'
class Test extends Component {
...
}
export default graphql(queries.getSubjects)(graphql(queries.getApps)(Test));
This will only get data for one of the queries (getApps) and not both. If I do one at a time so that it looks like this:
export default graphql(queries.getSubjects)(Test);
then it works but I don't have my other query. Yes, I have tested both separately and they work. How do I get it so that both queries show up in my props.data?
For Apollo 2.x: you can use
react-adopt
to compose the Queries and Mutations into a single level. (That lib will compose any components with render props, e.g. the React Context API.)https://github.com/pedronauck/react-adopt
My preferred way is to use the
compose
functionality of the apollo client (docu).EDIT: If you have more than one query you should name them.
So in your case, it could look like this:
I'm using react-adopt to make this. It's really simple and keep our code clean.
Simple example:
There are a lot of examples using
Be careful with
<Query>
component, It needs a children, otherwise, it will have the following error:To avoid the previous warning, I have found a possible solution:
Hope it helps you!
If you don't want to reuse any of those queries independently, why not make a single request by combining both queries in one i.e:
and then you can use it in your component
IMHO, one of the most neat solutions is described in the Apollo Client React implementation.
The basic idea is to wrap your queries into nested Query components. Using closure functions as component children makes it handy to delegate the results of one query down into another query and so on.
Another way around this is to use the
props
option.I've found that this approach is a bit nicer for my use case.
Here is a link to the docs: https://www.apollographql.com/docs/react/api/react-apollo.html#graphql-config-props