1.0.0 • Published 10 months ago

graphql-sdk-generator v1.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

npm.io

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.