Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slice doesn't exist in any of 1 places to unmarshal #54

Open
achempak opened this issue Jun 17, 2020 · 4 comments
Open

slice doesn't exist in any of 1 places to unmarshal #54

achempak opened this issue Jun 17, 2020 · 4 comments

Comments

@achempak
Copy link

achempak commented Jun 17, 2020

I know this error has popped up before, but previous solutions don't seem to apply to me (or they do and I've overlooked it somehow). I'm using this module to itneract with the Shopify GraphQL API. I have a function like this:

func GetCustomersGql(client *graphql.Client) *[]entity.RawCustomer {
	type address struct {
		Id           string
		Address1     string
		Address2     string
		City         string
		Company      string
		Country      string
		CountryCode  string
		Province     string
		ProvinceCode string
		Zip          string
	}
	type node struct {
		Id               string
		Email            string
		FirstName        string
		LastName         string
		State            string
		Note             string
		VerifiedEmail    bool
		OrdersCount      string
		TaxExempt        bool
		TotalSpent       string
		Phone            string
		Tags             string
		AcceptsMarketing bool
		DefaultAddress   address
		CreatedAt string
		UpdatedAt string
	}
	type edge struct {
		Cursor string
		Node   node
	}
	var query struct {
		Customers struct {
			PageInfo struct {
				HasNextPage     bool
				HasPreviousPage bool
			}
			Edges []edge
		} `graphql:"customers(first: 2)"`
	}

	err := client.Query(context.Background(), &query, nil)
	if err != nil {
		fmt.Println("GraphQL Problem")
		panic(err)
	}
	fmt.Println(query.Customers)
	return nil // for now
}

However, when I run this function, I get the following error: slice doesn't exist in any of 1 places to unmarshal.
This is the GraphQL call I am trying to perform:

query {
  customers(first: 2) {
    pageInfo {
      hasNextPage
      hasPreviousPage
    }
    edges {
      cursor
      node {
        id
        email
        firstName
        lastName
        state
        note
        verifiedEmail
        ordersCount
        taxExempt
        totalSpent
        phone
        acceptsMarketing
        defaultAddress {
          id
          address1
          address2
          city
          company
          country
          countryCode
          province
          provinceCode
          zip
        }
        createdAt
        updatedAt
      }
    }
  }
}

The response looks something like this:

{
    "data": {
        "customers": {
            "pageInfo": {
                "hasNextPage": true,
                "hasPreviousPage": false
            },
            "edges": [
                {
                    "cursor": "cursorString",
                    "node": {
                        "id": "someID",
                        "email": "someEmail",
                        "firstName": "First",
                        "lastName": "Last",
                        "state": "ENABLED",
                        "note": null,
                        "verifiedEmail": true,
                        "ordersCount": "0",
                        "taxExempt": false,
                        "totalSpent": "0.00",
                        "phone": null,
                        "acceptsMarketing": true,
                        "defaultAddress": null,
                        "createdAt": "2018-10-13T17:00:00Z",
                        "updatedAt": "2020-03-01T12:00:00Z"
                    }
                },
                {
                    "cursor": "cursorString",
                    "node": {
                        "id": "foo",
                        "email": "bar",
                        "firstName": "First",
                        "lastName": "Last",
                        "state": "ENABLED",
                        "note": "",
                        "verifiedEmail": true,
                        "ordersCount": "5",
                        "taxExempt": false,
                        "totalSpent": "650.00",
                        "phone": null,
                        "acceptsMarketing": true,
                        "defaultAddress": {
                            "id": "someID",
                            "address1": "some street name",
                            "address2": "",
                            "city": "some city",
                            "company": "",
                            "country": "United States",
                            "countryCode": "US",
                            "province": "California",
                            "provinceCode": "CA",
                            "zip": "some zipcode"
                        },
                        "createdAt": "2019-10-23T01:47:25Z",
                        "updatedAt": "2019-11-10T20:38:58Z"
                    }
                }
            ]
        }
    },
    "extensions": {
        "cost": {
            "requestedQueryCost": 6,
            "actualQueryCost": 6,
            "throttleStatus": {
                "maximumAvailable": 2000.0,
                "currentlyAvailable": 1994,
                "restoreRate": 100.0
            }
        }
    }
}

I can't figure out what I'm missing. I have a slice for the edges. And no other slice appears in the response.

@achempak
Copy link
Author

I managed to fix the problem. For some reason when I don't use embedded structs, it works fine. In particular, I need to use this as the query:

var query struct {
		Customers struct {
			PageInfo struct {
				HasNextPage     bool
				HasPreviousPage bool
			}
			Edges []struct {
				Cursor string
				Node   struct {
					Id               string
					Email            string
					FirstName        string
					LastName         string
					State            string
					Note             string
					VerifiedEmail    bool
					OrdersCount      string
					TaxExempt        bool
					TotalSpent       string
					Phone            string
					Tags             string
					AcceptsMarketing bool
					DefaultAddress   struct {
						Id           string
						Address1     string
						Address2     string
						City         string
						Company      string
						Country      string
						CountryCode  string
						Province     string
						ProvinceCode string
						Zip          string
					}
					CreatedAt time.Time
					UpdatedAt time.Time
				}
			}
		} `graphql:"customers(first: 4)"`
	}

@dmitshur
Copy link
Member

Thanks for reporting. I'll take a look at why the original query didn't work. It looks like it should work and this is a bug.

@achempak
Copy link
Author

Hi, it turns out this was my problem! One of the fields in the response (Tags), is actually a slice of strings. Somehow it worked for some customers and didn't work for others. I missed this because almost none of the customers had a value for the Tags field, so it worked most of the time. I think this can be marked as resolved. Sorry!

@lurein
Copy link

lurein commented Jun 10, 2022

+1 on this, I had a similar issue, it turned out that in my query shape I had

Errors struct {
				Path        graphql.String
				Code        graphql.String
				Description graphql.String
			}

but I needed to have:

Errors []struct {
				Path        []graphql.String
				Code        graphql.String
				Description graphql.String
			}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants