github.com/mkusaka/gqlgen@v0.7.2/codegen/input_test.go (about)

     1  package codegen
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/require"
     7  	"golang.org/x/tools/go/loader"
     8  )
     9  
    10  func TestTypeUnionAsInput(t *testing.T) {
    11  	err := generate("inputunion", `
    12  		type Query {
    13  			addBookmark(b: Bookmarkable!): Boolean!
    14  		}
    15  		type Item {name: String}
    16  		union Bookmarkable = Item
    17  	`)
    18  
    19  	require.EqualError(t, err, "model plan failed: Bookmarkable! cannot be used as argument of Query.addBookmark. only input and scalar types are allowed")
    20  }
    21  
    22  func TestTypeInInput(t *testing.T) {
    23  	err := generate("typeinput", `
    24  		type Query {
    25  			addBookmark(b: BookmarkableInput!): Boolean!
    26  		}
    27  		type Item {name: String}
    28  		input BookmarkableInput {
    29  			item: Item
    30  		}
    31  	`)
    32  
    33  	require.EqualError(t, err, "model plan failed: Item cannot be used as a field of BookmarkableInput. only input and scalar types are allowed")
    34  }
    35  
    36  func generate(name string, schema string, typemap ...TypeMap) error {
    37  	cfg := Config{
    38  		SchemaFilename: SchemaFilenames{"schema.graphql"},
    39  		SchemaStr:      map[string]string{"schema.graphql": schema},
    40  		Exec:           PackageConfig{Filename: "gen/" + name + "/exec.go"},
    41  		Model:          PackageConfig{Filename: "gen/" + name + "/model.go"},
    42  	}
    43  
    44  	if len(typemap) > 0 {
    45  		cfg.Models = typemap[0]
    46  	}
    47  	err := Generate(cfg)
    48  	if err == nil {
    49  		conf := loader.Config{}
    50  		conf.Import("github.com/99designs/gqlgen/codegen/testdata/gen/" + name)
    51  
    52  		_, err = conf.Load()
    53  		if err != nil {
    54  			panic(err)
    55  		}
    56  	}
    57  	return err
    58  }