https://github.com/antoniopresto/graphql-clientgen
https://github.com/antoniopresto/graphql-clientgen
Last synced: about 2 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/antoniopresto/graphql-clientgen
- Owner: antoniopresto
- License: mit
- Created: 2019-06-17T19:20:59.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2020-10-28T17:15:45.000Z (over 4 years ago)
- Last Synced: 2025-03-23T18:52:14.555Z (2 months ago)
- Language: TypeScript
- Size: 439 KB
- Stars: 6
- Watchers: 0
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# graphql-clientgen
> [Work in progress]Generate typescript [client script](https://github.com/antoniopresto/graphql-clientgen/blob/master/examples/Client.ts) 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
```json
"scripts": {
"get-client": "get-client http://localhost:3777/graphql dest-path/",
}
```
then run: `npm run get-client`3) Using the generated code
```ts
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>methods.posts({}); // Promise
// one default query is generated for each query/mutation,
// but we can pass a custom fragment.
methods.posts({}, { fragment: `id title` }); // Promise<(Partial)[]>
```### cli:
`$ get-client http://localhost:3777/graphql`
`$ get-client --help`
### api
```ts
// 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'
);xfs.writeFile(__dirname + '/client.ts', client);
```[Complete generated code example](https://github.com/antoniopresto/graphql-clientgen/blob/master/client.ts#L152)
[antoniopresto.github.io/graphql-clientgen/](https://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