• Stars
    star
    2,256
  • Rank 19,654 (Top 0.4 %)
  • Language
    JavaScript
  • License
    MIT License
  • Created over 7 years ago
  • Updated about 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Simple and Isomorphic GraphQL Client for JavaScript

graphql.js: lightweight graphql client

Lightest GraphQL client with intelligent features.


Bower version NPM version Build Status

Originally inspired by Robert Mosolgo's blog post

Features

  • Too small, 4k gzipped.
  • No dependencies, plain vanilla JavaScript.
  • Plug & Play.
  • Isomorphic.
  • Runs on most browsers.
  • You don't need to install Node.js ecosystem on your computer.
  • Query merging to reduce request number.

Overview

GraphQL is based on a very simple HTTP transaction, which sends a request to an endpoint with query and variables.

Many libraries require complex stacks to make that simple request. In any project you don't use React, Relay, you'll need a simpler client which manages your query and makes a simple request.

// Connect...
var graph = graphql("/graphql")

// Prepare...
graph.fragment({
  user: `on User {
    id,
    name
  }`
})

const allUsers = graph(`query { allUsers { ...user } }`)

const createUser = graph(`mutation (@autodeclare) {
  createUser($firstName, $lastName) { ...user }
}`)

await createUser({
  firstName: "John",
  lastName: "Doe"
})

const users = await allUsers()

console.log(users)
// {
//   "allUsers": [{ "id": 1, "name": "John Doe" }]
// }

Installation

You can download graphql.js directly, or you can use Bower or NPM.

CDN

<script src="//cdn.jsdelivr.net/npm/[email protected]/graphql.min.js"></script>

Download for Browser

Using NPM

npm install graphql.js --save

# or

yarn add graphql.js

Using with Rails Asset Pipeline

You can use GraphQL.js with Rails Asset Pipeline using graphqljs-rails.

Using

GraphQL.js is isomorphic. You can use it in both browser and Node.js.

Use in Browser

<script src="/path/to/graphql.js"></script>

Use in Node.js

var graphql = require('graphql.js')

Or using import

import graphql from 'graphql.js'

Connection

Create a simple connection to your GraphQL endpoint.

var graph = graphql("http://localhost:3000/graphql", {
  method: "POST", // POST by default.
  headers: {
    // headers
    "Access-Token": "some-access-token"
    // OR "Access-Token": () => "some-access-token"
  },
  fragments: {
    // fragments, you don't need to say `fragment name`.
    auth: "on User { token }",
    error: "on Error { messages }"
  }
})

Executing Queries and Mutations

graph will be a simple function that accepts query and variables as parameters.

graph(`query ($email: String!, $password: String!) {
  auth(email: $email, password: $password) {
    ... auth # if you use any fragment, it will be added to the query.
    ... error
  }
}`, {
  email: "[email protected]",
  password: "my-super-password"
}).then(function (response) {
  // response is originally response.data of query result
  console.log(response)
}).catch(function (error) {
  // response is originally response.errors of query result
  console.log(error)
})

Prepare Query for Lazy Execution

You can prepare queries for lazy execution. This will allow you to reuse your queries with different variables without any hassle.

var login = graph(`query ($email: String!, $password: String!) {
  auth(email: $email, password: $password) {
    ... on User {
      token
    }
  }
}`)

// Call it later...
login({
  email: "[email protected]",
  password: "my-super-password"
})

Direct Execution with .run and ES6 Template Tag

If your query doesn't need any variables, it will generate a lazy execution query by default. If you want to run your query immediately, you have three following options:

// 1st option. create and run function.
graph(`...`)()
graph.query(`...`)()
graph.mutate(`...`)()
//...

// 2nd option. create and run function with `run` method.
graph.run(`...`)
graph.query.run(`...`)
graph.mutate.run(`...`)

// 3rd option. create and run function with template tag.
graph`...`
graph.query`...`
graph.mutate`...`

I don't recommend using this. Using it too much may break DRY. Use lazy execution as much as possible.

Prefix Helpers

You can prefix your queries by simply calling helper methods: .query, .mutate or .subscribe

var login = graph.query(`($email: String!, $password: String!) {
  auth(email: $email, password: $password) {
    ... on User {
      token
    }
  }
}`)

var increment = graph.mutate`increment { state }`
var onIncrement = graph.subscribe`onIncrement { state }`

Automatic Declaring with @autodeclare or {declare: true}

Declaring primitive-typed (String, Int, Float, Boolean) variables in query were a little bothering to me. That's why I added an @autodeclare keyword or {declare: true} setting to the processor. It detects types from the variables and declares them in query automatically.

var login = graph.query(`(@autodeclare) {
  auth(email: $email, password: $password) {
    ... on User {
      token
    }
  }
}`)

login({
  email: "[email protected]", // It's String! obviously.
  password: "my-super-password" // It is, too.
})

This will create following query:

query ($email: String!, $password: String!) {
  auth(email: $email, password: $password) {
    ... on User {
      token
    }
  }
}

You can also pass {declare: true} option to the .query, .mutate and .subscribe helper:

var login = graph.query(`auth(email: $email, password: $password) {
  ... on User {
    token
  }
}`, {declare: true})

This will also create the same query above.

Detecting IDs

Variable names with matching /_id/i pattern will be declared as ID type. Following examples will be declared as IDs:

  • id: 1 will be declared as $id: ID!
  • post_id: "123af" will be declared as $post_id: ID!
  • postID: 3 will be declared as $postID: ID!
  • postId: 4 will be declared as $postId: ID!

You can disable auto ID declaration by adding an ! to the end of the variable name:

  • id!: 1 will be declared as $id: Int!
  • post_id!: "123af" will be declared as $post_id: String!

And, explicitly given types are prioritized.

  • postID!CustomId: 3 will be declared as $postID: CustomId!
  • postId!UUID: 4 will be declared as $postId: UUID!
var userById = graph.query(`(@autodeclare) {
  user(id: $id) {
    email
  }
}`)

userById({
  id: 15
})

The example above will generate following query:

query ($id: ID!) {
  user(id: $id) {
    email
  }
}

Solving Integer and Float Problem

Let's say you have a rating query that accepts an argument with a Float argument named rating. GraphQL.js will declare 10 value as Integer since it casts using value % 1 === 0 ? 'Int' : 'Float' check.

var rate = graph.query(`(@autodeclare) {
  rating(rating: $rating) {
    rating
  }
}`)

rate({
  rating: 10
})

In this case, you must use ! mark to force your type to be Float as below:

rate({
  "rating!Float": 10
})

This will bypass the casting and declare rating as Float.

Advanced Auto Declaring

Beside you can pass {declare: true} to helpers:

graph.query("auth(email: $email, password: $password) { token }", {declare: true})

Also you can enable auto declaration to run by default using alwaysAutodeclare setting.

var graph = graphql("http://localhost:3000/graphql", {
  alwaysAutodeclare: true
})

After you enable alwaysAutodeclare option, your methods will try to detect types of variables and declare them.

// When alwaysAutodeclare is true, you don't have to pass {declare: true} option.

graph.query("auth(email: $email, password: $password) { token }")

Auto Declaring Custom Types

You can define custom types when defining variables by using a simple "variable!Type" notation. It will help you to make more complex variables:

var register = graph.mutate(`(@autodeclare) {
  userRegister(input: $input) { ... }
}`)

register({
  // variable name and type.
  "input!UserRegisterInput": { ... }
})

This will generate following query:

mutation ($input: UserRegisterInput!) {
  userRegister(input: $input) { ... }
}

Fragments

Fragments make your GraphQL more DRY and improves reusability. With .fragment method, you'll manage your fragments easily.

Simple Fragments

While constructing your endpoint, you can predefine all of your fragments.

var graph = graphql("/graphql", {
  fragments: {
    userInfo: `on User { id, name, surname, avatar }`
  }
})

And you can use your fragments in your queries. The query will pick your fragments and will add them to the bottom of your query.

graph.query(`{ allUsers { ...userInfo } }`)

Nested Fragments

You can nest your fragments to keep them organized/namespaced.

var graph = graphql("/graphql", {
  fragments: {
    user: {
      info: `on User { id, name, surname, avatar }`
    }
  }
})

Accessing them is also intuitive:

graph.query(`{ allUsers { ...user.info } }`)

Using Fragments in Fragments

You can reuse fragments in your fragments.

graph.fragment({
  user: "on User {name, surname}",
  login: {
    auth: "on User {token, ...user}"
  }
})

Lazy Fragments

You can also add fragments lazily. So you can use your fragments more modular.

// Adds a profile fragment
graph.fragment({
  profile: `on User {
    id
    name(full: true)
    avatar
  }`
})

var allUsers = graph.query(`{
  allUsers {
    ... profile
  }
}`)

allUsers().then(...)

Also you can add nested fragments lazily, too:

graph.fragment({
  login: {
    error: `on LoginError {
      reason
    }`
  }
})

graph.fragment({
  something: {
    error: `on SomeError {
      messages
    }`
  }
})

graph.query(`{ login {... login.error } }`)
graph.query(`{ something {... something.error } }`)

Getting Fragments by Path

You can call fragment string by using .fragment method. You have to pass path string to get the fragment.

graph.fragment('login.error')

This will give you the matching fragment code:

fragment login_error on LoginError {
  reason
}

Using Fragments in Tag Query

You can use fragments lazily using ES6 template tag queries.

var userProfileToShow = graph.fragment('user.profile')

graph`query { ... ${userProfileToShow} }`

Query Building

You can create queries using .ql ES6 template tag.

// Add some fragments...
graph.fragment({
  username: {
    user: `on User {
      username
    }`,
    admin: `on AdminUser {
      username,
      administrationLevel
    }`
  }
})

// Get any fragment with its path...
var admin = graph.fragment('username.admin')

// Build your query with using fragment paths or dynamic template variables.
var query = graph.ql`query {
  ...username.user
  ...${admin}
}`

// Use query anywhere...
$.post("/graphql", {query: query}, function (response) { ... })

graph.ql will generate this query string:

query {
  ... username_user
  ... username_admin
}

fragment username_user on User {
  username
}

fragment username_admin on AdminUser {
  username,
  administrationLevel
}

Query Merging: Merge Multiple Queries into One Request

merge

This GIF shows a before/after case to make an example how query merging changes the performance.

graphql.js supports query merging that allows you to collect all the requests into one request.

Assume we've these queries on server, define them just like before we do:

var fetchPost = graph.query(`{
  post(id: $id) {
    id
    title
    text
  }
}`)

var fetchComments = graph.query(`{
  commentsOfPost: comments(postId: $postId) {
    comment
    owner {
      name
    }
  }
}`)

Normally, we make requests as following:

var postId = 123

// This will send a request.
fetchPost({ id: postId }).then(function (response) {
  console.log(response.post)
})

// This also will send a request.
fetchComments({ postId: postId }).then(function (response) {
  console.log(response.commentsOfPost)
})

This will make two requests:

ss1

Use .merge(mergeName, variables) command to put them into a merge buffer:

var postId = 123

// This won't send a request.
fetchPost.merge('buildPage', { id: postId }).then(function (response) {
  console.log(response.post)
})

// This also won't send a request.
fetchComments.merge('buildPage', { postId: postId }).then(function (response) {
  console.log(response.commentsOfPost)
})

These will create a buffer with buildPage name, and append the queries to that buffer. You need to use commit(mergeName) to merge the buffer and send to the server, the response will be consolidated:

// This will send a merged request:
graph.commit('buildPage').then(function (response) {
  // All base fields will be in response return.
  console.log(response.post)
  console.log(response.commentsOfPost)
})

And this will create only one request:

ss2

This will create the following merged query generated by graphql.js:

query ($merge024533__id: ID!, $merge141499__postId: ID!) {
  merge024533_post: {
    post(id: $merge024533__id) {
      id
      title
      text
    }
  }
  merge141499_commentsOfPost: {
    comments(postId: $merge141499__postId) {
      comment
      owner {
        name
      }
    }
  }
}

And variables will be generated, too:

{
  "merge024533__id": 123,
  "merge141499__postId": 123
}

The merge{number} aliases won't be passed into your responses, since they will be used for initial seperation.

⚠️ Important Restriction: You cannot use multiple root fields using query merging. ⚠️ Important Restriction: Autodeclaration is on by default, do not use alwaysAutodeclare: true.

Debugging

You can pass debug: true to options parameter to get a console output looks like following:

[graphql]: POST http://localhost:3000/graphql
  QUERY: query ($email: String!, $password: String!) {
    auth(email: $email, password: $password) {
      .. login_auth
    }
  }

  fragment info on User { hasPin }
  fragment login_auth on User { token, ...info }

  VARIABLES: {
    "email": "[email protected]",
    "password": "123123"
  }

  sending as form url-data

Advanced

Using with Vue.js

Create a GraphQLProvider.js.

import graphql from 'graphql.js';

/* eslint-disable no-underscore-dangle */
export default {
  install(Vue, url, options) {
    Vue.mixin({
      created() {
        this._graph = graphql(url, options);
      },
    });
    Object.defineProperty(Vue.prototype, '$graph', {
      get() {
        return this._graph;
      },
    });
  },
};

And then you can use this with your Vue app:

import Vue from 'vue';
import GraphQLProvider from './GraphQLProvider';

Vue.use(GraphQLProvider, 'http://localhost:3000/graphql', {
  headers: {
    // headers...
  },
});

// ... in your Vue VM
data() {
  return {
    hello: '',
  };
},
methods: {
  makeSomeQuery() {
    this.$graph.query(`{hello}`).then(response => {
      this.hello = response.hello;
    });
  },
}

Change POST Method

As default, GraphQL.js makes a POST request. But you can change the behavior by setting asJSON.

var graph = graphql("http://localhost:3000/graphql", {
  asJSON: true
});

Using with graphql-tag

graphql-tag converts GraphQL query strings to AST. You can use graphql-tag with GraphQL.js

graph.query(gql`query { name }`)

Using graphql-tag will not allow you to use auto declaration and nested fragments syntaxes since these are not valid query syntax for GraphQL but only for this library.

Change Url Anywhere

You can change url anywhere with setUrl method.

var graph = graphql("http://localhost:3000/graphql", {
  asJSON: true
});

// Change url
graph.setUrl('http://www.example.com')

// Run query
graph.query(`{ name }`)

Todo App Example

A CRUD ToDo app example code to show how to use GraphQL.js. An implementation can be found at f/graphql.js-demo

var graph = graphql("/graphql", {
  alwaysAutodeclare: true,
  fragments: {
    todo: `on Todo {
      id
      text
      isCompleted
    }`
  }
})

function getTodos() {
  return graph.query.run(`allTodos {
    ...todo
  }`)
}

function addTodo(text) {
  return graph.mutate(`todoAdd(text: $text) {
    ...todo
  }`, {
    text: text
  })
}

function setTodo(id, isCompleted) {
  return graph.mutate(`todoComplete(
    id: $id,
    status: $isCompleted
  ) {
    ...todo
  }`, {
    id: id,
    isCompleted: isCompleted
  })
}

function removeTodo(id) {
  return graph.mutate(`todoRemove(
    id: $id
  ) {
    ...todo
  }`, {
    id: id
  })
}

License

MIT License

Copyright (c) 2018 Fatih Kadir Akın

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:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

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.

More Repositories

1

awesome-chatgpt-prompts

This repo includes ChatGPT prompt curation to use ChatGPT better.
HTML
95,527
star
2

vue-wait

Complex Loader and Progress Management for Vue/Vuex and Nuxt Applications
JavaScript
2,005
star
3

omelette

Omelette is a simple, template based autocompletion tool for Node and Deno projects with super easy API. (For Bash, Zsh and Fish)
CoffeeScript
1,355
star
4

delorean

An Agnostic, Complete Flux Architecture Framework
JavaScript
747
star
5

fatura

eArşiv / eFatura Fatura Oluşturucu
JavaScript
548
star
6

vue-smart-route

Smart route search to make intelligent looking apps with Vue.js.
JavaScript
319
star
7

react-wait

Complex Loader Management Hook for React Applications
JavaScript
305
star
8

atom-term2

THIS PROJECT IS NO LONGER MAINTAINED. PLEASE USE ATOM TERM3 PACKAGE
CoffeeScript
296
star
9

guardian

Guardian watches over your files and run assigned tasks.
Crystal
277
star
10

loremjs

Lorem.js Dummy Text/Image Generator for jQuery and Native JS
HTML
189
star
11

react-hooks-todo-app

A highly testable TodoList app that uses React hooks and Context.
JavaScript
187
star
12

kamber

Static site server (basically blogs) with Crystal Language
Crystal
173
star
13

dom-wait

Complex Loader and Progress Management for Vanilla JS
HTML
143
star
14

pq

Human Readable Promise Chains
JavaScript
143
star
15

vue-plugin-boilerplate

Vue Plugin Boilerplate
JavaScript
122
star
16

confirmation

A simple Node tool to replicate Browser's confirm popup on your CLI.
JavaScript
115
star
17

netflix-data

All Netflix Genres
111
star
18

dahi

Türkçe NLP'de bir marka (Parody)
JavaScript
97
star
19

atom-bootstrap3

Twitter Bootstrap 3 Snippets for Atom
CoffeeScript
81
star
20

completion

Easy Command Line Completion for Crystal
Crystal
70
star
21

honst

Fixes your dataset according to your rules.
JavaScript
69
star
22

do-sshuttle

Use DigitalOcean Droplet to Transparent Proxying via sshuttle
Shell
66
star
23

baristai

TypeScript
66
star
24

equiv

It finds equivalents of the things.
Crystal
60
star
25

kemal-react-chat

This demonstrates how easy it is to build Realtime Web applications with Kemal.
JavaScript
60
star
26

temel

Extensible Markup DSL for Crystal
Crystal
59
star
27

omi-devtools

DevTools for Omi.js
JavaScript
54
star
28

vue-analog-clock-range

Vue Analog Clock Range Component
Vue
52
star
29

kreal

Kreal is a model sharing & RPC library built on and works with Kemal seamlessly.
HTML
44
star
30

ufcs

DLang's Universal Function Call Syntax port to JavaScript
JavaScript
42
star
31

xtract

Extract data from DOM, easily.
JavaScript
40
star
32

lambda.cr

Uniformed function call syntax for Crystal Language.
Crystal
36
star
33

qvimrc

Quick Vimrc
Vim Script
33
star
34

deasciifier

Deasciifier yabancı metinleri Türkçeye çevirmenize yarayan bir uygulamadır.
JavaScript
29
star
35

emoji-downloader

A simple Emoji downloader for CLI
Shell
29
star
36

phaser-coffee-boilerplate

Phaser CoffeeScript + Browserify + LiveReload Boilerplate
CoffeeScript
27
star
37

hexwords-tr

Turkish Hex Words
JavaScript
27
star
38

graphql.js-demo

GraphQL.js Sinatra Example
Ruby
24
star
39

macaron

Macros for CoffeeScript
CoffeeScript
24
star
40

postcss-inline-image

PostCSS plugin that puts images as data URIs into your CSS
JavaScript
21
star
41

sofle-mario

C
21
star
42

dont-fail-me-again

Node.js Error Handler by The Dark Side
JavaScript
21
star
43

fka

JavaScript
20
star
44

idiot

Easier NodeMCU Environment - IoT Made Simple
MoonScript
19
star
45

storm-bitbar

BitBar plugin for Storm-SSH
Shell
16
star
46

veznedar

Arapça Kök Üreteci
JavaScript
16
star
47

graphqljs-rails

GraphQL.js Rails for Rails 5
JavaScript
15
star
48

f

14
star
49

view.coffee

Dead Simple, Vanilla-ish Client-Side Framework based on jQuery, inspired by GitHub's Space-Pen Library
CoffeeScript
13
star
50

jquery.resizestop

Special Event for Resize Controls
12
star
51

awesome-safran

Safran CLI Okuyucuları
12
star
52

safran-cli

Safran.io için command line okuyucu.
JavaScript
11
star
53

GitHubDashboard.kdapp

GitHubDashboard
CoffeeScript
11
star
54

twhosts

Twitter Unblock
10
star
55

jackpack

JackPack
JavaScript
9
star
56

mood-convert-ampt

JavaScript
9
star
57

backbone-presentation

Backbone.js Presentation (Turkish)
JavaScript
9
star
58

popthelock

Pop The Lock JS Clone
8
star
59

puremise.js

Yet another purely functional Promise Monad implementation
JavaScript
8
star
60

mertlang

8
star
61

boehm

7
star
62

wolves

JavaScript port of lykoss/lykos, a Werewolf party game IRC bot
CoffeeScript
7
star
63

knockbone

Knockout.js and Backbone.js Entegration
JavaScript
7
star
64

dyncall

Dynamic method calling support for Crystal Language.
Crystal
6
star
65

lama.app

OS X Application Helper for emre/lama
Objective-C
6
star
66

ircbot

IRC Bot Framework
PHP
6
star
67

kurye

GitHub Project Cloner for Boilerplate Projects
Python
6
star
68

llm-viz-tr

TypeScript
6
star
69

r3

Ruby
5
star
70

html5init

HTML5 Project Startup
CSS
5
star
71

ronin

Very simple Jinja2 based Static Site Boilerplate
Python
5
star
72

plug

jquery plugin generator.
JavaScript
5
star
73

vimme

my newest simple vim environment
Vim Script
5
star
74

shelljs

Javascript + PHP ShellLike Application
5
star
75

pinata

Sinatra-like PHP Framework
PHP
5
star
76

twittersearch

Workshop Project for Ozgur Web Gunleri 2012
JavaScript
4
star
77

restafarian

Node.js Restful Client
3
star
78

mvc

PHP
3
star
79

kamber-theme-default

Kamber Default Template
CSS
3
star
80

cssmodules-demo

Ruby
2
star
81

notes

HTML
2
star
82

win-turkish-us-layout

Windows sistemlere OS X'teki `ALT + harf` desteği sağlar.
2
star
83

kamber-theme-dark

Dark theme for Kamber
CSS
2
star
84

lolero

Lölero Language
1
star
85

knapsack-problem

Solving Knapsack Problem with dynamic programming in Ruby
Ruby
1
star
86

umut

1
star
87

dotvim

Vim Script
1
star
88

turkiye-brand-db

The Brands Database for Turkey
1
star
89

keybase-messenger

keybase based crypted local messenger interface running on firebase
1
star
90

jspy

jspy conference 2012 website
1
star
91

md5solve

MD5 decrypter. Generates content from it's MD5 sum. Work in progress.
1
star
92

react-rails-server-side-example

React Rails with Server Side Rendering Example
JavaScript
1
star
93

crystal-kemal-todo-list

TodoList based on Crystal and Kemal
HTML
1
star
94

react-playground

JavaScript
1
star
95

popeye

Popeye.js ~ Backbone.js PowerUp Wrapper
JavaScript
1
star
96

respublica

Complete Vieux Framework - "Res publica non dominetur"
1
star
97

perr

Perr UI Library using CoffeeScript
JavaScript
1
star
98

vim-script-template

Vim Script
1
star
99

dotfiles

Dotfiles for myself
Vim Script
1
star
100

eventstream.js

Pure functional EventStream monad implementation
JavaScript
1
star