Skip to content

sebastianrath/graphql-reflect-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

A easy-to-use API to retrieve fields and subfields from Go structs using GraphQL and reflection.

Topics

Resources

License

Stars

Watchers

Forks

Languages