graphql-code-generator icon indicating copy to clipboard operation
graphql-code-generator copied to clipboard

Code samples for ts react apollo with mutations

Open dstaver opened this issue 7 years ago • 4 comments

Is there a complete code sample for how to use the Hocs for a mutation?

I’m pretty new to graphql and can’t figure out how to actually use these.

dstaver avatar Mar 07 '19 11:03 dstaver

@ardatan can you please assist? :)

dotansimha avatar Mar 11 '19 16:03 dotansimha

You can use withYourMutationName that is generated with react-apollo plugin.

withYourMutationName()(YourBaseComponent);

class YourBaseComponent<YourMutationNameDataProps>{
   render(){
       this.mutate({ variables: {} })
   }
}

ardatan avatar Mar 19 '19 17:03 ardatan

@dotansimha Are there any plans to add additional documentation on this? I'm trying to figure out how to use the generated mutation components but I'm not having much luck digging through ts without docs.

From what I can gather, the mutation component forwards a 'mutation function' that fires the mutation accepting some options?

<UpdateUserComponent>
  {mutate => (
    <Button onClick={() => mutate(someOptions)}>Mutate</Button>
  )}
</UpdateUserComponent>

Am I close?

vivirancy avatar May 14 '19 19:05 vivirancy

Hi @Flash619 ! The output from the codegen is has the same signature and usage as Apollo. You can find the documentation here: https://www.apollographql.com/docs/react/api/react-apollo The only difference is that GraphQL Codegen wraps the original <Mutation> with a custom name, adds the type-safety to it, and automatically sets the GraphQL operation, so you can just use it as-is without the need to pass the GraphQL document or type generics.

dotansimha avatar May 15 '19 09:05 dotansimha