[GraphQL] Apollo React Mutation Component

In this lesson I refactor a React component that utilizes a higher-order component for mutations to the new Mutation render prop component baked into react-apollo 2.1.

Additional Resources: https://dev-blog.apollodata.com/introducing-react-apollo-2-1-c837cc23d926

If you want to mutate the server state, you can use <Mutation> component to simplify the code:

const ADD_ITEM = gql`
  mutation addItem($value: String!) {
    addItem(value: $value) @client
  }
`;

const client = new ApolloClient({
  clientState: {
    defaults,
    resolvers: {
      Mutation: {
        addItem: (_, { value }, { cache }) => {
          let { items } = cache.readQuery({ query: GET_ITEMS });
          items = [...items, value];
          cache.writeData({ data: { items } });
          return null;
        }
      }
    }
  }
});

const AddItem = () => {
  let input;
  return (
    <Mutation mutation={ADD_ITEM}>
      {(addItem, { loading, error, data }) => (
        <div>
          <form
            onSubmit={e => {
              e.preventDefault();
              addItem({ variables: { value: input.value } });
              input.value = "";
              input.focus();
            }}
          >
            <input ref={node => (input = node)} />
            <button type="submit">Add Item</button>
          </form>
        </div>
      )}
    </Mutation>
  );
};

原文地址:https://www.cnblogs.com/Answer1215/p/8973795.html

时间: 2024-10-31 09:27:21

[GraphQL] Apollo React Mutation Component的相关文章

[GraphQL] Apollo React Query Component

In this lesson I refactor a React component that utilizes the graphql higher-order component to the new Query render prop component baked into react-apollo. Additional Resources: What's next for React Apollo import React from "react"; import { r

[React + GraphQL] Use useLazyQuery to manually execute a query with Apollo React Hooks

When using useQuery from Apollo React Hooks, the request will be sent automatically after the component has been mounted. This might not be the desired behaviour as we might want to send the request in response to user action (for instance, after a b

[React] Create component variations in React with styled-components and &quot;extend&quot;

In this lesson, we extend the styles of a base button component to create multiple variations of buttons, using "extend". We can then modify the base styles in one place, and have all button types updated. import React from "react"; im

[React] Update Component State in React With Ramda Lenses

In this lesson, we'll refactor a React component to use Ramda lenses to update our component state. We'll create a lens to focus on the property we want to target and use over to apply the existing state value to a utility function and we'll get back

[React Fundamentals] Component Lifecycle - Updating

The React component lifecycle will allow you to update your components at runtime. This lesson will explore how to do that. import React from 'react'; import ReactDOM from 'react-dom'; export default class App extends React.Component { constructor(){

[React Fundamentals] Component Lifecycle - Mounting Usage

The previous lesson introduced the React component lifecycle mounting and unmounting. In this lesson you will learn some simple uses for these hooks. import React from 'react'; import ReactDOM from 'react-dom'; class App extends React.Component { con

[React Fundamentals] Component Lifecycle - Mounting Basics

React components have a lifecycle, and you are able to access specific phases of that lifecycle. This lesson will introduce mounting and unmounting of your React components. import React from 'react'; import ReactDOM from 'react-dom'; export default

A Bite Of React(2) Component, Props and State

component component:用户自己定义的元素 const element = <Welcome name="Sara" />; class Welcome extends React.Component { render() { return <h1>Hello, {this.props.name}</h1>; } } 遇到自己定义的component Welcom,React会将它的属性(name)作为对象传递给组建Welcom,即{

[React] Styling a React button component with Radium

React's inline styles allow components to stand on their own by not requiring any external CSS. However HTML's style attributes don't support pseudo selectors like :hover and :active. By using Radium to listen to mouse events we can restore :hover an