Skip to content

GitHub GraphQL API (v4) support. #646

@dmitshur

Description

@dmitshur

This is a long-term tracking/planning issue for GitHub GraphQL API (v4). /cc @willnorris @gmlewis

Today, GitHub has announced that the GitHub GraphQL API is out of the Early Access program. The documentation is thorough, and very helpful at explaining the neccessary concepts and how to get started with using the API. (Note that it requires authentication; it's no longer possible to make unauthenticated API calls.)

Announcement: https://github.com/blog/2359-introducing-github-marketplace-and-more-tools-to-customize-your-workflow#user-content-github-graphql-api
GitHub API Documentation: https://developer.github.com/v4/

GitHub GraphQL API v4 vs REST API v3

The GraphQL API v4 is significantly different from the REST API v3, which this repository currently implements. There's very little in common, so support will be offered in a separate package (and maybe even separate repo).

Implementing this will require investigation and discovery. What does a good Go client library for a GraphQL look like? We don't know yet, we'll need to try and see.

I think the discovery part is best prototyped in separate repositories. That way, no one's blocked on anyone, and different approaches can be tested out by different people. We can coordinate and report on progress here.

Once there's agreement on the best API/solution, we can decide to either:

  • Move the package into this repository, perhaps under githubql or v4/github subfolder.
  • Keep the package in a separate repository, but link to it from README.

Initial Observations

I've started looking into this, and here are some observations:

  • It's very likely the end result package can be entirely generated. The GraphQL schema is entirely introspectable and very structured, and strongly typed. See https://developer.github.com/v4/guides/intro-to-graphql/#discovering-the-graphql-api. Aside from prototyping an initial version, there's really no need to write/maintain the entire package manually.

  • It might be the case that one Go type per GraphQL fragment would be a good approach.

  • My initial idea (that I'm thinking about) on how a Go client for a GraphQL API can look like is similar to json.Unmarshal API, with the githubql package providing all the needed types to compose queries. For example:

    /*
    The following GraphQL query:
    
    query {
      viewer {
        login
        createdAt
      }
    }
    
    Can look *roughly* like this:
    */
    var query struct {
        Viewer struct {
            Login     githubql.String
            CreatedAt githubql.DateTime
            // other fields you're interested in, expressed in some Go-compatible way
        }
    }
    err := githubqlClient.Do(ctx, &query)
    if err != nil {
        // handle error
    }
    
    fmt.Println("current user:", query.Viewer.Login, query.Viewer.CreatedAt)
    
    // Output:
    // current user: gopher 2016-03-07T23:46:14Z

    However, the devil is in details. Figuring out a way to make this work in a general case for all types of GraphQL queries is the required investigation/discovery effort. Perhaps this is not viable at all, and a completely different approach is better.

  • It might turn out that it's not viable to create a helpful Go client package for GraphQL. It might turn out to be easier to just make ad-hoc requests. We will find out.

  • Nothing about this is GitHub specific. It's GraphQL specific. A good solution to this will likely apply to any other GraphQL API.

  • I've looked at the starwars example in github.com/neelance/graphql-go, a good Go GraphQL package (/cc @neelance). However, that package is for creating GraphQL servers, and the example simply uses GraphiQL for the client.

I'm not an expert on GraphQL, just looking into it now. I might be missing some existing efforts/knowledge about creating Go clients for GraphQL APIs. If so, any insight or links would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions