github.com/dennys-bd/goals@v0.0.0-20210328114421-251a004d41e3/cmd/basic_templates.go (about)

     1  package cmd
     2  
     3  func basicTemplates() {
     4  	templates["schema"] = `package schema
     5  	
     6  import (
     7  	"github.com/dennys-bd/goals/core"
     8  	"{{.importpath}}/app/scalar"
     9  )
    10  
    11  // Concat here your types, queries, mutations and
    12  // subscriptions that will be in the schema, e.g.
    13  // const queries = userQueries +
    14  // shoppingQueries
    15  
    16  const {{if .name}}{{.name}}T{{else}}t{{end}}ypes = ""
    17  
    18  const {{if .name}}{{.name}}Q{{else}}q{{end}}ueries = ""
    19  
    20  const {{if .name}}{{.name}}M{{else}}m{{end}}utations = ""
    21  
    22  const {{if .name}}{{.name}}S{{else}}s{{end}}ubscriptions = ""
    23  
    24  // Get{{.Name}}Schema returns the schema String
    25  func Get{{.Name}}Schema() core.Schema {
    26  	{{if .name}}return core.MountSchema("Schema", {{.name}}Types, {{.name}}Queries, {{.name}}Mutations, {{.name}}Subscriptions, scalar.Scalars)
    27  	{{else}}return core.MountSchema("{{.Name}}Schema", types, queries, mutations, subscriptions, scalar.Scalars){{end}}
    28  }
    29  `
    30  
    31  	templates["resolver"] = `package resolver
    32  {{if .importpath}}
    33  import (
    34  	"context"
    35  
    36  	"{{.importpath}}/app/model"
    37  	"{{.importpath}}/lib"
    38  )
    39  {{end}}
    40  // {{.name}}Resolver type for graphql
    41  type {{.name}}Resolver {{if .model}}struct { 
    42  	{{.abbreviation}} model.{{.model}}
    43  }{{else}}struct{}{{end}}
    44  {{if .name}}
    45  // FillAuthStruct puts the user inside the AuthResolver
    46  func (r *{{.name}}Resolver) FillAuthStruct(ctx context.Context) {
    47  	// TODO: Use ctx to get auth variables and set the AuthStruct
    48  	// E.G.
    49  	// id := ctx.Value(lib.ContextKeyAuth)
    50  	// DB.find(id, &r.u)
    51  }
    52  
    53  // GetAuthHeaders put in the context the headers you want
    54  // from the original request to the resolver
    55  func (r *{{.name}}Resolver) GetAuthHeaders() []string {
    56  	// TODO: return here the headers you want
    57  	// to come in context from the original request
    58  	return []string{lib.ContextKeyAuth.String()}
    59  }
    60  {{end}}`
    61  }