Public
Authored by Jure

GraphQL createFragment example

import React from 'react'

import gql from "graphql-tag";
import { Mutation } from "react-apollo";

const CREATE_FRAGMENT = gql`
  mutation createFragment($input: String!) {
    createFragment(input: $input) {
      id
      title
    }
  }
`;

export default class Trainer extends React.Component {

  render () {
    let input
    return <Mutation mutation={CREATE_FRAGMENT}>
      {(createFragment, { data }) => (
        <div>
          <form
            onSubmit={e => {
              e.preventDefault();
              createFragment({ variables: { input: input.value } });
              input.value = "{ \"fragmentType\": \"bye\", \"title\": \"hello\" }";
            }}
          >
            <input
              ref={node => {
                input = node;
              }}
            />
            <button type="submit">Create Fragment</button>
          </form>
        </div>
      )}
    </Mutation>
  }
}
13 Bytes
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment