I have given mutation query for adding book details. I have to accept 3 params dynamically. this is my bookQuery.js
const ADD_BOOK = gql`{
mutation ($name: String!, $genre: String!, $authorId: ID!) {
addBook(name: $name,genre : $genre, authorId: $authorId) {
id
}
}
}`
If I’m calling this query using following code.
import { useQuery, useMutation } from '@apollo/client';
...
const [addBook] = useMutation(ADD_BOOK);
function submitBook(e) {
e.preventDefault()
const { name, genre, authorId } = book;
addBook({ variables: { name, genre, authorId } })
}
I’m using another useQuery in same function as well. I’m getting following syntax error which is not known to me.
GraphQLError: Syntax Error: Expected Name, found "$".
Query should be without curly braces like
const ADD_BOOK = gql`
mutation ($name: String!, $genre: String!, $authorId: ID!) {
addBook(name: $name,genre : $genre, authorId: $authorId) {
id
}
}
`