admin管理员组文章数量:1356227
I have created a user table in Hasura with the following schema:
id: UUID generated by gen_random_uuid().
name: String.
email: String.
I am building a Go application using the go-graphql-client library to interact with Hasura's GraphQL API. While the create and read operations work perfectly, I'm facing issues with delete and update operations when using the id (UUID) field.
Here is the error message I receive:
Used string or int type but expected UUID type for id.
I tried to use a custom UUID type for consuming the auto-generating Hasura GraphQL using go-graphql-client, but I still cannot resolve this issue.
What I Tried:
Verified the GraphQL mutation structure for delete_users_by_pk
and update_users_by_pk
.
Implemented custom UUID serialization/deserialization using GraphQLUUID.
Ensured proper validation of UUID format.
Looked up various resources but couldn't find a working solution.
Code Snippet: Here is the relevant portion of my code:
func deleteUserHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
idStr := vars["id"]
log.Printf("[DEBUG] Raw ID from URL: %s", idStr)
// Parse and validate UUID
parsedUUID, err := uuid.Parse(idStr)
if err != nil {
log.Printf("[ERROR] Invalid UUID: %v", err)
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
return
}
log.Printf("[DEBUG] Parsed UUID: %s", parsedUUID)
// Create GraphQL UUID
gqlUUID := GraphQLUUID(parsedUUID.String())
log.Printf("[DEBUG] GraphQL UUID: %s", gqlUUID)
// Build variables with type information
variables := map[string]interface{}{
"id": gqlUUID,
}
log.Printf("[DEBUG] Mutation Variables: %+v", variables)
// Execute mutation
var mutation struct {
DeleteUsersByPk struct {
ID GraphQLUUID `graphql:"id"`
} `graphql:"delete_users_by_pk(id: $id)"`
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Mutate(ctx, &mutation, variables)
if err != nil {
log.Printf("[ERROR] GraphQL Mutation Failed: %v", err)
http.Error(w, "Error deleting user: "+err.Error(), http.StatusInternalServerError)
return
}
log.Printf("[DEBUG] Mutation Response ID: %s", mutation.DeleteUsersByPk.ID)
log.Printf("[SUCCESS] Deleted user with ID: %s", parsedUUID)
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]string{
"message": "User deleted successfully",
"id": parsedUUID.String(),
})
}
// Update handler with similar debugging
func updateUserHandler(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
idStr := vars["id"]
log.Printf("[DEBUG] Update Request ID: %s", idStr)
parsedUUID, err := uuid.Parse(idStr)
if err != nil {
log.Printf("[ERROR] Invalid UUID: %v", err)
http.Error(w, "Invalid UUID format", http.StatusBadRequest)
return
}
var input struct {
Name string `json:"name"`
Email string `json:"email"`
Age int `json:"age"`
}
if err := json.NewDecoder(r.Body).Decode(&input); err != nil {
log.Printf("[ERROR] Request Body Decode: %v", err)
http.Error(w, "Invalid request body", http.StatusBadRequest)
return
}
log.Printf("[DEBUG] Update Data: %+v", input)
var mutation struct {
UpdateUsersByPk struct {
ID GraphQLUUID `graphql:"id"`
Name graphql.String `graphql:"name"`
Email graphql.String `graphql:"email"`
Age graphql.Int `graphql:"age"`
} `graphql:"update_users_by_pk(pk_columns: {id: $id}, _set: {name: $name, email: $email, age: $age})"`
}
variables := map[string]interface{}{
"id": GraphQLUUID(parsedUUID.String()),
"name": graphql.String(input.Name),
"email": graphql.String(input.Email),
"age": graphql.Int(input.Age),
}
log.Printf("[DEBUG] Mutation Variables: %+v", variables)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
err = client.Mutate(ctx, &mutation, variables)
if err != nil {
log.Printf("[ERROR] Update Mutation Failed: %v", err)
http.Error(w, "Error updating user: "+err.Error(), http.StatusInternalServerError)
return
}
log.Printf("[SUCCESS] Updated user ID: %s", parsedUUID)
response := User{
ID: mutation.UpdateUsersByPk.ID,
Name: string(mutation.UpdateUsersByPk.Name),
Email: string(mutation.UpdateUsersByPk.Email),
Age: int(mutation.UpdateUsersByPk.Age),
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(response)
}
What I Need Help With: How can I fix the issue with the UUID type when performing delete and update operations in Hasura GraphQL using go-graphql-client? I suspect it has something to do with my custom UUID type, but I'm unsure how to resolve it. Any guidance would be greatly appreciated! but when i check the delete and update(raw http or directly on hasura graphql playground) directly on graphql playground or others both are works as excepted.
本文标签: GraphQL UUID Handling Issue with Hasura CRUD Operations Using gographqlclientStack Overflow
版权声明:本文标题:GraphQL UUID Handling Issue with Hasura CRUD Operations Using go-graphql-client - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744056934a2583427.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论