superawesomelabs / leo

Highly Extensible, Declarative Static Site Generator
103 stars 6 forks source link

leo-plugin-github #146

Open ChristopherBiscardi opened 8 years ago

ChristopherBiscardi commented 8 years ago

There should be a GitHub plugin to make requests to their GraphQL API. Such a plugin could be implemented as the github key in the GraphQL Schema, which would encompass GitHub's entire GraphQL.

{
  root: {
    github: {
      viewer {
        login
        bio
        organizations(first: 3) {
          edges {
            org:node {
              name
            }
          }
        }
      }
    }
  }
}

The main challenge here is recreating the GitHub GraphQL Schema so that queries can be passed through. Can we do an introspection passthrough? (ie: "mount" the passthrough API at github, which when we generate our schema, just goes and fetches the introspection data from GitHub).

Mounting the Schema is the preferred approach so that we do not have to manually recreate and maintain the entire schema in the plugin.

The plugin should handle passing the raw query through to github and dealing with the response appropriately.

KyleAMathews commented 8 years ago

Related: https://github.com/graphql/graphql-js/issues/490

ChristopherBiscardi commented 8 years ago

consume the introspection query and auto-construct the schema locally

I think I've successfully prototyped a merged Introspection query that monkey-patches the GitHub GraphQL API. I sure as hell wouldn't use it in a production webapp (hackkkkkkk), but for LEO/Gatsby it might be useful? I do believe it can also be written out directly and used in a Relay client to validate queries.

Step 2, which is what I'm working on right now, is to hijack express-graphql's introspection query and return the data so that Graphiql can use it. I have no idea if this is going to work and if it does it's an even hackier hack than merging the introspection queries.

Step 3 is to parse the request body ahead of express-graphql's handling and finagle the AST to rip out the github query, then remount it on it's own query and send it to github to return the appropriate data.

ChristopherBiscardi commented 8 years ago
> node index.js
# introspection types for the base schema
[ 'Query',
  'String',
  '__Schema',
  '__Type',
  '__TypeKind',
  'Boolean',
  '__Field',
  '__InputValue',
  '__EnumValue',
  '__Directive',
  '__DirectiveLocation' ]
# length of types for base and github schema respectively
11 184
# skipping types that are basic and exist in both places
skipping String
skipping Boolean
185

The final number is the length of the merged introspection query's types, which is correct because a new type is created from GitHub's Query type (renamed to GitHubQuery and remounted) while the original Query type is preserved and added-to.