Skip to content

Commit

Permalink
Merge pull request #246 from Yamashou/fix-only-use
Browse files Browse the repository at this point in the history
Fix recursively
  • Loading branch information
Yamashou authored Dec 1, 2024
2 parents a63452c + 5c166c5 commit 2538970
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
2 changes: 1 addition & 1 deletion generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func Generate(ctx context.Context, cfg *config.Config) error {
return fmt.Errorf(": %w", err)
}

usedTypes := querydocument.CollectTypesFromQueryDocuments(operationQueryDocuments)
usedTypes := querydocument.CollectTypesFromQueryDocuments(cfg.GQLConfig.Schema, operationQueryDocuments)

var clientGen api.Option
if cfg.Generate != nil {
Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/urfave/cli/v2"
)

const version = "0.27.0"
const version = "0.27.1"

var versionCmd = &cli.Command{
Name: "version",
Expand Down
20 changes: 19 additions & 1 deletion querydocument/query_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,20 +70,38 @@ func fragmentsInOperationWalker(selectionSet ast.SelectionSet) ast.FragmentDefin
}

// CollectTypesFromQueryDocuments returns a map of type names used in query document arguments
func CollectTypesFromQueryDocuments(queryDocuments []*ast.QueryDocument) map[string]bool {
func CollectTypesFromQueryDocuments(schema *ast.Schema, queryDocuments []*ast.QueryDocument) map[string]bool {
usedTypes := make(map[string]bool)

for _, doc := range queryDocuments {
for _, op := range doc.Operations {
// Collect types from variable definitions
for _, v := range op.VariableDefinitions {
collectTypeFromTypeReference(v.Type, usedTypes)
// Recursively collect input object fields
if def, ok := schema.Types[v.Type.Name()]; ok && def.IsInputType() {
collectInputObjectFields(def, schema, usedTypes)
}
}
}
}

return usedTypes
}

// collectInputObjectFields recursively collects types from input object fields
func collectInputObjectFields(def *ast.Definition, schema *ast.Schema, usedTypes map[string]bool) {
for _, field := range def.Fields {
typeName := field.Type.Name()
usedTypes[typeName] = true

// If the field is an input object type, recursively collect
if fieldDef, ok := schema.Types[typeName]; ok && fieldDef.IsInputType() {
collectInputObjectFields(fieldDef, schema, usedTypes)
}
}
}

// collectTypeFromTypeReference is a helper function to collect type names from type references
func collectTypeFromTypeReference(t *ast.Type, usedTypes map[string]bool) {
if t == nil {
Expand Down

0 comments on commit 2538970

Please sign in to comment.