shurcooL / githubv4

Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql).
MIT License
1.11k stars 89 forks source link

access to a repositories dependency graph #54

Open ghost opened 4 years ago

ghost commented 4 years ago

Hi,

Hope you are all well !

I wonder if you could provide me the example for accessing the DependencyGraphManifest for a repository.

Ref. https://developer.github.com/v4/previews/#access-to-a-repositories-dependency-graph

I cannot understand how to build the query.

Thanks in advance for your help.

Cheers, X

dmitshur commented 4 years ago

Hello.

DependencyGraphManifest is a part of API preview, meaning it's not included by default. To use it, the Accept header must have application/vnd.github.hawkgirl-preview+json set. This is described at https://developer.github.com/v4/previews/#access-to-a-repositories-dependency-graph.

There's an open issue about finding a way for this library to support previews, see #34. It is not complete. Until more progress is made, the best way I can recommend (and what others have done, e.g., see #45) is to make a fork and modify it to add preview API support that you're looking for.

Alternatively, you can wait until GitHub graduates said preview API into their normal API.

Hope that helps.

ghost commented 4 years ago

@djoksimo any plans to fix it ? or it would be awesome if you solve this incomplete pull request.

djoksimo commented 4 years ago

@x0rzkov I will most likely not be able to complete the pull request soon but there are a few alternatives that may help.

You could use http.RoundTripper to attach HTTP headers directly in your code before calling any queries. Check out this stackoverflow link.

Optional:

My fork of this package (https://github.com/djoksimo/githubv4) autogenerates the preview mode Go GraphQL schema by fetching the preview headers directly from GitHub. See how the headers are attached and check out the diff here

Feel free to use my fork as inspiration, or to fork my fork. My fork may be a bit outdated so I cannot guarantee total functionality. Let me know if you have any questions! 😄

nehzata commented 11 months ago

If anyone else needs to do this:

type Transport struct {
    T http.RoundTripper
}

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {
    req.Header.Add("Accept", "application/vnd.github.hawkgirl-preview+json")
    return t.T.RoundTrip(req)
}

func NewTransport(T http.RoundTripper) *Transport {
    if T == nil {
        T = http.DefaultTransport
    }
    return &Transport{T}
}

var q struct {}
var vars map[string]any = map[string]any{}

httpClient := &http.Client{Transport: httpgh.NewTransport(nil)}

ctx := context.WithValue(
    context.Background(), 
    oauth2.HTTPClient, 
    &http.Client{Transport: httpgh.NewTransport(nil)},
)
client := githubv4.NewClient(
    oauth2.NewClient(
        ctx,
        oauth2.StaticTokenSource(&oauth2.Token{
            AccessToken: ghAccesstoken,
        }),
    ),
)

err = client.Query(ctx, &q, vars)
if err != nil {
    logger.WithError(err).Error("failed to query repos")
    return false, err
}