Getting started with GraphQL-Sdk-Generator
GraphQL SDK generator translates GraphQL Schema to Javascript, Typescript code enabling you to get smooth auto completion and validation for your GraphQL Schema,
Installation
First install the required package from npm(we will use Apollo space x as a sample example). GraphQL SDK Generator is a standalone package. Once you generate the code, you don't need the package anymore, so it is recommended to install it globally.
Then will create a config.json file with the following contents.
Then run the graphql-sdk-generator command to generate the client inside a directory by passing path of config file using -c
Once the code is generated, you need to install additional dependencies to run the generated code. These dependencies are necessary for making requests using the generated SDK
Using the client
The generated client exposes a getSdk
function. To use it, initialize a GraphQLClient
, pass custom headers if necessary, and pass the client to getSdk
to send requests.
Node.js version Compatibility
GraphQL SDK generator will only work for Node.js 18+ and is not tested for below versions.
Handling errors
All the errors generated by client is handled by a custom error handler which you add in your codebase by seeing this example here.
Unsupported features
Currently we don't support the following features (some features are planned in future)
Custom naming for mutations, queries, variables
Allow to create multiple clients in one folder
We cannot pass a custom fetch client as the script is highly dependent on graphql-request
Batching requests
Read more
Last updated