Skip to main content

Type Generation

Using the type generator feature you'll be able to automatically create structs for all of your types defined in the schema.

Generating Types#

At this stage the generator is not a fully fledged cli command, hence you'll need to invoke it in your own space.

schema := dql.NewSchema()
schema.Type("User", func(user *dql.TypeBuilder) {
user.String("name")
user.String("surname")
user.DateTime("birthday")
user.Password("password")
})
schema.Type("Tag", func(tag *dql.TypeBuilder) {
tag.String("name")
tag.Type("posts", "Post").Reverse()
})
schema.Type("Post", func(post *dql.TypeBuilder) {
post.String("title").Lang()
post.String("content")
post.Bool("published")
post.DateTime("created_at")
post.Type("tags", "Tag").Reverse().List()
post.Int("views")
})
err := dql.GenerateTypes(schema, dql.GeneratorOption{
Path: "your_path/models/models.go",
PackageName: "models",
})

It will generate

// Code auto-generated by dqlx DO NOT EDIT!!
package models
import (
"time"
)
var UserFields = []string{"uid", "User.name", "User.surname", "User.birthday", "User.password", "dgraph.type"}
var UserType = "User"
type User struct {
Uid string `json:"uid,omitempty"`
Name string `json:"User.name,omitempty"`
Surname string `json:"User.surname,omitempty"`
Birthday time.Time `json:"User.birthday,omitempty"`
Password string `json:"User.password,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
var TagFields = []string{"uid", "Tag.name", "dgraph.type"}
var TagToPostsEdge = "Tag.posts"
var TagType = "Tag"
type Tag struct {
Uid string `json:"uid,omitempty"`
Name string `json:"Tag.name,omitempty"`
Posts Post `json:"Tag.posts,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}
var PostFields = []string{"uid", "Post.title", "Post.content", "Post.published", "Post.created_at", "Post.views", "dgraph.type"}
var PostToTagsEdge = "Post.tags"
var PostType = "Post"
type Post struct {
Uid string `json:"uid,omitempty"`
Title string `json:"Post.title,omitempty"`
Content string `json:"Post.content,omitempty"`
Published bool `json:"Post.published,omitempty"`
CreatedAt time.Time `json:"Post.created_at,omitempty"`
Tags []Tag `json:"Post.tags,omitempty"`
Views int64 `json:"Post.views,omitempty"`
DType []string `json:"dgraph.type,omitempty"`
}