Skip to content
This repository has been archived by the owner on May 26, 2023. It is now read-only.

Parse result of mutation in the promise #90

Closed
1 of 5 tasks
tmepple opened this issue May 22, 2018 · 3 comments
Closed
1 of 5 tasks

Parse result of mutation in the promise #90

tmepple opened this issue May 22, 2018 · 3 comments
Labels
feature New addition or enhancement to existing solutions

Comments

@tmepple
Copy link

tmepple commented May 22, 2018

Issue Labels

  • has-reproduction
  • feature
  • docs
  • blocking
  • good first issue

It would be helpful if inside the promise result of the mutation function the result is returned already parsed just like the result that is passed as the argument of the render prop. Looking at the Pokemon example from README:

module AddPokemon = [%graphql
  {|
  mutation addPokemon($name: String!) {
      addPokemon(name: $name) {
          name
      }
  }
|}
];

module AddPokemonMutation = ReasonApollo.CreateMutation(AddPokemon);

let make = _children => {
  render: (_) =>
    <AddPokemonMutation>
      ...(
           (mutation /* Mutation to call */, 
           {result} /* Parsed result of your mutation */ ) => {
             let newPokemon = AddPokemon.make(~name="Bob", ());
             <div>
               <button
                 onClick=(
                   _mouseEvent =>
                     mutation(
                       ~variables=newPokemon##variables,
                       ~refetchQueries=[|"getAllPokemons"|],
                       (),
                     )
                     |> Js.Promise.then_(rawResult => {
                          /* The following line is required since the rawResult is not already parsed */
                          let parsedResult =
                            AddPokemonMutation.convertJSInputToReason(rawResult).result;
                          switch (parsedResult) {
                          | Loading => ()
                          | Error(err) => Js.log2("Error", err)
                          | Data(data) => Js.log2("Data", data)
                          };
                          Js.Promise.resolve();
                        })
                 )>
                 (ReasonReact.string("Add Pokemon"))
               </button>
             </div>;
           }
         )
    </AddPokemonMutation>,
};

Please let me know if you have any questions... thanks!

@ghost ghost added the feature New addition or enhancement to existing solutions label May 22, 2018
@Gregoirevda
Copy link
Contributor

Thanks for submitting

@Gregoirevda
Copy link
Contributor

Fixed in #164

@andrewlinfoot
Copy link

andrewlinfoot commented Jun 4, 2019

@Gregoirevda do you have an example of how to use the mutation response value with the updated types? We used .convertJSInputToReason(rawResult).result; in our application and now I'm not sure how to refactor to upgrade

Nvm. Figured it out. Here is a full example in case anyone else comes across the same issue

module Graphql = [%graphql
  {|
    mutation getSignedUrl($fileName: String!, $fileType: String!) {
      getSignedUrl(fileName: $fileName, fileType: $fileType)
    }
  |}
];

module Mutation = ReasonApollo.CreateMutation(Graphql);

let getSignedUrl = (mutation: Mutation.apolloMutation, ~fileName, ~fileType) => {
  let query = Graphql.make(~fileName, ~fileType, ());
  mutation(~variables=query##variables, ())
  |> Js.Promise.then_(
       (executionResponse: ReasonApolloTypes.executionResponse(Graphql.t)) => {
       let signedUrl =
         switch (executionResponse) {
         | Data(data) => data##getSignedUrl
         };
       Js.Promise.resolve(signedUrl);
     });
};

let component = ReasonReact.statelessComponent("GetSignedUrl");

[@react.component]
let make = children =>
  <Mutation>
    ...{(mutation, result) => children(getSignedUrl(mutation), result)}
  </Mutation>;

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature New addition or enhancement to existing solutions
Projects
None yet
Development

No branches or pull requests

3 participants