Giter VIP home page Giter VIP logo

graphql-clientgen's Introduction

graphql-clientgen

[Work in progress]

Generate typescript client script with batch queries for graphql projects. One client-side method is generated for each query/mutation, with a default and customizable fragment.

Basic usage

1 ) npm install --save graphql-clientgen or yarn add graphql-clientgen

2 ) Generate the client code:

Insert this script in the scripts section in your package.json

  "scripts": {
    "get-client": "get-client http://localhost:3777/graphql dest-path/",
  }

then run: npm run get-client

  1. Using the generated code
import GraphQLClient from './Client.ts';

const { methods } = new GraphQLClient({
  url: "http://localhost:3777/graphql",
});

// assuming there is a query called findUser, the generated code will accept
// arguments for the corresponding input type
methods.findUser({ filter: { name: 'Maggie' } }); // Promise<Maybe<User>>

methods.posts({}); // Promise<Post[]>

// one default query is generated for each query/mutation,
// but we can pass a custom fragment.
methods.posts({}, { fragment: `id title` }); // Promise<(Partial<Post>)[]>

cli:

$ get-client http://localhost:3777/graphql

$ get-client --help

api

// from schema
import { printClient } from 'graphql-clientgen';
let graphqlSchema: GraphQLSchema;
const client = await printClient(graphqlSchema);

// Or from endpoint
import { printFromEndpoint } from 'graphql-clientgen';
const { status, client } = await printFromEndpoint(
  'http://localhost:3000/graphql'
);x

fs.writeFile(__dirname + '/client.ts', client);

Complete generated code example

antoniopresto.github.io/graphql-clientgen/

TODO

  • generate typescript client
  • generate typed query methods
  • generate typed mutation methods
  • generate default fragments
  • batch queries
  • generate from endpoint
  • print schema
  • print query
  • improve docs
  • generate pure javascript version of the client

graphql-clientgen's People

Contributors

antoniopresto avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

graphql-clientgen's Issues

Invalid generated query: Expected Name, found }

The code generation currently expects at least one simple field.

## invalid
  type PlacePositivatorsPayload {
    positivators: [Positivator]
    positivations: [PositivatorPlace]
  }
`

## valid
```graphql
  type PlacePositivatorsPayload {
    _idSimpleFieldFooBar: String
    positivators: [Positivator]
    positivations: [PositivatorPlace]
  }
`

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.