Skip to content

Commit

Permalink
Merge pull request #250 from Yamashou/fix-sycle-input
Browse files Browse the repository at this point in the history
The input correction for the circular reference was insufficient, so it was corrected.
  • Loading branch information
Yamashou authored Dec 3, 2024
2 parents d5223c4 + 098b26c commit b27c708
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 18 deletions.
14 changes: 14 additions & 0 deletions example/skipmodel/.gqlgenc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
schema:
- ./schema/schema.graphql
model:
filename: ./model/models_gen.go
client:
filename: ./gen/client.go
models:
DateTime:
model: github.com/99designs/gqlgen/graphql.Time
query:
- "./query/*.graphql"
generate:
clientV2: true
onlyUsedModels: true
68 changes: 68 additions & 0 deletions example/skipmodel/gen/client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions example/skipmodel/model/models_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions example/skipmodel/query/query.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
mutation A($input: UserInput!) {
user(input: $input) {
id
}
}
32 changes: 32 additions & 0 deletions example/skipmodel/schema/schema.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
schema {
query: Query
mutation: Mutation
}

type Query {
user: User!
}

type Mutation {
user(input: UserInput!): User!
}

input UserInput {
id: ID!
profile: ProfileInput!
friends: [UserInput!]!
}

input ProfileInput {
name: String!
}

type User {
id: ID!
profile: Profile!
}

type Profile {
id: ID!
name: String!
}
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.2"
const version = "0.27.3"

var versionCmd = &cli.Command{
Name: "version",
Expand Down
36 changes: 19 additions & 17 deletions querydocument/query_document.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,18 @@ func fragmentsInOperationWalker(selectionSet ast.SelectionSet) ast.FragmentDefin
// CollectTypesFromQueryDocuments returns a map of type names used in query document arguments
func CollectTypesFromQueryDocuments(schema *ast.Schema, queryDocuments []*ast.QueryDocument) map[string]bool {
usedTypes := make(map[string]bool)
processedTypes := 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)
if typeName := v.Type.Name(); typeName != "" {
if def, ok := schema.Types[typeName]; ok && def.IsInputType() {
collectInputObjectFieldsWithCycle(def, schema, usedTypes, processedTypes)
}
}
}
}
Expand All @@ -89,32 +92,31 @@ func CollectTypesFromQueryDocuments(schema *ast.Schema, queryDocuments []*ast.Qu
return usedTypes
}

// collectInputObjectFields recursively collects types from input object fields
func collectInputObjectFields(def *ast.Definition, schema *ast.Schema, usedTypes map[string]bool) {
// Skip if type has already been processed
if _, ok := usedTypes[def.Name]; ok {
return
func collectInputObjectFieldsWithCycle(def *ast.Definition, schema *ast.Schema, usedTypes map[string]bool, processedTypes map[string]bool) {
if processedTypes[def.Name] {
return // この型は既に完全に処理済み
}
usedTypes[def.Name] = true

usedTypes[def.Name] = true // この型を使用済みとしてマーク

for _, field := range def.Fields {
// Get the actual type name
typeName := field.Type.NamedType
if typeName == "" {
// For list types, get the element type name
if field.Type.Elem != nil {
typeName = field.Type.Elem.NamedType
}
var typeName string
// リスト型の要素型まで辿る
for field.Type != nil && field.Type.NamedType != "" {
typeName = field.Type.NamedType
break
}

if typeName != "" {
usedTypes[typeName] = true
// If field is an input object type, collect recursively
// 入力型のフィールドを再帰的に収集
if fieldDef, ok := schema.Types[typeName]; ok && fieldDef.IsInputType() {
collectInputObjectFields(fieldDef, schema, usedTypes)
collectInputObjectFieldsWithCycle(fieldDef, schema, usedTypes, processedTypes)
}
}
}

processedTypes[def.Name] = true // この型の処理が完了したことをマーク
}

// collectTypeFromTypeReference is a helper function to collect type names from type references
Expand Down

0 comments on commit b27c708

Please sign in to comment.