项目作者: ldd

项目描述 :
Pull data into Gatsby from Github API v4
高级语言: JavaScript
项目地址: git://github.com/ldd/gatsby-source-github-api.git
创建时间: 2017-09-10T06:16:58Z
项目社区:https://github.com/ldd/gatsby-source-github-api

开源协议:MIT License

下载


Build Status
Coverage Status

gatsby-source-github-api

Source plugin for pulling data into Gatsby from the official GitHub v4 GraphQL API.

Install

npm i gatsby-source-github-api

How to use

Follow GitHub’s guide how to generate a token.

Once you are done, either create a gatsby-config.js file or open the one you already have.

In there, you want to add this plugin and at least add the token in the options object:

  1. // In your gatsby-config.js
  2. plugins: [
  3. {
  4. resolve: `gatsby-source-github-api`,
  5. options: {
  6. // url: API URL to use. Defaults to https://api.github.com/graphql
  7. url: someUrl,
  8. // token: required by the GitHub API
  9. token: someString,
  10. // GraphQLquery: defaults to a search query
  11. graphQLQuery: anotherString,
  12. // variables: defaults to variables needed for a search query
  13. variables: someObject
  14. }
  15. }
  16. ];

Examples

Search query:

  1. // In your gatsby-config.js
  2. plugins: [
  3. {
  4. resolve: `gatsby-source-github-api`,
  5. options: {
  6. token: "hunter2",
  7. variables: {
  8. q: "author:ldd state:closed type:pr sort:comments",
  9. nFirst: 2
  10. }
  11. }
  12. }
  13. ];

resulting API call:

  1. query ($nFirst: Int, $q: String) {
  2. search(query: "${q}", type: ISSUE, first: ${nFirst}){
  3. edges{
  4. node{
  5. ... on PullRequest{
  6. title
  7. }
  8. }
  9. }
  10. }
  11. }

Custom GraphQL query:

  1. // In your gatsby-config.js
  2. plugins: [
  3. {
  4. resolve: `gatsby-source-github-api`,
  5. options: {
  6. token: "hunter2",
  7. variables: {},
  8. graphQLQuery: `
  9. query {
  10. repository(owner:"torvalds",name:"linux"){
  11. description
  12. }
  13. }
  14. `
  15. }
  16. }
  17. ];

resulting API call:

  1. query {
  2. repository(owner: "torvalds", name: "linux") {
  3. description
  4. }
  5. }

For more examples see gatsby-starter-github-portfolio.

Tips and Tricks

You’ll probably want to use valid GraphQL queries. To help you, GitHub has a Query Explorer with auto-completion.

Query Explorer

Changelog

  • v1.0.0 add support for gatsby v3. Remove support for older versions of gatsby
  • v0.2.1 update dependencies
  • v0.2.0 provide raw github response
  • v0.1.5 document url option (for GitHub Enterprise users)
  • v0.1.4
    • Add tests
    • expose DEFAULT_QUERY by exporting it
  • v0.1.3
    • Change mediaType of exported node to be ignored by gatsby-transformer-json
    • prettify changelog of this file
  • v0.1.2 Updated yarn.lock to address github security warnings
  • v0.1.1 Updated Readme for easier usage
  • v0.1.0 Submit to Gatsby’s Plugin Library
  • v0.0.4 Update dev dependencies, add linting script to package.json
  • v0.0.3 Initial public release