create-next-api
TypeScript icon, indicating that this package has built-in type declarations

0.0.2 • Public • Published

create-next-api

[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

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

Readme

Keywords

none

Package Sidebar

Install

npm i create-next-api

Weekly Downloads

1

Version

0.0.2

License

MIT

Unpacked Size

383 kB

Total Files

50

Last publish

Collaborators

  • antonio.presto