githubv4 icon indicating copy to clipboard operation
githubv4 copied to clipboard

ID type intersects poorly with map[string]interface{}.

Open dmitshur opened this issue 8 years ago • 3 comments

When you create a variable of type githubql.ID and place it into a map[string]interface{} as part of variables, the fact that its type was githubql.ID gets "lost" because the type is interface{} which satisfies interfaces{}.

This means queryArguments has a hard time telling the original type name:

var nodeID = githubql.ID("someid")

variables := map[string]interface{}{
    "id": nodeID,
}

Results in query($nodeID: string!) { ... } instead of query($nodeID: ID!) { ... }.

dmitshur avatar Jul 09 '17 16:07 dmitshur

This breaks it for me when I have query variables of type String! as it converts all strings to ID!

var m struct {
	CreateClinicalCodeType struct {
		Type        graphql.String
		Description graphql.String
	} `graphql:"createClinicalCodeType(type:$type, description:$description)"`
}
variables := map[string]interface{}{
	"type":        "TestType",
	"description": "Test Description",
}

Results in the mutation string, converting all Strings! to ID!:

mutation($description:ID!$type:ID!){createClinicalCodeType(type:$type, description:$description){type,description}}

mwilli31 avatar May 08 '18 23:05 mwilli31

@mwilli31 You need to explicitly specify the GraphQL type for variables, like so:

variables := map[string]interface{}{
	"type":        githubql.String("TestType"),
	"description": githubql.String("Test Description"),
}

This is now mentioned in the Arguments and Variables section of the README.

dmitshur avatar May 09 '18 01:05 dmitshur

@shurcooL Great! Validated it is working as stated. Thanks!

mwilli31 avatar May 09 '18 04:05 mwilli31