This API provides a simple and efficient way to query specific fields from Go structs.
- Selective Field Queries: Query only the fields you need.
- Nested Struct Support: Ability to query fields within nested structs.
- Easy Integration: Works seamlessly with popular Go frameworks like Echo, also check out the examples.
-
Define Your Structs:
Example:
type Cat struct { Name string `json:"name"` Age int `json:"age"` Color string `json:"color"` }
-
Use
QueryStructViaGraphql
Function:This function helps in querying a struct based on the provided GraphQL query string.
Example:
func QueryCats(c echo.Context) error { ... b, err := QueryStructViaGraphql("cats", cats, post.Query); ... }
-
Set Up Routes:
Use your favorite framework (like Echo) to set up routes and handle requests.
Example:
e.POST("/dogs", QueryDogs) e.POST("/cats", QueryCats)
-
Run Your Server:
e.Logger.Fatal(e.Start(":8000"))
Make a POST request to /cats
or /dogs
with a request body containing your GraphQL query.
Example Request Body:
{
cats
{
name
age
}
}
Response will contain only the name
and age
fields for the respective struct. A Postman example file called postman_examples_import_me.json
is included in the repository. Start the Go server via go run .
and import the json file into Postman to try out the examples.
MIT License. See LICENSE for more information.