I'm trying to make an update two fields to Postgres database:
const update_din_times_for_rep = gql`
mutation update_din_times_for_rep(
$id: Int!
$name: String
$val: String
) {
update_din_times_for_rep(id: $id, name: $name, val: $val) {
id
}
}
`;
Next:
const [data_din_times_for_rep, { loading:loadingMutation, error:errorMutation }] = useMutation(update_din_times_for_rep);
And handler with action:
let handlerMutation = () => {
data_din_times_for_rep({ variables: { id } });
}
<Button onClick={handlerMutation}>Test!</Button>
Seems the mistake in resolvers.js, but tried out much, no result:
update_din_times_for_rep: (parent, args) => {
const mytmp = {
id: args.id,
name: args.name,
val: args.val,
}
return Time_windows
}
How can I make the resolver simple as possible? The only resolver part is difficult now for me, please any advises? The resolver was written by another person, I need to modify it for my case.
Update. How to change a resolver.js in order it will write a data to db? One modification I did:
update_din_times_for_rep: (parent, args) => {
const mytmp = {
id: args.id,
name: args.name,
val: args.val,
}
return Time_windows
}
But I can't understand what to do exactly next, I tried to follow several examples found on the web. Most of them is working with just arrays or objects, or flat db files on disk. There is no examples where clearly showing the way to handle mutation to real postgres database as react->resolver-schema-tables-update db.
In my case I did properly react->schema-tables. But not a resolver part. My schema which declared as query and mutation in type Query and type Mutation parts:
type Time_windows {
id: Bigint
name: String
val: String
}
After many tries I found a minimal working code:
update_din_times_for_rep: async (parent, args) => {
const mytmp = { id: args.id,
name: args.name,
val: args.val,
}
await client.query("insert into din_times_for_rep (id, val) values ($1, $2)", [mytmp.id, mytmp.name]);
}
Would be good if anyone add comments or impoves.