├── .editorconfig ├── .gitattributes ├── .gitignore ├── .npmrc ├── .travis.yml ├── index.js ├── license ├── package.json ├── readme.md └── test.js /.editorconfig: -------------------------------------------------------------------------------- 1 | root = true 2 | 3 | [*] 4 | indent_style = tab 5 | end_of_line = lf 6 | charset = utf-8 7 | trim_trailing_whitespace = true 8 | insert_final_newline = true 9 | 10 | [*.yml] 11 | indent_style = space 12 | indent_size = 2 13 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | * text=auto 2 | *.js text eol=lf 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | yarn.lock 3 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - '8' 4 | - '6' 5 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | const AggregateError = require('aggregate-error'); 3 | const got = require('got'); 4 | const prependHttp = require('prepend-http'); 5 | 6 | class GraphQLError extends Error { 7 | constructor(err, body) { 8 | super(err.message); 9 | this.name = 'GraphQLError'; 10 | this.locations = err.locations; 11 | this.operationName = body.operationName; 12 | this.query = body.query; 13 | this.variables = body.variables; 14 | } 15 | } 16 | 17 | const graphqlGot = (url, opts) => { 18 | opts = Object.assign({method: 'POST'}, opts, {json: true}); 19 | 20 | opts.body = Object.assign({}, { 21 | query: opts.query, 22 | operationName: opts.operationName, 23 | variables: opts.variables 24 | }); 25 | 26 | delete opts.query; 27 | delete opts.operationName; 28 | delete opts.variables; 29 | 30 | if (opts.token) { 31 | opts.headers = Object.assign({}, opts.headers, { 32 | authorization: `bearer ${opts.token}` 33 | }); 34 | } 35 | 36 | return got(prependHttp(url, {https: true}), opts) 37 | .then(res => { 38 | res.errors = res.body.errors; 39 | res.body = res.body.data; 40 | return res; 41 | }) 42 | .catch(err => { 43 | if (err.response && typeof err.response.body === 'object') { 44 | const errors = err.response.body.errors; 45 | const error = Array.isArray(errors) && errors.length > 0 ? 46 | errors.map(x => new GraphQLError(x, opts.body)) : 47 | [new GraphQLError(err, opts.body)]; 48 | 49 | throw new AggregateError(error); 50 | } 51 | 52 | throw err; 53 | }); 54 | }; 55 | 56 | const helpers = [ 57 | 'get', 58 | 'post' 59 | ]; 60 | 61 | for (const x of helpers) { 62 | const method = x.toUpperCase(); 63 | graphqlGot[x] = (url, opts) => graphqlGot(url, Object.assign({}, opts, {method})); 64 | } 65 | 66 | module.exports = graphqlGot; 67 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Kevin Mårtensson (github.com/kevva) 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 10 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "graphql-got", 3 | "version": "0.1.2", 4 | "description": "Convenience wrapper for got to interact with GraphQL", 5 | "license": "MIT", 6 | "repository": "kevva/graphql-got", 7 | "author": { 8 | "name": "Kevin Mårtensson", 9 | "email": "kevinmartensson@gmail.com", 10 | "url": "github.com/kevva" 11 | }, 12 | "engines": { 13 | "node": ">=4" 14 | }, 15 | "scripts": { 16 | "test": "xo && ava" 17 | }, 18 | "files": [ 19 | "index.js" 20 | ], 21 | "keywords": [ 22 | "api", 23 | "apollo", 24 | "get", 25 | "got", 26 | "graphql", 27 | "gql", 28 | "http", 29 | "https", 30 | "post", 31 | "query", 32 | "relay", 33 | "request", 34 | "uri", 35 | "url", 36 | "util", 37 | "utility" 38 | ], 39 | "dependencies": { 40 | "aggregate-error": "^1.0.0", 41 | "got": "^7.1.0", 42 | "prepend-http": "^2.0.0" 43 | }, 44 | "devDependencies": { 45 | "apollo-server-micro": "^1.1.2", 46 | "ava": "*", 47 | "graphql": "^0.11.3", 48 | "graphql-tools": "^1.2.2", 49 | "micro": "^8.0.4", 50 | "nock": "^9.0.18", 51 | "test-listen": "^1.0.2", 52 | "xo": "*" 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # graphql-got [![Build Status](https://travis-ci.org/kevva/graphql-got.svg?branch=master)](https://travis-ci.org/kevva/graphql-got) 2 | 3 | > Convenience wrapper for `got` to interact with [GraphQL](http://graphql.org/) 4 | 5 | A lightweight alternative to [`apollo-client`](https://github.com/apollographql/apollo-client) and [`relay`](https://github.com/facebook/relay). 6 | 7 | 8 | ## Install 9 | 10 | ``` 11 | $ npm install graphql-got 12 | ``` 13 | 14 | 15 | ## Usage 16 | 17 | ```js 18 | const graphqlGot = require('graphql-got'); 19 | 20 | const query = `{ 21 | unicorn(name: "Foo Bar") { 22 | id 23 | name 24 | } 25 | }`; 26 | 27 | graphqlGot('api.graphql.unicorn', {query}).then(response => { 28 | console.log(response.body); 29 | /* 30 | { 31 | unicorn: { 32 | id: 0, 33 | name: 'Foo Bar' 34 | } 35 | } 36 | */ 37 | }); 38 | ``` 39 | 40 | 41 | ## API 42 | 43 | Same as [`got`](https://github.com/sindresorhus/got), but with some additional options below. URLs without protocol will be prepended with `https://`. 44 | 45 | ### query 46 | 47 | *Required*
48 | Type: `string` 49 | 50 | The `query` to send to [GraphQL](http://graphql.org/). 51 | 52 | ### variables 53 | 54 | Type: `Object` 55 | 56 | Variables to be used in your `query`. Read more [here](http://graphql.org/learn/queries/#variables). 57 | 58 | ### operationName 59 | 60 | Type: `string` 61 | 62 | If your `query` contains multiple operations, this option is required to decide which operation to run. 63 | 64 | ### token 65 | 66 | Type: `string` 67 | 68 | If defined, an `Authorization` header with `bearer ${TOKEN}` will be sent. 69 | 70 | 71 | ## License 72 | 73 | MIT © [Kevin Mårtensson](https://github.com/kevva) 74 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | import test from 'ava'; 2 | import {makeExecutableSchema} from 'graphql-tools'; 3 | import {microGraphql} from 'apollo-server-micro'; 4 | import micro from 'micro'; 5 | import nock from 'nock'; 6 | import testListen from 'test-listen'; 7 | import m from '.'; 8 | 9 | let url; 10 | 11 | test.before(async () => { 12 | const typeDefs = ` 13 | type Unicorn { 14 | id: Int 15 | name: String 16 | } 17 | 18 | type Query { 19 | unicorn(id: Int!): Unicorn 20 | error: Int 21 | } 22 | `; 23 | 24 | const resolvers = { 25 | Query: { 26 | unicorn: (_, {id}) => ({ 27 | id, 28 | name: 'Hello world' 29 | }), 30 | error: () => { 31 | throw new Error('boom'); 32 | } 33 | } 34 | }; 35 | 36 | url = await testListen(micro(microGraphql({ 37 | schema: makeExecutableSchema({typeDefs, resolvers}) 38 | }))); 39 | }); 40 | 41 | test('query', async t => { 42 | const query = ` 43 | { 44 | unicorn(id: 0) { 45 | id, 46 | name 47 | } 48 | } 49 | `; 50 | 51 | t.deepEqual((await m(url, {query})).body, { 52 | unicorn: { 53 | id: 0, 54 | name: 'Hello world' 55 | } 56 | }); 57 | }); 58 | 59 | test('variables', async t => { 60 | const variables = {id: 0}; 61 | const query = ` 62 | query ($id: Int!) { 63 | unicorn(id: $id) { 64 | id, 65 | name 66 | } 67 | } 68 | `; 69 | 70 | t.deepEqual((await m(url, {query, variables})).body, { 71 | unicorn: { 72 | id: 0, 73 | name: 'Hello world' 74 | } 75 | }); 76 | }); 77 | 78 | test('operationName', async t => { 79 | const operationName = 'foo'; 80 | const query = ` 81 | query foo { 82 | unicorn(id: 0) { 83 | id 84 | } 85 | } 86 | 87 | query bar { 88 | unicorn(id: 0) { 89 | name 90 | } 91 | } 92 | `; 93 | 94 | t.deepEqual((await m(url, {query, operationName})).body, { 95 | unicorn: { 96 | id: 0 97 | } 98 | }); 99 | }); 100 | 101 | test('resolver error', async t => { 102 | const query = ` 103 | { 104 | error 105 | } 106 | `; 107 | 108 | const {errors} = await m(url, {query}); 109 | t.deepEqual(errors, [{message: 'boom', locations: [{line: 3, column: 4}], path: ['error']}]); 110 | }); 111 | 112 | test('GraphQLError', async t => { 113 | const operationName = 'foo'; 114 | const variables = {id: 0}; 115 | const query = ` 116 | query foo ($id: Int!) { 117 | unicorn(id: 0) { 118 | foo 119 | } 120 | } 121 | `; 122 | 123 | const errors = await t.throws(m(url, {operationName, query, variables})); 124 | 125 | for (const x of errors) { 126 | t.is(x.name, 'GraphQLError'); 127 | t.is(x.operationName, operationName); 128 | t.is(x.query, query); 129 | t.true(Array.isArray(x.locations)); 130 | t.deepEqual(x.variables, variables); 131 | } 132 | }); 133 | 134 | test('token option', async t => { 135 | const token = 'unicorn'; 136 | 137 | nock('http://foo.bar/') 138 | .matchHeader('authorization', `bearer ${token}`) 139 | .post('/') 140 | .reply(200, {data: {token}}); 141 | 142 | t.is((await m('http://foo.bar/', {token})).body.token, token); 143 | }); 144 | 145 | test('prepends `https://` to url', async t => { 146 | nock('https://foo.bar/') 147 | .post('/') 148 | .reply(200, {data: 'ok'}); 149 | 150 | t.is((await m('foo.bar/')).body, 'ok'); 151 | }); 152 | --------------------------------------------------------------------------------