Giter Club home page Giter Club logo

graphqlschemagenerator-go's Introduction

Project name

Graphql Schema Generator

Description

Generate Go code from a GraphQL schema. For example:

type test {
  id: Int!
  name: [String]
}

will generate:

package schema

import graphql "github.com/graphql-go/graphql"

type Test struct {
        Id   int      `json:"id"`
        Name []string `json:"name"`
}

func GetTest() *graphql.Object {
        return graphql.NewObject(graphql.ObjectConfig{Name: "test", Fields: graphql.Fields{"id": &graphql.Field{Type: graphql.NewNonNull(graphql.Int)}, "name": &graphql.Field{Type: graphql.NewList(graphql.String)}}})
}

Usage

Generate Go Code

Simple

Advanced

  • Use the graphql Api to get Go code from a schema string
{
 generateGoCode(schemaStr: "your schema file at string the \n(breaks are importent) ")
}

Expert

go get github.com/fasibio/graphqlSchemaGenerator-go

import (
	"github.com/fasibio/graphqlSchemaGenerator-go/goCodeGenerator"
	"github.com/fasibio/graphqlSchemaGenerator-go/schemaInterpretations"
)
  //return the Golang Code (String)
  goCode := goCodeGenerator.GetGenerateFile(schemaInterpretations.GetSchemaList(schemaStr),   schemaInterpretations.GetEnumList(schemaStr))

Use the generated code in your Go application

See https://github.com/graphql-go/graphql to learn how to use GraphQL in Go

  • Generate a folder/package schema in your project
  • Copy the generated Go file to a folder schema (for example a schema.go)
  • Create GraphQL queries and set the type as the generated Graphql Schema you want to use. As result you can fill the generated struct
	fields := graphql.Fields{
		"test": &graphql.Field{
			Type: schema.GetTest(),// <- this is the generated Funktion
			Args: graphql.FieldConfigArgument{
				"type": &graphql.ArgumentConfig{
					Type:         graphql.String,
					DefaultValue: "developer",
				},
			},

			Resolve: func(p graphql.ResolveParams) (interface{}, error) {
				var result schema.Test // <- this is the generated Struct
					result = schema.Test{
						Id:   "MUHAHAH",
						Name: "Fasibio",
					}

				return result, nil
			},
		},

Notes

  • The generator is a work in progress
  • I'm happy if you want to help to make it better
  • So feel free to open an issue or send pull requests

TODO

  • understand schemas
  • generate code for schemas
    • understand deprecated
    • [] generate code for deprecated
  • understand enums
  • [] generate code for enums

License

GNU General Public License v3.0

graphqlschemagenerator-go's People

Contributors

fasibio avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.