Last active
May 14, 2025 02:38
-
-
Save pythoninthegrass/73363599680380af824bf0b5acad7ee0 to your computer and use it in GitHub Desktop.
llm generated iris rest server to make gql requests
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"net/http" | |
"strings" | |
"github.com/kataras/iris/v12" | |
"github.com/thepolyglotdeveloper/graphql" | |
) | |
func main() { | |
app := iris.New() | |
// Define a route for the GraphQL endpoint | |
app.Post("/graphql", handleGraphQLRequest) | |
// Start the server on port 8080 | |
app.Run(iris.Addr(":8080")) | |
} | |
func handleGraphQLRequest(ctx iris.Context) { | |
query := ctx.FormValue("query") | |
variables := map[string]interface{}{} | |
// Execute the GraphQL query | |
result, err := graphql.ExecuteQuery(query, variables) | |
if err != nil { | |
ctx.JSON(iris.Map{"error": err.Error()}) | |
return | |
} | |
// Post the results to Slack | |
slackWebhookURL := "YOUR_SLACK_WEBHOOK_URL" | |
postToSlack(slackWebhookURL, fmt.Sprintf("GraphQL Query Result: %v", result)) | |
ctx.JSON(result) | |
} | |
func postToSlack(webhookURL string, message string) error { | |
reqBody := fmt.Sprintf(`{"text": "%s"}`, message) | |
resp, err := http.Post(webhookURL, "application/json", strings.NewReader(reqBody)) | |
if err != nil { | |
return err | |
} | |
defer resp.Body.Close() | |
return nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://iris-go.com/docs/