Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.nyc_output
coverage
dist/
node_modules/
54 changes: 54 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
language: node_js
cache:
directories:
- ~/.npm
- node_modules/cypress/dist

# Trigger a push build on master and greenkeeper branches + PRs build on every branches
# Avoid double build on PRs (See https://github.com/travis-ci/travis-ci/issues/1147)
branches:
only:
- master
- /^greenkeeper.*$/

stages:
- test
- name: release
if: branch = master AND type IN (push)

jobs:
include:
- stage: test
node_js: 6
script: npm run test
- node_js: 8
script: npm run test
- node_js: 10
env: Node 10, coverage upload
script:
- npm run test
- npm run coverage:upload
- node_js: lts/*
sudo: required
addons:
chrome: stable
env: browser tests
script:
- npm run test:browser
- node_js: lts/*
sudo: required
addons:
chrome: stable
env: bundle size
script:
- npm run build
- npx bundlesize

# release stage: run semantic release & update the docs
- stage: release
node_js: lts/*
env: semantic-release
script:
- npm run build
- npm run bundle-report
- npx semantic-release
43 changes: 41 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
# 🚧 THIS IS [WORK IN PROGRESS](https://github.com/octokit/graphql.js)

# graphql.js

> GitHub GraphQL API client for browsers and Node

[![@latest](https://img.shields.io/npm/v/@octokit/graphql.svg)](https://www.npmjs.com/package/@octokit/graphql)
[![Build Status](https://travis-ci.org/octokit/graphql.js.svg?branch=master)](https://travis-ci.org/octokit/graphql.js)
[![Coverage Status](https://coveralls.io/repos/github/octokit/graphql.js/badge.svg)](https://coveralls.io/github/octokit/graphql.js)
[![Greenkeeper](https://badges.greenkeeper.io/octokit/graphql.js.svg)](https://greenkeeper.io/)

## Usage

Send a simple query
Expand Down Expand Up @@ -118,6 +121,42 @@ const { data } = await graphql(`{
}`)
```

## Errors

In case of a GraphQL error, `error.message` is set to the first error from the response’s `errors` array. All errors can be accessed at `error.errors`. `error.request` has the request options such as query, variables and headers set for easier debugging.

```js
const graphql = require('@octokit/graphql').defaults({
headers: {
authorization: `token secret123`
}
})
const query = `{
viewer {
bioHtml
}
}`

try {
const { data } = await graphql(query)
} catch (error) {
// server responds with
// {
// "data": null,
// "errors": [{
// "message": "Field 'bioHtml' doesn't exist on type 'User'",
// "locations": [{
// "line": 3,
// "column": 5
// }]
// }]
// }

console.log('Request failed:', error.request) // { query, variables: {}, headers: { authorization: 'token secret123' } }
console.log(error.message) // Field 'bioHtml' doesn't exist on type 'User'
}
```

## License

[MIT](LICENSE)
4 changes: 4 additions & 0 deletions cypress.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"integrationFolder": "test/",
"video": false
}
15 changes: 15 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const request = require('@octokit/request')
const getUserAgent = require('universal-user-agent')

const version = require('./package.json').version
const userAgent = `octokit-graphql.js/${version} ${getUserAgent()}`

const withDefaults = require('./lib/with-defaults')

module.exports = withDefaults(request, {
method: 'POST',
url: '/graphql',
headers: {
'user-agent': userAgent
}
})
15 changes: 15 additions & 0 deletions lib/error.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module.exports = class GraphqlError extends Error {
constructor (request, response) {
const message = response.data.errors[0].message
super(message)

Object.assign(this, response.data)
this.request = request

// Maintains proper stack trace (only available on V8)
/* istanbul ignore next */
if (Error.captureStackTrace) {
Error.captureStackTrace(this, this.constructor)
}
}
}
35 changes: 35 additions & 0 deletions lib/graphql.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module.exports = graphql

const GraphqlError = require('./error')

const NON_VARIABLE_OPTIONS = ['method', 'baseUrl', 'url', 'headers', 'request', 'query']

function graphql (request, query, options) {
const requestOptions = {
variables: {}
}

if (typeof query === 'string') {
options = Object.assign({ query }, options)
} else {
options = query
}

Object.keys(options).forEach(key => {
if (NON_VARIABLE_OPTIONS.includes(key)) {
requestOptions[key] = options[key]
return
}

requestOptions.variables[key] = options[key]
})

return request(requestOptions)
.then(response => {
if (response.data.data) {
return response.data
}

throw new GraphqlError(requestOptions, response)
})
}
13 changes: 13 additions & 0 deletions lib/with-defaults.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module.exports = withDefaults

const graphql = require('./graphql')

function withDefaults (request, newDefaults) {
const newRequest = request.defaults(newDefaults)
const newApi = function (query, options) {
return graphql(newRequest, query, options)
}

newApi.defaults = withDefaults.bind(null, newRequest)
return newApi
}
Loading