All Projects → fabienjuif → graph-client

fabienjuif / graph-client

Licence: MIT license
light zero dependency graphql-client, supporting cache and SSR

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to graph-client

Use Http
🐶 React hook for making isomorphic http requests
Stars: ✭ 2,066 (+7551.85%)
Mutual labels:  query, mutation
touchMyRipple
A simple library for apply the ripple effect where you want
Stars: ✭ 19 (-29.63%)
Mutual labels:  light, simple
python-qlient
A fast and modern graphql client designed with simplicity in mind.
Stars: ✭ 29 (+7.41%)
Mutual labels:  query, mutation
fluffy.js
🍱 Makes sure your content – no matter how big it is – will fit in any screen!
Stars: ✭ 47 (+74.07%)
Mutual labels:  light, simple
Yui
Minimal vim color scheme
Stars: ✭ 93 (+244.44%)
Mutual labels:  light, simple
Alfred Simple
Simple theme for Alfred
Stars: ✭ 217 (+703.7%)
Mutual labels:  light, simple
MatrixLib
Lightweight header-only matrix library (C++) for numerical optimization and machine learning. Contact me if there is an exciting opportunity.
Stars: ✭ 35 (+29.63%)
Mutual labels:  light, simple
Server Manager
This repository holds the IntISP Interface. It can be rebuilt to interface with any other hosting panel.
Stars: ✭ 31 (+14.81%)
Mutual labels:  light, simple
Parvula
An extremely simple & flexible CMS generated from flat files with a complete RESTful API —
Stars: ✭ 76 (+181.48%)
Mutual labels:  light, simple
Mu
The μ css framework — a 1 ko css file.
Stars: ✭ 202 (+648.15%)
Mutual labels:  light, simple
strapi-graphql-documentation
Collections of queries and mutations that hopefully help you in a Strapi project powered by GraphQL API 🚀
Stars: ✭ 45 (+66.67%)
Mutual labels:  query, mutation
GeneticVariation.jl
Datastructures and algorithms for working with genetic variation
Stars: ✭ 33 (+22.22%)
Mutual labels:  mutation
QArchive
Async C++ Cross-Platform library that modernizes libarchive using Qt5 🚀. Simply extracts 7z 🍔, Tarballs 🎱 and other supported formats by libarchive. ❤️
Stars: ✭ 66 (+144.44%)
Mutual labels:  simple
Simple-Slack-Bot
Simple Python library for creating Slack bots.
Stars: ✭ 26 (-3.7%)
Mutual labels:  simple
llb
Dead simple event-driven load-balancer
Stars: ✭ 27 (+0%)
Mutual labels:  simple
mohusman360.github.io
Simple Resume Template with Tailwind CSS
Stars: ✭ 38 (+40.74%)
Mutual labels:  simple
cancer-data
TCGA data acquisition and processing for Project Cognoma
Stars: ✭ 17 (-37.04%)
Mutual labels:  mutation
flexui
A light UI for C++ with XML and CSS support
Stars: ✭ 21 (-22.22%)
Mutual labels:  light
dice-simulator
A Python simple Dice Simulator just for fun
Stars: ✭ 17 (-37.04%)
Mutual labels:  simple
Hacktoberfest-Banned-The-Repo-Guys-Sorry-For-Your-Time-and-effort
A beginner-friendly open source repository to create your first pull request.
Stars: ✭ 27 (+0%)
Mutual labels:  simple

@fabienjuif/graph-client

light zero dependency graphql-client, supporting cache and SSR

CircleCI npm bundle size (scoped) npm (scoped) GitHub Coveralls github

Features

  • Light (bundlesize)
  • Supports browser and Node (SSR compatible)
  • Supports cache (via third party library, or your own code)
    • cache is not used for mutations
    • cache can be disabled per request with noCache: true option

Install

npm install --save @fabienjuif/graph-client

API

import createClient from '@fabienjuif/graph-client'

  • createClient(options: object): Client: creates and returns a new graphql client
    • options.url: (required), the graphql endpoint to query
    • options.cache: (optional, default = undefined), the cache implementation to use, it must implement set(key: string, value: object) and get(key: string): object to be compatible. You can use a Map or lru package for example.
    • options.token: (optional, default = undefined) will be used to add an authorization header, can be:
      • a ̀string: in which cache it will the client will add Bearer in front of it
      • a function:
        • if the function returns a Promise, the client will wait the promise to resolve, then add Bearer before the returned value. Attention, your query will not be send to your graphql API until the promise is resolved!
        • in other cases, the Bearer is added to the returned value
    • options.logger: (optional, default = console), the logger to user, it can implement logger.trace(value: any) or logger('trace', value: any) and will be used to log errors if found
    • options.headers: (optional; default = {}), the headers to set to http requests

const graphql = createClient(**options**)

  • graphql.setHeaders(param: function|object):
    • if param is a function it will be called with the previous headers and the returned value will be used as new headers
    • in other cases param will be used as new headers
  • graphql(query: string, variables: object, options: object): Promise: will query your endpoint and returns the data part
    • if cache is set it will be used, and if an entry is found it will be returned without calling the API
    • if the query is a mutation, the cache will not be used
    • if options.noCache is set to true the cache, even if it exists, will not be used

Usage / Examples

Minimum

This is minimal informations you have to give to use this lib. In this case, the client does not use cache.

import createClient from '@fabienjuif/graph-client'

const graphql = createClient({
  url: 'https://my-domain/graphql',
})

const QUERY = `
query GetUser($id: String!) {
  user (id: $id) {
    id
    name
    email
  }
}
`

graphql(QUERY, { id: '2' })
  .then(data => console.log(data))

LRU cache

In this example, the client will use a cache from an external library (here lru).

import LRU from 'lru'
import createClient from '@fabienjuif/graph-client'

const graphql = createClient({
  url: 'http://my-domain.com/graphql'
  cache: new LRU(100), // max 100 items
})

const QUERY = `
query getUser($id: String!) {
  user(id: $id) {
    id
    name
    email
  }
}
`
const run = async () => {
  // first call will set the result into the cache
  // the cache key is a composition of your query and variables
  // both should be serializable
  const { user } = await graphql(QUERY, { id: '2' })
  console.log(user)

  // second call will use cache instead of quering the database
  const { user: user2 } = await graphql(QUERY, { id: '2' })
  console.log(user2)

  // third call will call your endpoint because variables are differents
  // a new entry will be added to the cache
  const { user: user3 } = await graphql(QUERY, { id: '3' })
  console.log(user3)
}
run()

Map cache

In this example, the client will use a javascript Map as a cache.

import createClient from '@fabienjuif/graph-client'

const graphql = createClient({
  url: 'http://my-domain.com/graphql'
  cache: new Map(),
})

const QUERY = `
query getUser($id: String!) {
  user(id: $id) {
    id
    name
    email
  }
}
`
const run = async () => {
  // first call will set the result into the cache
  // the cache key is a composition of your query and variables
  // both should be serializable
  const { user } = await graphql(QUERY, { id: '2' })
  console.log(user)

  // second call will use cache instead of quering the database
  const { user: user2 } = await graphql(QUERY, { id: '2' })
  console.log(user2)

  // third call will call your endpoint because variables are differents
  // a new entry will be added to the cache
  const { user: user3 } = await graphql(QUERY, { id: '3' })
  console.log(user3)
}
run()

Disable cache for specific queries

In this example cache is set from a javascript Map. The cache will not be used for one of the request, even if the cache is set and the query is a graphql query (not a mutation).

import createClient from '@fabienjuif/graph-client'

const graphql = createClient({
  url: 'https://my-domain/graphql',
  cache: new Map(), // use a Javascript map as cache
})

const CACHED_QUERY = `
query GetUser($id: String!) {
  user (id: $id) {
    id
    name
    email
  }
}
`

// this request will be cached
graphql(CACHED_QUERY, { id: '2' })
  .then(data => console.log(data))

const QUERY = `
query GetTopics($max: Int!) {
  topics (max: $max) {
    id
    title
  }
}
`

// this request will NOT be cached because we ask not to use it in request scope
// even if the cache is specified in the factory
graphql(QUERY, { max: 10 }, { noCache: true })
  .then(data => console.log(data))

Set a token

In this example the token is retrieved from the localStorage for each request. If the token does not exists then the Authorization header will not be set

import createClient from '@fabienjuif/graph-client'

const getToken = () => localStorage.getItem('token')

const graphql = createClient({
  url: 'https://my-domain/graphql',
  token: getToken, // token can also be a string, or a function that returns a promise
})

const run = async () => {
  // set a token
  localStorage.setItem('token', 'my-token')

  // first call the Authorization header is set:
  //  - Authorization: Bearer my-token
  const { user } = await graphql(QUERY, { id: '2' })
  console.log(user)

  // remove the token
  localStorage.removeItem('token')

  // second call the Authorization header is not set
  const { user: user2 } = await graphql(QUERY, { id: '2' })
  console.log(user2)
}
run()
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].