Graphql Schema Generator
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)}}})
}
- Use the webpage
- Use the graphql Api to get Go code from a schema string
{
generateGoCode(schemaStr: "your schema file at string the \n(breaks are importent) ")
}
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))
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
},
},
- 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
- understand schemas
- generate code for schemas
- understand deprecated
- [] generate code for deprecated
- understand enums
- [] generate code for enums
GNU General Public License v3.0