Giter Club home page Giter Club logo

graphql-call's Introduction

GraphQL Call

Library for call GraphQL API: GraphQL client

Install

npm i graphql-call isomorphic-fetch --save

Queries

Simple query

query {
    users {
        id
    }
}
import 'isomorphic-fetch';
import api, {GraphQLCall} from "graphql-call";

let client = api({url: 'http://localhost:4000/graphql'});

client.query({
    users: {
        result: 'id'
    }
}).then(result => {
    console.log('result: ', result);
}).catch(error => {
    console.error('error: ', error);
});

Query with params

query {
    users(search: "Test") {
        id
    }
}
client.query({
    users: {
        variables: {search: "Test"},
        result: 'id'
    }
});

Complex query

query {
    list: items (search: "Test", limit: 10, inTrash: true, labels: [1,2,4]) {
        countAll
        data {
            id
            firstName
        }
    }
}
let filter = {
    search: "Test",
    limit: 10,
    inTrash: true,
    labels: [1, 2, 4]
};
client.query({
    items: {
        variables: filter,
        result: `
        countAll
        data {
            id
            firstName
        }`,
        alias: 'list'
    }
});

Multiple queries

query {
    users {
        id
    }
    items {
        id
        name
    }
}
client.query({
    users: {
        result: 'id'
    },
    items: {
        result: `
        id
        name`
    }
});

Mutations

Mutations using pattern: nameMutation(input: {}) ...

Simple mutation

mutation {
    addUser (input: {firstName: "Ales", lastName: "Dostal"}) {
        id
    }
}
client.mutation({
    addUser: {
        variables: {firstName: "Ales", lastName: "Dostal"},
        result: 'id'
    }
});

Complex mutation

As complex query. Difference between query is: client.query(...) to client.mutation(....)

Token and headers customize

Add token to http header

let client = api({
    url: 'http://localhost:4000/graphql',
    headers: {
        Authorization: 'Bearer xxxx',
    }
});

Customize http header

let client = api({
    url: 'http://localhost:4000/graphql',
    headers: {
        Authorization: 'Bearer xxxx',
        ClientType: 'web',
        ...
    }
});

graphql-call's People

Contributors

adostal avatar

Watchers

 avatar

graphql-call's Issues

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.