templates

package
v0.4.7 Latest Latest
Warning

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

Go to latest
Published: Mar 20, 2020 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Constants = `package gen

import (
)

type key int

const (
	KeyPrincipalID      	key    = iota
	KeyLoaders          	key    = iota
	KeyExecutableSchema 	key    = iota
	KeyJWTClaims        	key    = iota
	KeyMutationTransaction	key    = iota
	KeyMutationEvents		key    = iota
	SchemaSDL string = ` + "`{{.SchemaSDL}}`" + `
)
`
View Source
var Database = `` /* 3042-byte string literal not displayed */
View Source
var Dockerfile = `` /* 647-byte string literal not displayed */
View Source
var DummyModel = `` /* 296-byte string literal not displayed */
View Source
var Federation = `` /* 343-byte string literal not displayed */
View Source
var Filters = `` /* 3883-byte string literal not displayed */
View Source
var GQLGen = `` /* 1274-byte string literal not displayed */
View Source
var GeneratedResolver = `` /* 15128-byte string literal not displayed */
View Source
var HTTPHandler = `` /* 2447-byte string literal not displayed */
View Source
var Lambda = `` /* 461-byte string literal not displayed */
View Source
var Loaders = `` /* 1346-byte string literal not displayed */
View Source
var Main = `` /* 3178-byte string literal not displayed */
View Source
var Makefile = `generate:
	GO111MODULE=on go run github.com/novacloudcz/graphql-orm

reinit:
	GO111MODULE=on go run github.com/novacloudcz/graphql-orm init

migrate:
	DATABASE_URL=sqlite3://test.db PORT=8080 go run *.go migrate

automigrate:
	DATABASE_URL=sqlite3://test.db PORT=8080 go run *.go automigrate

run:
	DATABASE_URL=sqlite3://test.db PORT=8080 go run *.go start --cors

voyager:
	docker run --rm -v ` + "`" + `pwd` + "`" + `/gen/schema.graphql:/app/schema.graphql -p 8080:80 graphql/voyager

build-lambda-function:
	GO111MODULE=on GOOS=linux go build -o main lambda/main.go && zip lambda.zip main && rm main

test-sqlite:
	GO111MODULE=on go build -o app *.go && DATABASE_URL=sqlite3://test.db ./app migrate && (DATABASE_URL=sqlite3://test.db PORT=8080 ./app start& export app_pid=$$! && make test-godog || test_result=$$? && kill $$app_pid && exit $$test_result)
test:
	GO111MODULE=on go build -o app *.go && ./app migrate && (PORT=8080 ./app start& export app_pid=$$! && make test-godog || test_result=$$? && kill $$app_pid && exit $$test_result)
// TODO: add detection of host ip (eg. host.docker.internal) for other OS
test-godog:
	docker run --rm --network="host" -v "${PWD}/features:/godog/features" -e GRAPHQL_URL=http://$$(if [[ $${OSTYPE} == darwin* ]]; then echo host.docker.internal;else echo localhost;fi):8080/graphql jakubknejzlik/godog-graphql
`
View Source
var Migrations = `` /* 1593-byte string literal not displayed */
View Source
var MigrationsSrc = `` /* 457-byte string literal not displayed */
View Source
var Model = `package gen

import (
	"fmt"
	"reflect"
	"time"
	
	"github.com/99designs/gqlgen/graphql"
	"github.com/mitchellh/mapstructure"
)

type NotFoundError struct {
	Entity string
}

func (e *NotFoundError) Error() string {
	return fmt.Sprintf("%s not found", e.Entity)
}

{{range $object := .Model.ObjectEntities}}

	type {{.Name}}ResultType struct {
		EntityResultType
	}

	type {{.Name}} struct {
	{{range $col := $object.Columns}}
		{{$col.MethodName}} {{$col.GoType}} ` + "`" + `{{$col.ModelTags}}` + "`" + `{{end}}

	{{range $rel := $object.Relationships}}
	{{$rel.MethodName}} {{$rel.GoType}} ` + "`" + `{{$rel.ModelTags}}` + "`" + `
	{{if $rel.Preload}}{{$rel.MethodName}}Preloaded bool ` + "`gorm:\"-\"`" + `{{end}}
	{{end}}
	}

	func (m *{{.Name}}) Is_Entity() {}

	{{range $interface := $object.Interfaces}}
	func (m *{{$object.Name}}) Is{{$interface}}() {}
	{{end}}

	type {{.Name}}Changes struct {
		{{range $col := $object.Columns}}
		{{$col.MethodName}} {{$col.InputTypeName}}{{end}}
		{{range $rel := $object.Relationships}}{{if $rel.IsToMany}}
		{{$rel.ChangesName}} {{$rel.ChangesType}}{{end}}{{end}}
	}

	{{range $rel := $object.Relationships}}
		{{if and $rel.IsManyToMany $rel.IsMainRelationshipForManyToMany}}
		type {{$rel.ManyToManyObjectName}} struct {
			{{$rel.ForeignKeyDestinationColumn}} string
			{{$rel.InverseRelationship.ForeignKeyDestinationColumn}} string
		}
		func ({{$rel.ManyToManyObjectName}}) TableName() string {
			return "{{$rel.ManyToManyJoinTable}}"
		}
		{{end}}
	{{end}}
{{end}}

// used to convert map[string]interface{} to EntityChanges struct
func ApplyChanges(changes map[string]interface{}, to interface{}) error {
	dec, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
		ErrorUnused: true,
		TagName:     "json",
		Result:      to,
		ZeroFields:  true,
		// This is needed to get mapstructure to call the gqlgen unmarshaler func for custom scalars (eg Date)
		DecodeHook: func(a reflect.Type, b reflect.Type, v interface{}) (interface{}, error) {

			if b == reflect.TypeOf(time.Time{}) {
				switch a.Kind() {
				case reflect.String:
					return time.Parse(time.RFC3339, v.(string))
				case reflect.Float64:
					return time.Unix(0, int64(v.(float64))*int64(time.Millisecond)), nil
				case reflect.Int64:
					return time.Unix(0, v.(int64)*int64(time.Millisecond)), nil
				default:
					return v, fmt.Errorf("Unable to parse date from %v", v)
				}
			}

			if reflect.PtrTo(b).Implements(reflect.TypeOf((*graphql.Unmarshaler)(nil)).Elem()) {
				resultType := reflect.New(b)
				result := resultType.MethodByName("UnmarshalGQL").Call([]reflect.Value{reflect.ValueOf(v)})
				err, _ := result[0].Interface().(error)
				return resultType.Elem().Interface(), err
			}

			return v, nil
		},
	})

	if err != nil {
		return err
	}

	return dec.Decode(changes)
}
`
View Source
var QueryFilters = `` /* 2494-byte string literal not displayed */
View Source
var ResolverCore = `` /* 2845-byte string literal not displayed */
View Source
var ResolverExtensions = `` /* 1627-byte string literal not displayed */
View Source
var ResolverFederation = `` /* 3118-byte string literal not displayed */
View Source
var ResolverMutations = `` /* 8847-byte string literal not displayed */
View Source
var ResolverQueries = `` /* 7588-byte string literal not displayed */
View Source
var ResolverSrc = `` /* 595-byte string literal not displayed */
View Source
var ResolverSrcGen = `` /* 2086-byte string literal not displayed */
View Source
var ResultType = `` /* 3390-byte string literal not displayed */
View Source
var Sorting = `` /* 1135-byte string literal not displayed */

Functions

func WriteTemplate

func WriteTemplate(t, filename string, data TemplateData) error

func WriteTemplateRaw

func WriteTemplateRaw(t, filename string, data interface{}) error

Types

type TemplateData

type TemplateData struct {
	Model     *model.Model
	Config    *model.Config
	RawSchema *string
}

Jump to

Keyboard shortcuts

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