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

QQ on generating proper struct #55

Closed
atmosx opened this issue Jan 3, 2020 · 3 comments
Closed

QQ on generating proper struct #55

atmosx opened this issue Jan 3, 2020 · 3 comments
Labels

Comments

@atmosx
Copy link

@atmosx atmosx commented Jan 3, 2020

Hello,

I'm trying to convert the following GraphQL expression to Golang struct:

query{
  search(first: 100, type: REPOSITORY, query: "topic:golang org:google") {
    pageInfo {
      hasNextPage
      endCursor
      }
    repos: edges {
      repo: node {
        ... on Repository {
          name
          url
          id
        }
      }
    }
  }
}

I came up with something like this:

package main

import (
	"context"

	"fmt"

	"github.com/shurcooL/githubv4"
	"golang.org/x/oauth2"
)

var q struct {
	Search struct {
		PageInfo struct {
			HasNextPage bool
			EndCursor   githubv4.String
		}
		Repos []struct {
			Repo []struct {
				Repository struct {
					Name githubv4.String
				} `graphql:"... on Repository"`
			} `graphql:"repo: node"`
		} `graphql:"repos: edges"`
	} `graphql:"search(first: 3, type: REPOSITORY, query: \"topic:golang org:google\")"`
}

func main() {
	token := "123"
	ctx := context.Background()
	ts := oauth2.StaticTokenSource(
		&oauth2.Token{AccessToken: token},
	)
	tc := oauth2.NewClient(ctx, ts)

	client := githubv4.NewClient(tc)
	err := client.Query(context.Background(), &q, nil)
	if err != nil {
		fmt.Println(err)
	}
	fmt.Println(q.Search.Repos)
}

However, I'm getting back an empty struct:

$ go run main.go
struct field for "name" doesn't exist in any of 1 places to unmarshal
[{[]}]

Ideas, thoughts?

Thank you for your time & Happy new year!

P.

@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Jan 4, 2020

Hi @atmosx,

Thanks for providing the exact GraphQL query and the Go code you tried, it made investigating this very easy.

The problem is here:

-Repo []struct {
+Repo struct {

It is because the `graphql:"repo: node"` field corresponds to a SearchResultItemEdge.node, which a single SearchResultItem, not a list.

It's unfortunate the error message is not more helpful in spotting where the problem is. I have added a comment in shurcooL/graphql#46 (comment) to investigate if it's possible to improve that.

@dmitshur dmitshur added the question label Jan 4, 2020
@dmitshur

This comment has been minimized.

Copy link
Member

@dmitshur dmitshur commented Jan 4, 2020

Slightly off topic, I want to point out you could simplify that query by using the nodes field directly (since the only field in SearchResultItemEdge being used is node):

nodes {
	... on Repository {
		name
	}
}

Its Go code equivalent:

Repos []struct {
	Repository struct {
		Name githubv4.String
	} `graphql:"... on Repository"`
} `graphql:"nodes"`
@atmosx

This comment has been minimized.

Copy link
Author

@atmosx atmosx commented Jan 4, 2020

Thanks @dmitshur for the prompt reply and simplified version of the query!

I'm closing the issue as it is solved.

@atmosx atmosx closed this Jan 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
2 participants
You can’t perform that action at this time.