graphql-reflect-go

command module
v0.0.0-...-6a0bf9a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 27, 2023 License: MIT Imports: 9 Imported by: 0

README

GraphQL query API for Go structs

Overview

GraphQL Logo

This API provides a simple and efficient way to query specific fields from Go structs.

Key Features

  1. Selective Field Queries: Query only the fields you need.
  2. Nested Struct Support: Ability to query fields within nested structs.
  3. Easy Integration: Works seamlessly with popular Go frameworks like Echo, also check out the examples.

Quick Start

  1. Define Your Structs:

    Example:

    type Cat struct {
        Name string `json:"name"`
        Age  int    `json:"age"`
        Color string `json:"color"`
    }
    
  2. 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);
        ...
    }
    
  3. 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)
    
  4. Run Your Server:

    e.Logger.Fatal(e.Start(":8000"))
    

Usage

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.

License

MIT License. See LICENSE for more information.

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL