The GraphQL SDK Generator converts GraphQL schemas into JavaScript or TypeScript code, enabling smooth autocomplete and validation for your GraphQL queries.
Read this quick start guide to generate your client and start writing queries
- ✅ 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
- Install the required package from npm globally recommended
npm install -g graphql-sdk-generator
- Then will create a config.json file with the following contents.
{
"url": "your-graphql-endpoint",
"sdkName": "custom-name",
"fileType": "ts",
"debug": true
}
- 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
- 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();
- All the documententation can be found here.
- Example repo with config file examples can be found here