Apollo/Graphqlmutate数组
创始人
2024-09-09 06:31:31
0

在Apollo/GraphQL中,要通过mutation来修改数组。可以按如下步骤操作:

  1. 定义GraphQL mutation:
mutation AddItem($item: ItemInput!) {
  addItem(item: $item) {
    id
    name
    description
  }
}
  1. 在组件当中使用mutation:
import { useMutation } from '@apollo/react-hooks';
import { ADD_ITEM } from '../graphql/mutations';

function AddItemForm() {
  const [item, setItem] = useState({});

  const [addItem, { loading, error }] = useMutation(ADD_ITEM, {
    onCompleted(data) {
      // Do something with data
    },
    onError(error) {
      // Handle error
    }
  });

  const handleSubmit = (e) => {
    e.preventDefault();

    addItem({ variables: { item } });
  };

  return (
    
setItem({ ...item, name: e.target.value })} /> setItem({ ...item, description: e.target.value })} /> {error &&

{error.message}

}
) }
  1. 在后端实现mutation:
const typeDefs = gql`
  input ItemInput {
    name: String
    description: String
  }

  type Item {
    id: Int
    name: String
    description: String
  }

  type Mutation {
    addItem(item: ItemInput): Item
  }
`;

const resolvers = {
  Mutation: {
    addItem: (_, { item }) => {
      const newItem = {
        id: items.length + 1,
        ...item
      };

      items.push(newItem);

      return newItem;
    }
  }
}

相关内容

热门资讯

透视线上!红龙poker辅助,... 透视线上!红龙poker辅助,aapoker有脚本吗,发现一款(有挂透明)1、不需要AI权限,帮助你...
透视好友房!智星德州菠萝透视插... 透视好友房!智星德州菠萝透视插件工具,hhpoker开挂教程,第三方教程(其实真的有挂);玩家在智星...
透视存在!wpk辅助ai,we... 透视存在!wpk辅助ai,wepoker辅助器下载,一分钟揭秘(有挂了解)是一款可以让一直输的玩家,...
透视软件!aapoker辅助挂... 透视软件!aapoker辅助挂,约局吧如何查看是否有挂,细节揭秘(原本是有挂)1、不需要AI权限,帮...
透视挂透视!hhpoker透视... 透视挂透视!hhpoker透视方法,wpk辅助器安装,一分钟带你了解(有挂详情)1、让任何用户在无需...
透视安卓版!wepoekr底牌... 这是一款非常优秀的wepoekr底牌透视 ia辅助检测软件,能够让你了解到wepoekr底牌透视中牌...
透视教程!hhpoker透视脚... 透视教程!hhpoker透视脚本安卓,wpk辅助软件,一分钟了解(有挂技巧);是一款可以让一直输的玩...
透视系统!德普之星透视辅助io... 透视系统!德普之星透视辅助ios,wepoker作弊方法,详细教程(果然真的是有挂);亲们利用一分钟...
透视私人局!wepoker透视... 透视私人局!wepoker透视脚本安卓,aapoker辅助插件工具下载,今日百科(有挂攻略)(1)透...
透视系统!wepoker辅助器... 透视系统!wepoker辅助器是真的的吗,wepoker透视app下载,wpk教程(总是是有挂),亲...