more miscellaneous fixes while integrating into webapp

This commit is contained in:
Ben Kraft
2021-04-01 15:04:36 -07:00
parent fbf00f3bef
commit 9fe8f09e8c
20 changed files with 44 additions and 31 deletions
+1 -2
View File
@@ -2,6 +2,5 @@
package: example package: example
schema: schema.graphql schema: schema.graphql
queries: queries:
- queries.graphql - genqlient.graphql
generated: generated.go generated: generated.go
use_context: true
+16 -5
View File
@@ -12,7 +12,7 @@ import (
var defaultConfig = &Config{ var defaultConfig = &Config{
Schema: "schema.graphql", Schema: "schema.graphql",
Queries: []string{"queries.graphql"}, Operations: []string{"genqlient.graphql"},
Generated: "generated.go", Generated: "generated.go",
ContextType: "context.Context", ContextType: "context.Context",
} }
@@ -24,12 +24,13 @@ type Config struct {
// how to convert that to SDL). // how to convert that to SDL).
Schema string `yaml:"schema"` Schema string `yaml:"schema"`
// Filenames or globs with the queries; defaults to queries.graphql. // Filenames or globs with the operations for which to generate code;
// defaults to genqlient.graphql.
// //
// These may be .graphql files, containing the queries in SDL format, or // These may be .graphql files, containing the queries in SDL format, or
// Go files, in which case any string-literal starting with (optional // Go files, in which case any string-literal starting with (optional
// whitespace and) the string "# @genqlient" will be extracted as a query. // whitespace and) the string "# @genqlient" will be extracted as a query.
Queries []string `yaml:"queries"` Operations []string `yaml:"operations"`
// If set, a file at this path will be generated containing the exact // If set, a file at this path will be generated containing the exact
// operations that genqlient will send to the server. // operations that genqlient will send to the server.
@@ -41,6 +42,7 @@ type Config struct {
// {"operations": [{ // {"operations": [{
// "operationName": "operationname", // "operationName": "operationname",
// "query": "query operationName { ... }", // "query": "query operationName { ... }",
// "sourceLocation": "myqueriesfile.graphql",
// }]} // }]}
// Keys may be added in the future. // Keys may be added in the future.
// //
@@ -74,8 +76,8 @@ func (c *Config) ValidateAndFillDefaults(configFilename string) error {
// Make paths relative to config dir // Make paths relative to config dir
configDir := filepath.Dir(configFilename) configDir := filepath.Dir(configFilename)
c.Schema = filepath.Join(configDir, c.Schema) c.Schema = filepath.Join(configDir, c.Schema)
for i := range c.Queries { for i := range c.Operations {
c.Queries[i] = filepath.Join(configDir, c.Queries[i]) c.Operations[i] = filepath.Join(configDir, c.Operations[i])
} }
c.Generated = filepath.Join(configDir, c.Generated) c.Generated = filepath.Join(configDir, c.Generated)
@@ -105,6 +107,15 @@ func (c *Config) ContextPackage() string {
return c.ContextType[:i] return c.ContextType[:i]
} }
func (c *Config) ContextTypeReference() string {
if c.ContextType == "" {
return ""
}
i := strings.LastIndex(c.ContextType, "/")
return c.ContextType[i+1:]
}
func ReadAndValidateConfig(filename string) (*Config, error) { func ReadAndValidateConfig(filename string) (*Config, error) {
config := *defaultConfig config := *defaultConfig
if filename != "" { if filename != "" {
+10 -7
View File
@@ -41,6 +41,8 @@ type operation struct {
Args []argument `json:"-"` Args []argument `json:"-"`
// The type-name for the operation's response type. // The type-name for the operation's response type.
ResponseName string `json:"-"` ResponseName string `json:"-"`
// The original location of this query.
SourceLocation string `json:"sourceLocation"`
} }
type exportedOperations struct { type exportedOperations struct {
@@ -96,8 +98,8 @@ func (g *generator) getDocComment(op *ast.OperationDefinition) string {
var commentLines []string var commentLines []string
sourceLines := strings.Split(op.Position.Src.Input, "\n") sourceLines := strings.Split(op.Position.Src.Input, "\n")
for i := op.Position.Line - 1; i > 0; i-- { for i := op.Position.Line - 1; i > 0; i-- {
line := sourceLines[i-1] line := strings.TrimSpace(sourceLines[i-1])
if strings.HasPrefix(line, "#") { if strings.HasPrefix(line, "#") && !strings.HasPrefix(line, "# @genqlient") {
commentLines = append(commentLines, commentLines = append(commentLines,
"// "+strings.TrimSpace(strings.TrimPrefix(line, "#"))) "// "+strings.TrimSpace(strings.TrimPrefix(line, "#")))
} else { } else {
@@ -141,9 +143,10 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error {
Name: op.Name, Name: op.Name,
Doc: g.getDocComment(op), Doc: g.getDocComment(op),
// The newline just makes it format a little nicer // The newline just makes it format a little nicer
Body: "\n" + builder.String(), Body: "\n" + builder.String(),
Args: args, Args: args,
ResponseName: responseName, ResponseName: responseName,
SourceLocation: op.Position.Src.Name,
}) })
return nil return nil
@@ -156,7 +159,7 @@ func Generate(config *Config) (map[string][]byte, error) {
return nil, err return nil, err
} }
document, err := getAndValidateQueries(config.Queries, schema) document, err := getAndValidateQueries(config.Operations, schema)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -165,7 +168,7 @@ func Generate(config *Config) (map[string][]byte, error) {
// package-name, if it turns out to be more convenient that way. (As-is, // package-name, if it turns out to be more convenient that way. (As-is,
// we generate a broken file, with just (unused) imports.) // we generate a broken file, with just (unused) imports.)
if len(document.Operations) == 0 { if len(document.Operations) == 0 {
return nil, fmt.Errorf("no queries found in %v", config.Queries) return nil, fmt.Errorf("no queries found in %v", config.Operations)
} }
g := newGenerator(config, schema) g := newGenerator(config, schema)
+1 -1
View File
@@ -65,7 +65,7 @@ func TestGenerate(t *testing.T) {
t.Run(graphqlFilename, func(t *testing.T) { t.Run(graphqlFilename, func(t *testing.T) {
generated, err := Generate(&Config{ generated, err := Generate(&Config{
Schema: filepath.Join(dataDir, "schema.graphql"), Schema: filepath.Join(dataDir, "schema.graphql"),
Queries: []string{filepath.Join(dataDir, graphqlFilename)}, Operations: []string{filepath.Join(dataDir, graphqlFilename)},
Package: "test", Package: "test",
Generated: goFilename, Generated: goFilename,
ExportOperations: queriesFilename, ExportOperations: queriesFilename,
+2 -2
View File
@@ -3,7 +3,7 @@ package {{.Config.Package}}
// Code generated by github.com/Khan/genqlient, DO NOT EDIT. // Code generated by github.com/Khan/genqlient, DO NOT EDIT.
import ( import (
{{- if .Config.ContextType -}} {{if .Config.ContextType -}}
"{{.Config.ContextPackage}}" "{{.Config.ContextPackage}}"
{{end}} {{end}}
{{- if .ImportJSON -}} {{- if .ImportJSON -}}
@@ -21,7 +21,7 @@ import (
{{.Doc}} {{.Doc}}
func {{.Name}}( func {{.Name}}(
{{if $.Config.ContextType -}} {{if $.Config.ContextType -}}
ctx {{$.Config.ContextType}}, ctx {{$.Config.ContextTypeReference}},
{{end}} {{end}}
{{- if not $.Config.ClientGetter -}} {{- if not $.Config.ClientGetter -}}
client graphql.Client, client graphql.Client,
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"InputObjectQuery","query":"\nquery InputObjectQuery ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n"}]} {"operations":[{"operationName":"InputObjectQuery","query":"\nquery InputObjectQuery ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/InputObject.graphql"}]}
@@ -1 +1 @@
{"operations":[{"operationName":"InterfaceNoFragmentsQuery","query":"\nquery InterfaceNoFragmentsQuery {\n\troot {\n\t\tid\n\t\tname\n\t\tchildren {\n\t\t\tid\n\t\t\tname\n\t\t}\n\t}\n}\n"}]} {"operations":[{"operationName":"InterfaceNoFragmentsQuery","query":"\nquery InterfaceNoFragmentsQuery {\n\troot {\n\t\tid\n\t\tname\n\t\tchildren {\n\t\t\tid\n\t\t\tname\n\t\t}\n\t}\n}\n","sourceLocation":"testdata/queries/InterfaceNoFragments.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"ListInputQuery","query":"\nquery ListInputQuery ($names: [String]) {\n\tuser(query: {names:$names}) {\n\t\tid\n\t}\n}\n"}]} {"operations":[{"operationName":"ListInputQuery","query":"\nquery ListInputQuery ($names: [String]) {\n\tuser(query: {names:$names}) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/ListInput.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"QueryWithAlias","query":"\nquery QueryWithAlias {\n\tUser: user {\n\t\tID: id\n\t}\n}\n"}]} {"operations":[{"operationName":"QueryWithAlias","query":"\nquery QueryWithAlias {\n\tUser: user {\n\t\tID: id\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithAlias.graphql"}]}
@@ -1 +1 @@
{"operations":[{"operationName":"QueryWithDoubleAlias","query":"\nquery QueryWithDoubleAlias {\n\tuser {\n\t\tID: id\n\t\tAlsoID: id\n\t}\n}\n"}]} {"operations":[{"operationName":"QueryWithDoubleAlias","query":"\nquery QueryWithDoubleAlias {\n\tuser {\n\t\tID: id\n\t\tAlsoID: id\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithDoubleAlias.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"QueryWithEnums","query":"\nquery QueryWithEnums {\n\tuser {\n\t\troles\n\t}\n\totherUser: user {\n\t\troles\n\t}\n}\n"}]} {"operations":[{"operationName":"QueryWithEnums","query":"\nquery QueryWithEnums {\n\tuser {\n\t\troles\n\t}\n\totherUser: user {\n\t\troles\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithEnums.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"QueryWithSlices","query":"\nquery QueryWithSlices {\n\tuser {\n\t\temails\n\t\temailsOrNull\n\t\temailsWithNulls\n\t\temailsWithNullsOrNull\n\t}\n}\n"}]} {"operations":[{"operationName":"QueryWithSlices","query":"\nquery QueryWithSlices {\n\tuser {\n\t\temails\n\t\temailsOrNull\n\t\temailsWithNulls\n\t\temailsWithNullsOrNull\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithSlices.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"QueryWithStructs","query":"\nquery QueryWithStructs {\n\tuser {\n\t\tauthMethods {\n\t\t\tprovider\n\t\t\temail\n\t\t}\n\t}\n}\n"}]} {"operations":[{"operationName":"QueryWithStructs","query":"\nquery QueryWithStructs {\n\tuser {\n\t\tauthMethods {\n\t\t\tprovider\n\t\t\temail\n\t\t}\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithStructs.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"SimpleInputQuery","query":"\nquery SimpleInputQuery ($name: String!) {\n\tuser(query: {name:$name}) {\n\t\tid\n\t}\n}\n"}]} {"operations":[{"operationName":"SimpleInputQuery","query":"\nquery SimpleInputQuery ($name: String!) {\n\tuser(query: {name:$name}) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/SimpleInput.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"SimpleQuery","query":"\nquery SimpleQuery {\n\tuser {\n\t\tid\n\t}\n}\n"}]} {"operations":[{"operationName":"SimpleQuery","query":"\nquery SimpleQuery {\n\tuser {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/SimpleQuery.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"TypeNameQuery","query":"\nquery TypeNameQuery {\n\tuser {\n\t\t__typename\n\t\tid\n\t}\n}\n"}]} {"operations":[{"operationName":"TypeNameQuery","query":"\nquery TypeNameQuery {\n\tuser {\n\t\t__typename\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/TypeName.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"UnionNoFragmentsQuery","query":"\nquery UnionNoFragmentsQuery {\n\trandomLeaf {\n\t\t__typename\n\t}\n}\n"}]} {"operations":[{"operationName":"UnionNoFragmentsQuery","query":"\nquery UnionNoFragmentsQuery {\n\trandomLeaf {\n\t\t__typename\n\t}\n}\n","sourceLocation":"testdata/queries/UnionNoFragments.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"UsesEnumTwiceQuery","query":"\nquery UsesEnumTwiceQuery {\n\tMe: user {\n\t\troles\n\t}\n\tOtherUser: user {\n\t\troles\n\t}\n}\n"}]} {"operations":[{"operationName":"UsesEnumTwiceQuery","query":"\nquery UsesEnumTwiceQuery {\n\tMe: user {\n\t\troles\n\t}\n\tOtherUser: user {\n\t\troles\n\t}\n}\n","sourceLocation":"testdata/queries/UsesEnumTwice.graphql"}]}
+1 -1
View File
@@ -1 +1 @@
{"operations":[{"operationName":"unexported","query":"\nquery unexported ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n"}]} {"operations":[{"operationName":"unexported","query":"\nquery unexported ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/unexported.graphql"}]}