structogqlgen

command module
v0.0.0-...-fca02c2 Latest Latest
Warning

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

Go to latest
Published: Apr 21, 2024 License: GPL-3.0 Imports: 1 Imported by: 0

README

structogqlgen

StructsToGqlGenTypes is a tool that helps to automatically converts Golang structs into GraphQL types that are readily usable with the popular GraphQL framework, gqlgen. It aims to reduce the boilerplate code required to define GraphQL schemas manually, thus accelerating the development of GraphQL APIs in Go projects.

Features

  • Automatic Conversion: Convert your existing Golang structs into GraphQL types with a single command, preserving struct relationships and data types.
  • Compatibility with gqlgen: Generated GraphQL types are fully compatible with gqlgen, ensuring smooth integration into your existing GraphQL server setup.
  • Customizable Mappings: Offers options to customize how your Go data types are mapped to GraphQL types, allowing for fine-tuned control over the schema generation process.
  • CLI Tooling: Comes with a command-line interface that makes it easy to integrate into development workflows and CI/CD pipelines.

Use Case

This tool is perfect for developers using gqlgen who already have a collection of defined structs and wish to use them as templates for generating their GraphQL schemas. It removes the need to manually craft GraphQL type definitions that replicate existing Go data structures, thereby enhancing developer productivity and minimizing the likelihood of errors.

Getting Started

To get started with structogqlgen, clone the repository, install the necessary dependencies, and follow the quick setup guide provided in the documentation to integrate it into your project.

Installation

Using the Interactive CLI:

Ensure that you have Go installed on your local machine. If not, you can download it from the official Go website.

Run :

go install github.com/VintageOps/structogqlgen@latest

This typically compiles and Install the binary in bin directory inside your Go workspace or the global Go installation directory ($GOPATH/bin or $GOBIN), if neither $GOPATH nor $GOBIN are set, then it will install under $HOME/go/bin.

Using the available packages Types and Functions
go get github.com/VintageOps/structogqlgen
import github.com/VintageOps/structogqlgen

Usage

~/go/bin/structogqlgen -h
NAME:
   structogqlgen - Converts Golang structs into GraphQL types that are readily usable with the popular GraphQL framework, gqlgen

USAGE:
   structogqlgen [global options] 

DESCRIPTION:
   StructsToGqlGenTypes is a tool that helps to automatically converts Golang structs into GraphQL types that are readily usable with the popular GraphQL framework, gqlgen.
   It aims to reduce the boilerplate code required to define GraphQL schemas manually, thus accelerating the development of GraphQL APIs in Go projects.

AUTHOR:
   VintageOps

GLOBAL OPTIONS:
   --src SRC_PATH, -s SRC_PATH              SRC_PATH is the required path to the source file containing the structs to import (required)
   --use-json-tags, -j                      Use JSON Tag as field name when available. If this is selected and a field has no Json tag, then the field name will be used. (default: false)
   --use-custom-tags value, -c value        Specify a custom tag to use as field name. Specifying this takes precedence over JSON tags. If specifed and a field does not have this tag, the field name will be used
   --tags-value-ignored value, -i value     Specify a tag value that signal to ignore Field with tag having this value. When using json tags with use-json-tags option, if this not specified, it is automatically set to '-'
   --required-tags key=value, -r key=value  If there is a tag that make a field required, specified that tag using the format key=value. e.g. validate=required
   --help, -h                               show help

Running structogqlgen prints the generated Schema Definition on standard output (stdout), the output is segmented into two sections:

  • Custom Scalar Declaration This section displays the Custom Scalars that needs to be defined. It is to be highlighted that gqlgen ships with some built-in helpers for common custom scalar use-cases (Time, Any, Upload and Map). Adding any of these to a schema will automatically add the marshalling behaviour to Go types. Any other Scalar highlighted in this section needs to be implemented, Gqlgen documentation describes how this should be achieved, and there are some packages, such as gql-bingInt, that can help in this endeavour.

  • Graphql Type Definitions

Example:

Using the example in pkg/examples_test/examples_test.go with options to make use of json tags and to use the tag validate when set to "required" for finding the required fields.

~/go/bin/structogqlgen --src pkg/examples_test/examples_test.go --use-json-tags --required-tags validate=required
scalar PublicationStatus
scalar error
scalar interfaceEmpty
scalar interfacevalues
scalar BigInt

type Another {
}

type Article {
  id: Int!
  title: String
  content: String
  author: User
  tags: [String]
  comments: [Comment]
  published_at: Time
  status: PublicationStatus
  error: error
  anything: interfaceEmpty
  do_something: DoSomethingMap
  random_int: BigInt
  another_random_int64: BigInt
  created_at: Time
  updated_at: Time
}

type DoSomethingMap {
  key: String
  values: interfacevalues
}

type CMSData {
  users: [User]
  articles: [Article]
  article_comments: ArticleCommentsMap
}

type ArticleCommentsMap {
  key: Int
  values: [Int]
}

type Comment {
  id: Int
  article_id: Int
  author: User
  content: String
  created_at: Time
  updated_at: Time
}

type Metadata {
  created_at: Time
  updated_at: Time
}

type User {
  id: Int
  username: String
  email: String
  verified: Boolean
  created_at: Time
  updated_at: Time
}

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
pkg
conversion
Package conversion provides tools to systematically convert Go data structures into GraphQL schema elements including types and fields, based on the type information extracted from Go source files using functions from package github.com/VintageOps/structogqlgen/pkg/load
Package conversion provides tools to systematically convert Go data structures into GraphQL schema elements including types and fields, based on the type information extracted from Go source files using functions from package github.com/VintageOps/structogqlgen/pkg/load
load
Package load provides functionalities to parse and analyze Go source files to discover and retrieve struct type definitions.
Package load provides functionalities to parse and analyze Go source files to discover and retrieve struct type definitions.

Jump to

Keyboard shortcuts

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