Documentation
¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AirConf = `` /* 1376-byte string literal not displayed */
AirConf config for air, the reloader for dev
var Constants = `package gen
import (
)
type key int
// consts ...
const (
KeyPrincipalID key = iota
KeyLoaders key = iota
KeyExecutableSchema key = iota
KeyJWTClaims key = iota
KeyHTTPRequest key = iota
KeyMutationTransaction key = iota
KeyMutationEvents key = iota
SchemaSDL string = ` + "`{{.SchemaSDL}}`" + `
)
`
Constants ...
var Database = `` /* 3085-byte string literal not displayed */
Database ...
var DockerComposeYml = `` /* 666-byte string literal not displayed */
DockerComposeYml file
var Dockerfile = `` /* 660-byte string literal not displayed */
Dockerfile ...
var DockerfileDev = `` /* 292-byte string literal not displayed */
DockerfileDev development dockerfile
var DockerfileProd = `` /* 661-byte string literal not displayed */
DockerfileProd production dockerfile
var DotenvExample = `` /* 377-byte string literal not displayed */
DotenvExample example .env file
var DummyModel = `` /* 296-byte string literal not displayed */
DummyModel ...
var EventsController = `` /* 3078-byte string literal not displayed */
EventsController template
var Federation = `` /* 382-byte string literal not displayed */
Federation ...
var Filters = `` /* 5624-byte string literal not displayed */
Filters ...
var GQLGen = `` /* 1197-byte string literal not displayed */
GQLGen ...
var GitIgnore = `` /* 128-byte string literal not displayed */
GitIgnore ignores files no git commands
var HTTPHandler = `package gen
import (
"context"
"fmt"
"net/http"
"os"
"path"
"strings"
"time"
"github.com/rs/zerolog/log"
"github.com/99designs/gqlgen/graphql/handler"
"github.com/99designs/gqlgen/graphql/handler/apollotracing"
"github.com/99designs/gqlgen/graphql/handler/extension"
"github.com/99designs/gqlgen/graphql/handler/lru"
"github.com/99designs/gqlgen/graphql/handler/transport"
"github.com/99designs/gqlgen/graphql/playground"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"gopkg.in/gormigrate.v1"
)
// GetHTTPServeMux HTTP Mux
func GetHTTPServeMux(r ResolverRoot, db *DB, migrations []*gormigrate.Migration) *mux.Router {
mux := mux.NewRouter()
executableSchema := NewExecutableSchema(Config{Resolvers: r})
gqlHandler := handler.New(executableSchema)
gqlHandler.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
})
gqlHandler.AddTransport(transport.Options{})
gqlHandler.AddTransport(transport.GET{})
gqlHandler.AddTransport(transport.POST{})
gqlHandler.AddTransport(transport.MultipartForm{})
gqlHandler.Use(extension.FixedComplexityLimit(300))
if os.Getenv("DEBUG") == "true" {
gqlHandler.Use(extension.Introspection{})
}
gqlHandler.Use(apollotracing.Tracer{})
gqlHandler.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
loaders := GetLoaders(db)
if os.Getenv("EXPOSE_MIGRATION_ENDPOINT") == "true" {
mux.HandleFunc(os.Getenv("API_VERSION")+"/migrate", func(res http.ResponseWriter, req *http.Request) {
err := db.Migrate(migrations)
if err != nil {
http.Error(res, err.Error(), 400)
}
fmt.Fprintf(res, "OK")
})
mux.HandleFunc(os.Getenv("API_VERSION")+"/automigrate", func(res http.ResponseWriter, req *http.Request) {
err := db.AutoMigrate()
if err != nil {
http.Error(res, err.Error(), 400)
}
fmt.Fprintf(res, "OK")
})
}
gqlBasePath := os.Getenv("API_GRAPHQL_BASE_RESOURCE")
if gqlBasePath == "" {
gqlBasePath = "/graphql"
}
mux.HandleFunc(os.Getenv("API_VERSION")+gqlBasePath, func(res http.ResponseWriter, req *http.Request) {
ctx := initContextWithJWTClaims(req)
ctx = context.WithValue(ctx, KeyLoaders, loaders)
ctx = context.WithValue(ctx, KeyExecutableSchema, executableSchema)
req = req.WithContext(ctx)
gqlHandler.ServeHTTP(res, req)
})
if os.Getenv("EXPOSE_PLAYGROUND_ENDPOINT") == "true" {
playgroundHandler := playground.Handler("GraphQL playground", os.Getenv("API_VERSION")+gqlBasePath)
mux.HandleFunc(os.Getenv("API_VERSION")+gqlBasePath+"/playground", func(res http.ResponseWriter, req *http.Request) {
ctx := initContextWithJWTClaims(req)
ctx = context.WithValue(ctx, KeyLoaders, loaders)
ctx = context.WithValue(ctx, KeyExecutableSchema, executableSchema)
req = req.WithContext(ctx)
if req.Method == "GET" {
playgroundHandler(res, req)
}
})
}
return mux
}
// GetHTTPHandler HTTP func Handler
func GetHTTPHandler(r ResolverRoot, db *DB, migrations []*gormigrate.Migration, res http.ResponseWriter, req *http.Request) {
if os.Getenv("DEBUG") == "true" {
log.Debug().Msgf("Path base: %s", path.Base(req.URL.Path))
}
executableSchema := NewExecutableSchema(Config{Resolvers: r})
gqlHandler := handler.New(executableSchema)
gqlHandler.AddTransport(transport.Websocket{
KeepAlivePingInterval: 10 * time.Second,
})
gqlHandler.AddTransport(transport.Options{})
gqlHandler.AddTransport(transport.GET{})
gqlHandler.AddTransport(transport.POST{})
gqlHandler.AddTransport(transport.MultipartForm{})
gqlHandler.Use(extension.FixedComplexityLimit(300))
if os.Getenv("DEBUG") == "true" {
gqlHandler.Use(extension.Introspection{})
}
gqlHandler.Use(apollotracing.Tracer{})
gqlHandler.Use(extension.AutomaticPersistedQuery{
Cache: lru.New(100),
})
loaders := GetLoaders(db)
if os.Getenv("EXPOSE_MIGRATION_ENDPOINT") == "true" {
if path.Base(req.URL.Path) == "migrate" {
err := db.Migrate(migrations)
if err != nil {
http.Error(res, err.Error(), 400)
}
fmt.Fprintf(res, "OK")
}
if path.Base(req.URL.Path) == "automigrate" {
err := db.AutoMigrate()
if err != nil {
http.Error(res, err.Error(), 400)
}
fmt.Fprintf(res, "OK")
}
ctx := context.WithValue(req.Context(), KeyJWTClaims, claims)
ctx = context.WithValue(ctx, KeyHTTPRequest, req)
if principalID != nil {
ctx = context.WithValue(ctx, KeyPrincipalID, principalID)
}
}
gqlBasePath := os.Getenv("API_GRAPHQL_BASE_RESOURCE")
if gqlBasePath == "" {
gqlBasePath = "graphql"
}
if path.Base(req.URL.Path) == gqlBasePath {
ctx := initContextWithJWTClaims(req)
ctx = context.WithValue(ctx, KeyLoaders, loaders)
ctx = context.WithValue(ctx, KeyExecutableSchema, executableSchema)
req = req.WithContext(ctx)
gqlHandler.ServeHTTP(res, req)
}
if os.Getenv("EXPOSE_PLAYGROUND_ENDPOINT") == "true" && path.Base(req.URL.Path) == "playground" {
playgroundHandler := playground.Handler("GraphQL playground", gqlBasePath)
ctx := initContextWithJWTClaims(req)
ctx = context.WithValue(ctx, KeyLoaders, loaders)
ctx = context.WithValue(ctx, KeyExecutableSchema, executableSchema)
req = req.WithContext(ctx)
if req.Method == "GET" {
playgroundHandler(res, req)
}
}
}
func initContextWithJWTClaims(req *http.Request) context.Context {
claims, _ := getJWTClaims(req)
var principalID *string
if claims != nil {
principalID = &(*claims).Subject
}
ctx := context.WithValue(req.Context(), KeyJWTClaims, claims)
if principalID != nil {
ctx = context.WithValue(ctx, KeyPrincipalID, principalID)
}
return ctx
}
// GetPrincipalIDFromContext ...
func GetPrincipalIDFromContext(ctx context.Context) *string {
v, _ := ctx.Value(KeyPrincipalID).(*string)
return v
}
// GetJWTClaimsFromContext method
func GetJWTClaimsFromContext(ctx context.Context) *JWTClaims {
val, _ := ctx.Value(KeyJWTClaims).(*JWTClaims)
return val
}
// GetHTTPRequestFromContext ...
func GetHTTPRequestFromContext(ctx context.Context) *http.Request {
v, _ := ctx.Value(KeyHTTPRequest).(*http.Request)
return v
}
// JWTClaims JWT Claims
type JWTClaims struct {
jwtgo.StandardClaims
Scope *string ` + "`" + `json:"scope,omitempty"` + "`" + `
Email string ` + "`" + `json:"email"` + "`" + `
Name string ` + "`" + `json:"name"` + "`" + `
Nickname string ` + "`" + `json:"nickname"` + "`" + `
Picture string ` + "`" + `json:"avatar,omitempty"` + "`" + `
Roles []string ` + "`" + `json:"roles,omitempty"` + "`" + `
Permissions map[string]string ` + "`" + `json:"permissions,omitempty"` + "`" + `
}
func getJWTClaims(req *http.Request) (*JWTClaims, error) {
var p *JWTClaims
tokenStr := strings.Replace(req.Header.Get("authorization"), "Bearer ", "", 1)
if tokenStr == "" {
return p, nil
}
p = &JWTClaims{}
_, err := jwtgo.ParseWithClaims(tokenStr, p, nil)
if err != nil {
return p, err
}
return p, nil
}
// Scopes ...
func (c *JWTClaims) Scopes() []string {
s := c.Scope
if s != nil && len(*s) > 0 {
return strings.Split(*s, " ")
}
return []string{}
}
// HasScope ...
func (c *JWTClaims) HasScope(scope string) bool {
for _, s := range c.Scopes() {
if s == scope {
return true
}
}
return false
}
// Permission Constants
const (
JWTPermissionConstCreate = "create"
JWTPermissionConstRead = "read"
JWTPermissionConstUpdate = "update"
JWTPermissionConstDelete = "delete"
JWTPermissionConstList = "list"
)
// HasPermission method checks if claims have an [e]ntity's [p]ermission
func HasPermission(c *JWTClaims, e string, p string) bool {
return strings.Contains(c.Permissions[e], p)
}
// HasRole method checks if claims has a specific [r]ole
func HasRole(c *JWTClaims, r string) bool {
for _, role := range c.Roles {
if r == role {
return true
}
}
return false
}
`
HTTPHandler ...
var Lambda = `` /* 412-byte string literal not displayed */
Lambda ...
var Loaders = `` /* 1407-byte string literal not displayed */
Loaders ...
var Main = `` /* 3793-byte string literal not displayed */
Main template
var Makefile = `# Makefile for {{.Config.Package}}
generate:
GO111MODULE=on go run github.com/loopcontext/go-graphql-orm
reinit:
GO111MODULE=on go run github.com/loopcontext/go-graphql-orm init
migrate:
DATABASE_URL=sqlite3://dev.db PORT=8081 go run *.go migrate
automigrate:
DATABASE_URL=sqlite3://dev.db PORT=8081 go run *.go automigrate
run:
DATABASE_URL=sqlite3://dev.db PORT=8081 go run *.go start --cors
debug:
DEBUG=true DATABASE_URL=sqlite3://dev.db PORT=8081 go run *.go start --cors
voyager:
docker run --rm -v ` + "`" + `pwd` + "`" + `/gen/schema.graphql:/app/schema.graphql -p 8082:80 graphql/voyager
build-lambda-function:
GO111MODULE=on GOOS=linux CGO_ENABLED=0 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 && (ENABLE_DELETE_ALL_RESOLVERS=true 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 && (ENABLE_DELETE_ALL_RESOLVERS=true 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):8081/graphql loopcontext/godog-graphql
`
Makefile ...
var MiddlewareJWT = `` /* 5706-byte string literal not displayed */
MiddlewareJWT template
var Migrations = `` /* 2066-byte string literal not displayed */
Migrations ...
var MigrationsSrc = `` /* 490-byte string literal not displayed */
MigrationsSrc ...
var Model = `package gen
import (
"fmt"
"reflect"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/mitchellh/mapstructure"
)
{{range $object := .Model.ObjectEntities}}
// {{.Name}}ResultType struct
type {{.Name}}ResultType struct {
EntityResultType
}
// {{.Name}} struct
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}}
}
// IsEntity ...
func (m *{{.Name}}) IsEntity() {}
{{range $interface := $object.Interfaces}}
// Is{{$interface}} ...
func (m *{{$object.Name}}) Is{{$interface}}() {}
{{end}}
// {{.Name}}Changes struct
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}}
// {{$rel.ManyToManyObjectNameCC}} struct
type {{$rel.ManyToManyObjectNameCC}} struct {
{{$rel.ForeignKeyDestinationColumnCC}} string
{{$rel.InverseRelationship.ForeignKeyDestinationColumnCC}} string
}
// TableName ...
func ({{$rel.ManyToManyObjectNameCC}}) TableName() string {
return TableName("{{$rel.ManyToManyJoinTable}}")
}
{{end}}
{{end}}
{{end}}
// ApplyChanges 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)
}
`
Model ...
var QueryFilters = `` /* 2543-byte string literal not displayed */
QueryFilters ...
var ResolverCore = `` /* 3144-byte string literal not displayed */
ResolverCore ...
var ResolverExtensions = `` /* 1853-byte string literal not displayed */
ResolverExtensions ...
var ResolverFederation = `` /* 1484-byte string literal not displayed */
ResolverFederation ...
var ResolverMutations = `` /* 10388-byte string literal not displayed */
ResolverMutations template
var ResolverQueries = `` /* 11283-byte string literal not displayed */
ResolverQueries ...
var ResolverSrc = `` /* 1112-byte string literal not displayed */
ResolverSrc ...
var ResolverSrcExt = `` /* 4055-byte string literal not displayed */
ResolverSrcExt template
var ResolverSrcGen = `` /* 2753-byte string literal not displayed */
ResolverSrcGen ...
var ResultType = `` /* 7495-byte string literal not displayed */
ResultType ...
var RunDevSh = `` /* 296-byte string literal not displayed */
RunDevSh ...
var RunSh = `` /* 357-byte string literal not displayed */
RunSh ...
var Sorting = `` /* 1515-byte string literal not displayed */
Sorting ...
var UtilTools = `` /* 1213-byte string literal not displayed */
UtilTools template
Functions ¶
func WriteTemplate ¶
func WriteTemplate(t, filename string, data TemplateData) error
WriteTemplate ...
func WriteTemplateRaw ¶
WriteTemplateRaw ...
Types ¶
Source Files
¶
- constants.go
- database.go
- dockerfiles-template.go
- dummy-model.go
- events-controller.go
- federation.go
- filters.go
- gqlgen.yml.go
- helpers.go
- http-handler.go
- lambda.go
- loaders.go
- main.go
- makefile.go
- middleware-jwt-src.go
- migrations-src.go
- migrations.go
- model.go
- query-filter.go
- resolver-core.go
- resolver-extensions.go
- resolver-federation.go
- resolver-mutations.go
- resolver-queries.go
- resolver-src-ext.go
- resolver-src-gen.go
- resolver-src-main.go
- result-type.go
- sorting.go
- utils-src.go