Giter VIP home page Giter VIP logo

graphql-client's Introduction

Simple GraphQL Client

Small http client based on the fetch api. Uses isomorphic-fetch for wider support

If query fails, errors are thrown with messages and query highlight for easy debug

Install

npm install graphql-client -S

How To

Initialize the client

var client = require('graphql-client')({
  url: 'http://your-host/graphql',
  headers: {
    Authorization: 'Bearer ' + token
  }
})

Use the promise API

WARNING: Make sure the Promise API is polyfilled for older browsers, you can use es6-promise

var variables = {
  query: "Search Query",
  limit: 100,
  from: 200
}

client.query(`
query search ($query: String, $from: Int, $limit: Int) {
  search(query: $query, from: $from, limit: $limit) {
    took,
    totalHits,
    hits {
      name
    }
  }
}`, variables, function(req, res) {
  if(res.status === 401) {
    throw new Error('Not authorized')
  }
})
.then(function(body) {
  console.log(body)
})
.catch(function(err) {
  console.log(err.message)
})

graphql-client's People

Contributors

incognito avatar nordsimon avatar odykyi avatar snordrapidimages avatar willthesun avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

graphql-client's Issues

typeError - cannot read property 'line' of undefined

I am using graphql-client to fetch data from GitHub.

Usually, errors returned from GitHub looks like this:

[{"message":"Could not resolve to an Organization with the login of 'GCI-AOSSIE'.","type":"NOT_FOUND","path":["organization"],"locations":[{"line":2,"column":3}]}]

However, sometimes GitHub returns errors as follows:

[{"message":"Something went wrong while executing your query. This may be the result of a timeout, or it could be a GitHub bug. Please include `E689:1AF6:67E816:BAAF4C:5BD321E1` when reporting this issue."}]

Note that it doesn't have locations field.
Under the latter circumstance,

var locations = errors.map(function (e) { return e.locations })

together with
var lineErrors = locations.filter(function (loc) { return loc && loc.line === line })

would cause an error: cannot read property 'line' of undefined

c.f. coala/gci-leaders#161 (comment)

errors.map is not a function

var locations = errors.map(function (e) { return e.locations })

I understand by convention errors should be an array. But let's take this example when shopify for invalid access-token returns response as

{
    "errors": "[API] Invalid API key or access token (unrecognized login or wrong password)"
}

can we to a type check and handle this problem?

How can I do multiple mutations with one request using an array?

For an example. I have an array of things that I want to save with one request.
Without graphql-client it would look something like this.

const query = `
  mutation {
    ${things.map((x, i) => `
        m${i}: createThing(data: {name: "${x.name}"}) { id }
      `).join(' ')}
   }`

How would you do it with graphql-client making use of variables?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.