Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apollo GraphQL: Multiple Queries in One Component?

I have a component that needs to query two entirely separate tables. What do the schema, query and resolver need to look like in this case? I've googled but haven't found examples yet. Thanks in advance for any info.

UPDATE: On Slack I see there may be a way to use compose for this purpose, e.g.:

export default compose(
  graphql(query1, 
  ....),
  graphql(query2, 
  ....),
  graphql(query3, 
  ....),
  withApollo
)(PrintListEditPage)

Is there a way to have multiple declarations like this:

const withMutations = graphql(updateName, {
  props({ mutate }) {
    return {
      updatePrintListName({ printListId, name }) {
        return mutate({
          variables: { printListId, name },
        });
      },
    };
  },
});

...that come before the call to export default compose?

like image 468
VikR Avatar asked Oct 29 '16 23:10

VikR


People also ask

Can GraphQL schema have multiple queries?

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.

How do you pass two arguments in GraphQL query?

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.


1 Answers

The graphql function takes an optional second argument that allows you to alias the passed down property name. If you have multiple mutations you can use the name property to rename mutate as needed:

import { graphql, compose } from 'react-apollo'

export default compose(
  graphql(mutation1, { name: 'createSomething' }),
  graphql(mutation2, { name: 'deleteSomething' }),
)(Component)

For more details see the complete API.

like image 158
Daniel R. Avatar answered Nov 22 '22 23:11

Daniel R.