我正在尝试在React中使用Graphql创建一个简单的CRUD应用。
我拥有“创建并读取”功能,但我确实为删除而苦苦挣扎。
我已经设置了所有后端,并且一切都可以在Playground中运行。
在操场上,我们可以像
这样删除突变。mutation{
deleteRecipe(_id: "5e22e40f9f3478160a31555a"){
_id
name
}
}
并获得响应
{
"data": {
"deleteRecipe": {
"_id": "5e22e40f9f3478160a31555a",
"name": "test 13"
}
}
}
我在使这种突变在React中起作用时遇到了很大的问题(在React中,Create和Read正在工作)
服务器端
schema.js
exports.typeDefs = `
type Recipe{
_id: String
name: String
description: String
}
type Query{
recipe:[Recipe]
}
type Mutation{
addRecipe(name: String!, description: String):Recipe,
deleteRecipe(_id: String!):Recipe
}
`
resolvers.js
exports.resolvers = {
Query: {
recipe: async (obj, args, { Recipe }, info) => {
const allRecipes = await Recipe.find()
return allRecipes
}
},
Mutation: {
addRecipe: async (obj, { name, description }, { Recipe }, info) => {
const newRecipe = await new Recipe({
name,
description
}).save()
return newRecipe
},
deleteRecipe: async (obj, { _id }, { Recipe }, info) => {
const delRecipe = await Recipe.findByIdAndRemove({ _id })
return delRecipe
}
}
}
客户端
queries / index.tsx
import { gql } from 'apollo-boost';
export const GET_ALL_RECIPES = gql`
query RecipeData{
recipe{
_id
name
description
}
}
`
export const ADD_RECIPE = gql`
mutation addRecipe($name: String!, $description:String){
addRecipe(name: $name, description: $description){
_id
name
description
}
}
`
export const DELETE_RECIPE = gql`
mutation deleteRecipe($id: String!){
deleteRecipe(_id: $id){
_id
name
description
}
}
`
App.tsx
import React, { useState } from 'react';
import './App.css';
import { RecipeData } from '../generated/RecipeData';
import { GET_ALL_RECIPES, ADD_RECIPE, DELETE_RECIPE } from '../queries';
import { useQuery, useMutation } from 'react-apollo-hooks';
const App: React.FC = () => {
const [name, setName] = useState<string>('')
const [description, setDes] = useState<string>('')
const [] = useState<string>('')
// useEffect(() => {
// deleteRecipe()
// }, [recipeId])
const handleNameChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setName(e.target.value)
}
const handleDesChange = (e: React.ChangeEvent<HTMLInputElement>) => {
setDes(e.target.value)
}
const handleClick = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
createRecipe()
};
const [deleteRecipe] = useMutation(DELETE_RECIPE)
const handelDelete = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
const delID = e.target.parentElement.getAttribute("data-id")
deleteRecipe({ variables: { _id: delID }, refetchQueries: ['RecipeData'] });
}
// const [deleteRecipe, {data}] = useMutation(DELETE_RECIPE, {
// variables: { _id: }, refetchQueries: ['RecipeData']
// })
const [createRecipe, { error }] = useMutation(ADD_RECIPE, {
variables: { name, description }, refetchQueries: ['RecipeData']
})
if (error) {
console.error('erroring : ', error)
}
const { data, loading } = useQuery<RecipeData | null>(GET_ALL_RECIPES, {
suspend: false
})
if (loading || !data) return <div>Loading</div>
return (
<div className="App">
<h1>Graphql</h1>
<ul>
{
data.recipe !== null && data.recipe.map((recipe) => (
<li key={recipe._id} data-id={recipe._id}>
{recipe.name}
<button onClick={handelDelete}>X</button>
</li>
))
}
</ul>
<form>
<div>
<label>Name</label>
<input
type="text"
value={name}
onChange={handleNameChange}
/>
</div>
<div>
<label>Description</label>
<input
type="text"
value={description}
onChange={handleDesChange}
/>
</div>
<button onClick={handleClick}>Add Recipe</button>
</form>
</div>
);
}
export default App;
在“网络”标签中,我收到错误消息
"Variable "$id" of required type "String!" was not provided.",…
我假设我没有将id值传递给deleteRecipe
如何在此处传递ID以删除密码
const [deleteRecipe] = useMutation(DELETE_RECIPE)
const handelDelete = (e: React.MouseEvent<HTMLButtonElement>) => {
e.preventDefault()
const delID = e.target.parentElement.getAttribute("data-id")
deleteRecipe({ variables: { _id: delID }, refetchQueries: ['RecipeData'] });
}
答案 0 :(得分:1)
您的变量为id
{ variables: { id: delID }