0.0.1-1 • Published 7 years ago

typed-graph v0.0.1-1

Weekly downloads
3
License
MIT
Repository
github
Last release
7 years ago

TypedGraph

:construction: Simple GraphQL client for TypeScript.

Usage

Add TypeGraph to your project

$ npm install typed-graph

Generate TypeScript declaration files

To help you query a GraphQL server, TypedGraph needs to be informed of the type defintions used by the server.

From this point, we will be taking GitHub GraphQL API v4 as an example. However, you can replace it by whatever GraphQL API of your choice. The only requirement is that the GraphQL API supports introspection.

The following command downloads the schema definition from the server and outputs a TypeScript declaration file github.d.ts in the current working directory.

You may want to check the requirements of the server first. In our example, GitHub API needs an Authorization header.

$ npx graphql-url-to-typescript -o ./github.d.ts https://api.github.com/graphql 'Authorization:Bearer <your GitHub personal access token>'

npx is a tool that has been integrated to npm as of version 5.2.0. Read the release notes for additional information.

Alternatively, installing graphql-url-to-typescript globally:

$ npm install --global graphql-url-to-typescript
$ graphql-url-to-typescript -o ./github.d.ts https://api.github.com/graphql 'Authorization:Bearer <your GitHub personal access token>'

Instantiate the client and write your queries

An example on how to get the id and the last publication's date of AVA:

import TypedGraph from 'typed-graph'
import GitHubAPI from './github'

const client = new TypedGraph.Client<GitHubAPI>({
    url: 'https://api.github.com/graphql',
    headers: { 'Authorization': 'Bearer <your GitHub personal access token>' }
})

const query = new client.Query(options)
    .get('repository', {
        owner: 'avajs',
        name: 'ava'
    }, repository => repository
        .get('releases', { last: 1 }, _ => _
            .get('nodes', releases => releases.get('publishedAt')))
        .get('id'))

query
    .send()
    .then(JSON.stringify)
    .then(console.log)
    .catch(console.error)

TODO