简体   繁体   中英

Apollo: Multiple mutations

I can't find the right answer. So i'll hope someone helps me out. I want to have two mutations in my export default. But i don't know the right way to approach this.

Current code:

import React from 'react';
import gql from 'graphql-tag';
import {graphql,compose} from 'react-apollo';
import AanvraagFromulier from './AanvraagFormulier';

export class Aanvraag extends React.Component{
    onSubmit = (aanvraag) => {
        console.log(aanvraag);
        this.props.mutate({
            variables: {
                id: aanvraag.id,
                naam: aanvraag.naam,
                email: aanvraag.email,
                afdeling: aanvraag.afdeling,
                divisie: aanvraag.divisie,
                team: aanvraag.team,
                status: 'open',
                //project vars
                projectid: aanvraag.projectid,
                projectnaam: aanvraag.projectnaam,
                projecttype: aanvraag.ptype,
                projectlead_naam: aanvraag.pjnaam,
                projectlead_email:aanvraag.pjemail,
                aanvraag_id:aanvraag.id
            },
        });
    }

    render(){
        console.log(this.props);
        return(
            <div>
                <AanvraagFromulier 
                  onSubmit={this.onSubmit}
                />
            </div>
        );
    }
}

const aanvraagmutation = gql`
    mutation addAanvraag($id:ID, $naam:String,$email:String,$divisie:String,$afdeling:String,$team:String,$status:String){
    addAanvraag(id:$id, naam:$naam,email:$email,divisie:$divisie,afdeling:$afdeling,team:$team,status:$status){
      id
     }
    }
`;

const projectmutation = gql`
mutation addProject($projectid:ID,$projectnaam:String,$projecttype:String,$projectlead_naam:String,$projectlead_email:String,$aanvraag_id:ID){
    addProject(id:$projectid, naam:$projectnaam,type:$projecttype,lead_naam:$projectlead_naam,lead_email:$projectlead_email,aanvraag_id:$aanvraag_id){
      id
    }
  }  
`;

export default graphql(aanvraagmutation)(graphql(projectmutation)(Aanvraag))

The problem is when executing this file only projectmutation goes off and aanvraagmutation does nothing. How can i make it so both of them gets executed?

The graphql function creates a higher order component that, by default, injects a prop called mutate that runs your query.

In your code the higher-order-component created by the graphql(projectmutation) is overwriting the mutate prop that's injected by HOC created by the graphql(aanvraagmutation) .

To avoid this, you need to ask tell graphql to generate a prop with another name instead, using the config.name option to change the default mutate name into something else, so that they don't clash. For example:

export default compose(
   graphql(aanvraagmutation, { name: 'aanvraagmutate' }),
   graphql(projectmutation, { name: 'projectmutate' }),
)(Aanvraag);

(I've also used compose to neaten things up).

You then call each mutation separately in your handler. The HOCS don't combine to create a single mutate query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM