Reputation: 913
I'm trying create a simple CRUD app using Graphql in React.
I have the Create and Read but I'm really struggling with the delete.
I have the backend all set up and everything works in the Playground.
In the Playground I can us the delete mutation like
mutation{
deleteRecipe(_id: "5e22e40f9f3478160a31555a"){
_id
name
}
}
and get the response
{
"data": {
"deleteRecipe": {
"_id": "5e22e40f9f3478160a31555a",
"name": "test 13"
}
}
}
I'm having big problems getting this mutation working in React (In React the Create and Read are wotking)
server side
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
}
}
}
client side
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;
In the Network tab I'm getting the error
"Variable "$id" of required type "String!" was not provided.",…
I'm assuming I'm not passing the id value to the deleteRecipe
How can I pass the id to deleteRecipe here
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'] });
}
Upvotes: 0
Views: 2110