我有这样的突变-
import gql from "graphql-tag";
const completedAt = new Date().toISOString();
export const CREATE_TODO = gql`
mutation($body: String!, $completedAt: DateTime = completedAt) {
createTodo(input: { body: $body, completed_at: $completedAt }) {
id
body
}
}
`;
当我称这种突变为错误时
GraphQL错误:$ completedAt的默认值与类型不匹配 React Apollo中的DateTime
我该如何解决?我也尝试过JSON.stringify(new Date().toISOString())
,但没有运气:(
另外,请注意,这可行-
import gql from "graphql-tag";
const completedAt = JSON.stringify(new Date().toISOString());
export const CREATE_TODO = gql`
mutation($body: String!) {
createTodo(input: { body: $body, completed_at: ${completedAt} }) {
id
body
}
}
`;
答案 0 :(得分:0)
我更改了解决方案。现在,我要做的是不使用这样的 completedAt 。我将其作为GraphQL变量的输入,例如:
import gql from "graphql-tag";
export const CREATE_TODO = gql`
mutation createTodo($body: String!, $completedAt: DateTime) {
createTodo(input: { body: $body, completed_at: $completedAt }) {
id
body
completed_at
}
}
`;
然后,由于 $ completedAt 不是必需字段,如果我不需要任何值,我将发送 null 或如果我希望它具有当前时间,请新Date()。toISOString()。
例如:
mutate({
query: CREATE_TODO,
variables: { body: "Incomplete todo", completedAt: null }
});
mutate({
query: CREATE_TODO,
variables: { body: "Complete todo", completedAt: new Date().toISOString() }
});