shurcooL/graphql

Struct Field Tag not Properly Working

achempak opened this issue · 2 comments

I'm attempting to perform the following query:

	var query struct {
		Orders struct {
			PageInfo struct {
				HasNextPage     bool
				HasPreviousPage bool
			}
			Edges []struct {
				Cursor string
				Node   struct {
					Refunds []struct {
						Id              string
						Note            string
						CreatedAt       string
						UpdatedAt       string
						RefundLineItems struct {
							Edges []struct {
								Node struct {
									LineItem struct {
										Id   string
										Name string
										Sku  string
									}
									Quantity    int
									SubtotalSet struct {
										ShopMoney struct {
											Amount string
										}
									}
									PriceSet struct {
										ShopMoney struct {
											Amount string
										}
									}
								}
							}
						} `graphql:"refundLineItems(first: 20)"`
					} `graphql:"refunds(first: 5)"`
				}
			}
		} `graphql:"orders(query: $query, first: 1)"`
	}

The value of $query (at the bottom of the struct) is in the map

date := time.Date(2020, time.January, 1, 0, 0, 0, 0, time.Local).Format("2006-01-02 15:04:05")
variables := map[string]interface{}{
		"query": graphql.String(fmt.Sprintf(`\"updated_at:>'%s'\"`, date)),
	}

When I perform the query err := client.Query(*ctx, &query, variables), it returns no results, which shouldn't be the case. When I directly type in the date for query

`graphql:"orders(query: \"updated_at:>'2020-01-01 00:00:00'\", first: 1)"`

it properly returns the expected result. I compared the string produced with Sprintf with the literal string, and the two are identical. I'm not sure why this could be happening. Ideally I want to be able to use a variable date in the field tag.

variables := map[string]interface{}{
	"query": graphql.String(fmt.Sprintf(`\"updated_at:>'%s'\"`, date)),
}

That seems over-quoted. Can you try something like this:

variables := map[string]interface{}{
	"query": graphql.String(fmt.Sprintf("updated_at:>'%s'", date)),
}

That works, thank you! The Shpoify docs all use the additional pair of quotes, so I thought I had to include them here as well. I didn't realize they were implicitly added.