graphql-sdk-generator
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

GraphQL SDK Generator

The GraphQL SDK Generator converts GraphQL schemas into JavaScript or TypeScript code, enabling smooth autocomplete and validation for your GraphQL queries.

NPM version Run Eslint & Test cases Coverage Status

Read this quick start guide to generate your client and start writing queries

Features

  • ✅ Type completion & validation for enhanced developer experience
  • 🍃 Few runtime dependencies as compared to GraphQL Mesh
  • 🐎 Generate client only if schema changes
  • 🥃 Supports custom headers for requests
  • 🚂 Compatible with both browsers and Node.js, thanks tographql-request

Example

  1. Install the required package from npm globally recommended
npm install -g graphql-sdk-generator
  1. Then will create a config.json file with the following contents.
{
  "url": "your-graphql-endpoint",
  "sdkName": "custom-name",
  "fileType": "ts",
  "debug": true
}
  1. Run the generator and install additional dependencies required for making requests:
// use a custom json file path
graphql-sdk-generator -c config.json
npm i @graphql-typed-document-node/core graphql-request
  1. Use the generated client to write methods and send data:
import { GraphQLClient } from 'graphql-request';
import { getSdk } from '../graphqlSDKGenerator/graphqlSDKGenerator';

const initializeSDK = () => {
  // we can set custom headers
  const client = new GraphQLClient('https://your-graphql-url', {
    headers: {},
  });

  return getSdk(client);
};

const main = async () => {
  const sdkInstance = initializeSDK();
  const companies = await sdkInstance.companyQuery();

  // we get autocomplete here both for the arguments && output.
  const user = await sdkInstance.insert_usersMutation({
    objects: { id: 1, name: 'test', rocket: 'spacex' },
  });

  console.log(companies, user);
};

main();

Docs

Example

  • Example repo with config file examples can be found here

Licensed under MIT.

Package Sidebar

Install

npm i graphql-sdk-generator

Weekly Downloads

1

Version

1.0.0

License

MIT

Unpacked Size

53.2 kB

Total Files

21

Last publish

Collaborators

  • siddharth9890