From 9fe8f09e8cc3dc5cee05d52fd7e043db29a46974 Mon Sep 17 00:00:00 2001 From: Ben Kraft Date: Thu, 1 Apr 2021 15:04:36 -0700 Subject: [PATCH] more miscellaneous fixes while integrating into webapp --- .../{queries.graphql => genqlient.graphql} | 0 example/genqlient.yaml | 3 +-- generate/config.go | 21 ++++++++++++++----- generate/generate.go | 17 ++++++++------- generate/generate_test.go | 2 +- generate/operation.go.tmpl | 4 ++-- .../testdata/queries/InputObject.graphql.json | 2 +- .../queries/InterfaceNoFragments.graphql.json | 2 +- .../testdata/queries/ListInput.graphql.json | 2 +- .../queries/QueryWithAlias.graphql.json | 2 +- .../queries/QueryWithDoubleAlias.graphql.json | 2 +- .../queries/QueryWithEnums.graphql.json | 2 +- .../queries/QueryWithSlices.graphql.json | 2 +- .../queries/QueryWithStructs.graphql.json | 2 +- .../testdata/queries/SimpleInput.graphql.json | 2 +- .../testdata/queries/SimpleQuery.graphql.json | 2 +- .../testdata/queries/TypeName.graphql.json | 2 +- .../queries/UnionNoFragments.graphql.json | 2 +- .../queries/UsesEnumTwice.graphql.json | 2 +- .../testdata/queries/unexported.graphql.json | 2 +- 20 files changed, 44 insertions(+), 31 deletions(-) rename example/{queries.graphql => genqlient.graphql} (100%) diff --git a/example/queries.graphql b/example/genqlient.graphql similarity index 100% rename from example/queries.graphql rename to example/genqlient.graphql diff --git a/example/genqlient.yaml b/example/genqlient.yaml index 40236c1..cd4d851 100644 --- a/example/genqlient.yaml +++ b/example/genqlient.yaml @@ -2,6 +2,5 @@ package: example schema: schema.graphql queries: -- queries.graphql +- genqlient.graphql generated: generated.go -use_context: true diff --git a/generate/config.go b/generate/config.go index 7f6e58c..06e245a 100644 --- a/generate/config.go +++ b/generate/config.go @@ -12,7 +12,7 @@ import ( var defaultConfig = &Config{ Schema: "schema.graphql", - Queries: []string{"queries.graphql"}, + Operations: []string{"genqlient.graphql"}, Generated: "generated.go", ContextType: "context.Context", } @@ -24,12 +24,13 @@ type Config struct { // how to convert that to SDL). 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 // Go files, in which case any string-literal starting with (optional // 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 // operations that genqlient will send to the server. @@ -41,6 +42,7 @@ type Config struct { // {"operations": [{ // "operationName": "operationname", // "query": "query operationName { ... }", + // "sourceLocation": "myqueriesfile.graphql", // }]} // Keys may be added in the future. // @@ -74,8 +76,8 @@ func (c *Config) ValidateAndFillDefaults(configFilename string) error { // Make paths relative to config dir configDir := filepath.Dir(configFilename) c.Schema = filepath.Join(configDir, c.Schema) - for i := range c.Queries { - c.Queries[i] = filepath.Join(configDir, c.Queries[i]) + for i := range c.Operations { + c.Operations[i] = filepath.Join(configDir, c.Operations[i]) } c.Generated = filepath.Join(configDir, c.Generated) @@ -105,6 +107,15 @@ func (c *Config) ContextPackage() string { 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) { config := *defaultConfig if filename != "" { diff --git a/generate/generate.go b/generate/generate.go index 2552524..6fc695a 100644 --- a/generate/generate.go +++ b/generate/generate.go @@ -41,6 +41,8 @@ type operation struct { Args []argument `json:"-"` // The type-name for the operation's response type. ResponseName string `json:"-"` + // The original location of this query. + SourceLocation string `json:"sourceLocation"` } type exportedOperations struct { @@ -96,8 +98,8 @@ func (g *generator) getDocComment(op *ast.OperationDefinition) string { var commentLines []string sourceLines := strings.Split(op.Position.Src.Input, "\n") for i := op.Position.Line - 1; i > 0; i-- { - line := sourceLines[i-1] - if strings.HasPrefix(line, "#") { + line := strings.TrimSpace(sourceLines[i-1]) + if strings.HasPrefix(line, "#") && !strings.HasPrefix(line, "# @genqlient") { commentLines = append(commentLines, "// "+strings.TrimSpace(strings.TrimPrefix(line, "#"))) } else { @@ -141,9 +143,10 @@ func (g *generator) addOperation(op *ast.OperationDefinition) error { Name: op.Name, Doc: g.getDocComment(op), // The newline just makes it format a little nicer - Body: "\n" + builder.String(), - Args: args, - ResponseName: responseName, + Body: "\n" + builder.String(), + Args: args, + ResponseName: responseName, + SourceLocation: op.Position.Src.Name, }) return nil @@ -156,7 +159,7 @@ func Generate(config *Config) (map[string][]byte, error) { return nil, err } - document, err := getAndValidateQueries(config.Queries, schema) + document, err := getAndValidateQueries(config.Operations, schema) if err != nil { 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, // we generate a broken file, with just (unused) imports.) 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) diff --git a/generate/generate_test.go b/generate/generate_test.go index d375630..d1ded8c 100644 --- a/generate/generate_test.go +++ b/generate/generate_test.go @@ -65,7 +65,7 @@ func TestGenerate(t *testing.T) { t.Run(graphqlFilename, func(t *testing.T) { generated, err := Generate(&Config{ Schema: filepath.Join(dataDir, "schema.graphql"), - Queries: []string{filepath.Join(dataDir, graphqlFilename)}, + Operations: []string{filepath.Join(dataDir, graphqlFilename)}, Package: "test", Generated: goFilename, ExportOperations: queriesFilename, diff --git a/generate/operation.go.tmpl b/generate/operation.go.tmpl index 02bacc6..1b6af33 100644 --- a/generate/operation.go.tmpl +++ b/generate/operation.go.tmpl @@ -3,7 +3,7 @@ package {{.Config.Package}} // Code generated by github.com/Khan/genqlient, DO NOT EDIT. import ( - {{- if .Config.ContextType -}} + {{if .Config.ContextType -}} "{{.Config.ContextPackage}}" {{end}} {{- if .ImportJSON -}} @@ -21,7 +21,7 @@ import ( {{.Doc}} func {{.Name}}( {{if $.Config.ContextType -}} - ctx {{$.Config.ContextType}}, + ctx {{$.Config.ContextTypeReference}}, {{end}} {{- if not $.Config.ClientGetter -}} client graphql.Client, diff --git a/generate/testdata/queries/InputObject.graphql.json b/generate/testdata/queries/InputObject.graphql.json index 5e5a766..2407912 100644 --- a/generate/testdata/queries/InputObject.graphql.json +++ b/generate/testdata/queries/InputObject.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"InputObjectQuery","query":"\nquery InputObjectQuery ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n"}]} \ No newline at end of file +{"operations":[{"operationName":"InputObjectQuery","query":"\nquery InputObjectQuery ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/InputObject.graphql"}]} \ No newline at end of file diff --git a/generate/testdata/queries/InterfaceNoFragments.graphql.json b/generate/testdata/queries/InterfaceNoFragments.graphql.json index ca57037..62a9e28 100644 --- a/generate/testdata/queries/InterfaceNoFragments.graphql.json +++ b/generate/testdata/queries/InterfaceNoFragments.graphql.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/ListInput.graphql.json b/generate/testdata/queries/ListInput.graphql.json index de85190..7d7fc93 100644 --- a/generate/testdata/queries/ListInput.graphql.json +++ b/generate/testdata/queries/ListInput.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"ListInputQuery","query":"\nquery ListInputQuery ($names: [String]) {\n\tuser(query: {names:$names}) {\n\t\tid\n\t}\n}\n"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/QueryWithAlias.graphql.json b/generate/testdata/queries/QueryWithAlias.graphql.json index 194ff96..eff1df1 100644 --- a/generate/testdata/queries/QueryWithAlias.graphql.json +++ b/generate/testdata/queries/QueryWithAlias.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"QueryWithAlias","query":"\nquery QueryWithAlias {\n\tUser: user {\n\t\tID: id\n\t}\n}\n"}]} \ No newline at end of file +{"operations":[{"operationName":"QueryWithAlias","query":"\nquery QueryWithAlias {\n\tUser: user {\n\t\tID: id\n\t}\n}\n","sourceLocation":"testdata/queries/QueryWithAlias.graphql"}]} \ No newline at end of file diff --git a/generate/testdata/queries/QueryWithDoubleAlias.graphql.json b/generate/testdata/queries/QueryWithDoubleAlias.graphql.json index 1709bf2..5534d22 100644 --- a/generate/testdata/queries/QueryWithDoubleAlias.graphql.json +++ b/generate/testdata/queries/QueryWithDoubleAlias.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"QueryWithDoubleAlias","query":"\nquery QueryWithDoubleAlias {\n\tuser {\n\t\tID: id\n\t\tAlsoID: id\n\t}\n}\n"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/QueryWithEnums.graphql.json b/generate/testdata/queries/QueryWithEnums.graphql.json index 2262eff..723a380 100644 --- a/generate/testdata/queries/QueryWithEnums.graphql.json +++ b/generate/testdata/queries/QueryWithEnums.graphql.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/QueryWithSlices.graphql.json b/generate/testdata/queries/QueryWithSlices.graphql.json index 14119b9..c198a16 100644 --- a/generate/testdata/queries/QueryWithSlices.graphql.json +++ b/generate/testdata/queries/QueryWithSlices.graphql.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/QueryWithStructs.graphql.json b/generate/testdata/queries/QueryWithStructs.graphql.json index c7852fe..5186bcb 100644 --- a/generate/testdata/queries/QueryWithStructs.graphql.json +++ b/generate/testdata/queries/QueryWithStructs.graphql.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/SimpleInput.graphql.json b/generate/testdata/queries/SimpleInput.graphql.json index f2015f0..bb963b5 100644 --- a/generate/testdata/queries/SimpleInput.graphql.json +++ b/generate/testdata/queries/SimpleInput.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"SimpleInputQuery","query":"\nquery SimpleInputQuery ($name: String!) {\n\tuser(query: {name:$name}) {\n\t\tid\n\t}\n}\n"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/SimpleQuery.graphql.json b/generate/testdata/queries/SimpleQuery.graphql.json index 6ba92f5..ba9de31 100644 --- a/generate/testdata/queries/SimpleQuery.graphql.json +++ b/generate/testdata/queries/SimpleQuery.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"SimpleQuery","query":"\nquery SimpleQuery {\n\tuser {\n\t\tid\n\t}\n}\n"}]} \ No newline at end of file +{"operations":[{"operationName":"SimpleQuery","query":"\nquery SimpleQuery {\n\tuser {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/SimpleQuery.graphql"}]} \ No newline at end of file diff --git a/generate/testdata/queries/TypeName.graphql.json b/generate/testdata/queries/TypeName.graphql.json index 4ce96d2..e416744 100644 --- a/generate/testdata/queries/TypeName.graphql.json +++ b/generate/testdata/queries/TypeName.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"TypeNameQuery","query":"\nquery TypeNameQuery {\n\tuser {\n\t\t__typename\n\t\tid\n\t}\n}\n"}]} \ No newline at end of file +{"operations":[{"operationName":"TypeNameQuery","query":"\nquery TypeNameQuery {\n\tuser {\n\t\t__typename\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/TypeName.graphql"}]} \ No newline at end of file diff --git a/generate/testdata/queries/UnionNoFragments.graphql.json b/generate/testdata/queries/UnionNoFragments.graphql.json index e3682a8..87a0aa9 100644 --- a/generate/testdata/queries/UnionNoFragments.graphql.json +++ b/generate/testdata/queries/UnionNoFragments.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"UnionNoFragmentsQuery","query":"\nquery UnionNoFragmentsQuery {\n\trandomLeaf {\n\t\t__typename\n\t}\n}\n"}]} \ No newline at end of file +{"operations":[{"operationName":"UnionNoFragmentsQuery","query":"\nquery UnionNoFragmentsQuery {\n\trandomLeaf {\n\t\t__typename\n\t}\n}\n","sourceLocation":"testdata/queries/UnionNoFragments.graphql"}]} \ No newline at end of file diff --git a/generate/testdata/queries/UsesEnumTwice.graphql.json b/generate/testdata/queries/UsesEnumTwice.graphql.json index 5c49d9a..0054ffe 100644 --- a/generate/testdata/queries/UsesEnumTwice.graphql.json +++ b/generate/testdata/queries/UsesEnumTwice.graphql.json @@ -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"}]} \ No newline at end of file +{"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"}]} \ No newline at end of file diff --git a/generate/testdata/queries/unexported.graphql.json b/generate/testdata/queries/unexported.graphql.json index 55a8472..b4887f4 100644 --- a/generate/testdata/queries/unexported.graphql.json +++ b/generate/testdata/queries/unexported.graphql.json @@ -1 +1 @@ -{"operations":[{"operationName":"unexported","query":"\nquery unexported ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n"}]} \ No newline at end of file +{"operations":[{"operationName":"unexported","query":"\nquery unexported ($query: UserQueryInput) {\n\tuser(query: $query) {\n\t\tid\n\t}\n}\n","sourceLocation":"testdata/queries/unexported.graphql"}]} \ No newline at end of file