Error trying to create new, graphql
Error: Variable "$author" of required type "AddAuthorInput!" was not provided.: {"response":{"errors":[{"message":"Variable "$author" of required type "AddAuthorInput!" was not provided.","locations":[{"line":1,"column":20}],"extensions":{"code":"BAD_USER_INPUT","stacktrace":["GraphQLError: Variable "$author" of required type "AddAuthorInput!" was not provided."," at coerceVariableValues
this is my create.tsx
import { useForm, Create } from "@refinedev/antd";
import { Form, Input } from "antd";
import { ADD_AUTHOR } from "./queries";
export const CreateAuthor = () => {
const { formProps, saveButtonProps, onFinish } = useForm({
redirect: "edit",
meta: {
gqlMutation: ADD_AUTHOR,
},
onMutationError(error, data) {
console.log(error, data);
},
});
const handleSubmit = async (values: any) => {
try {
await onFinish({ author: { name: values.name } } );
} catch (error) {
console.error("Error adding author:", error);
}
};
return (
<Create saveButtonProps={saveButtonProps} >
<Form {...formProps} onFinish={handleSubmit} layout="vertical">
<Form.Item label="Name" name="name">
<Input />
</Form.Item>
</Form>
</Create>
);
};
interface AddAuthorResponse {
author: {
name: string;
};
}
and this is my
import gql from "graphql-tag";
export const AUTHORS_LIST_QUERY = gql
query Authors {
authors {
id
name
}
}
;
export const AUTHOR_SHOW_QUERY = gql
query AuthorShow($id: ID!) {
author(id: $id) {
name
}
}
;
export const ADD_AUTHOR = gql
mutation AddAuthor($author: AddAuthorInput!) {
addAuthor(author: $author) {
name
}
}
;
any ideas or suggestions?5 Replies
Hi @mfJOE. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:
----------------------
Based on the provided code, it seems like you are correctly setting up your GraphQL mutation and using it in your form. The error message you're seeing typically means that the GraphQL server is expecting a variable that was not provided in the mutation.
In your case, the server is expecting a variable
$author
of type AddAuthorInput!
which is not being provided. This could be due to a couple of reasons:
1. The variable is not being passed correctly in the onFinish
function. Make sure that the values
object contains the correct data and is being passed correctly.
2. There might be a mismatch between the variable name in your mutation and the variable you're passing in your onFinish
function. Ensure that the variable names match.
3. The AddAuthorInput!
type on your server might be expecting more fields than just name
. Check your GraphQL schema on the server to make sure you're providing all the necessary fields.
If you're still having trouble, you could try logging the values
object in your handleSubmit
function to make sure it contains the correct data. You could also try logging any errors in your onMutationError
function to get more information about what might be going wrong.
For more information on how to use GraphQL with Refine, you can refer to the Refine documentation on GraphQL (https://refine.dev/docs/data/packages/graphql#queries-and-mutations).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@941743472573030470> kapa.ai is still learning and improving, please let us know how it did by reacting below
eastern-cyanOP•9mo ago
thanks @kapa.ai , i have been reading the docs, but not having success, i noticed that when the request is done, this is the data that is send "status":200,"headers":{"map":{"cache-control":"no-store","content-length":"1734","content-type":"application/json; charset=utf-8"}}},"request":{"query":"mutation createAuthor($author: createAuthorInput!) {\n createAuthor(author: $author) {\n name\n }\n}\n","variables":{"input":{"data":{"author":{"name":"Stephen King"}}}}}}
i want to figure out how can i do, to only send {"author":{"name":"Stephen King"}}
and remove, variables, input and data
how can i handle this? on the useForm? on the Create? i dont find any reference on examples or other projects... what do i am missing?
Let me check the knowledge base for you. One moment! :books:
----------------------
The structure of the variables sent in the GraphQL mutation is determined by the GraphQL server's schema. In your case, it seems like the server is expecting the variables to be nested under
input
and data
.
If you want to change this structure, you would need to modify your GraphQL server's schema to accept the author
object directly, instead of nested under input
and data
.
However, if you don't have control over the server's schema, you can adjust the variables in your onFinish
function in your form. Here's how you can do it:
In this case, values
should be an object like { name: "Stephen King" }
.
This will ensure that the variables are structured in the way that your server's schema expects.
For more information on how to handle forms and mutations in Refine, you can refer to the Refine documentation on forms (https://refine.dev/docs/api-references/components/form/useform) and mutations (https://refine.dev/docs/api-references/hooks/mutations).
----------------------
Note: you can ask me a follow up question by @ mentioning me again :speech_balloon:
----------------------<@941743472573030470> kapa.ai is still learning and improving, please let us know how it did by reacting below