Applifier / graphql-codegen

Generates go code from graphql schema (to be used with github.com/neelance/graphql-go)

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Gen non null custom field types

ghostiam opened this issue · comments

type BarFoo {
  startCursor: ID
  endCursor: ID
  hasNextPage: Boolean!
}

type FooBar {
  barFoo: BarFoo
  barFoo2: BarFoo!
  test: Test
  test2: Test!
}

scalar Test

We get:

type FooBar struct {
	// BarFoo
	BarFoo *BarFooResolver `json:"barFoo"`
	// BarFoo2
	BarFoo2 *BarFooResolver `json:"barFoo2"`
	// Test
	Test *TestResolver `json:"test"`
	// Test2
	Test2 *TestResolver `json:"test2"`
}

Expected:

type FooBar struct {
	// BarFoo
	BarFoo *BarFooResolver `json:"barFoo"`
	// BarFoo2
	BarFoo2 *BarFooResolver `json:"barFoo2"`
	// Test
	Test *TestResolver `json:"test"`
	// Test2
	Test2 TestResolver `json:"test2"`
}

Maybe error in https://github.com/Applifier/graphql-codegen/blob/master/codegen/codegen.go#L426-L437

...
	} else if tp.Kind() != "INPUT_OBJECT" {
		if len(typ) > 0 {
			if typ[len(typ)-1] != '*' {
				typ = typ + "*"
			}
		} else {
			typ = "*"
		}
		typ = typ + *name + "Resolver"
	} else {
		typ = typ + *name
	}
...

fix

	if tp.Kind() == "ENUM" {
		typ = typ + *name
	} else if tp.Kind() != "INPUT_OBJECT" {
		if len(typ) > 0 {
			if typ[len(typ)-1] != '*' {
				typ = typ + "*"
			}
-		} else {
+		} else if tp.Kind() != "SCALAR" {
			typ = "*"
		}
		typ = typ + *name + "Resolver"
	} else {
		typ = typ + *name
	}