đĄGenerate TypeScript Interfaces
Learn how to generate your own TypeScript interfaces for your TS-based Airstack-powered application.
Table Of Contents
In this guide you will learn how to:
Pre-requisites
An Airstack account
Basic knowledge of GraphQL
Step 1: Install Packages
First, you need to install the required packages using your favorite package manager:
Step 2: Create codegen.ts
codegen.ts
Once you have the necessary packages, create a codegen.ts
file in your project root folder and copy the following configurations:
GraphQL Code Generator relies on a configuration file codegen.ts
to manage all possible options, input, and output document types.
Here, the types will be compiled and outputted in a single file src/graphql/types.ts
. You can modify this depending on your project needs.
Step 3: Modify package.json
Scripts
package.json
ScriptsOnce you have the codegen.ts
file ready, add the following scripts to your project's package.json
:
Here, you'll have 3 new scripts:
generate
Generate TypeScript interfaces of GraphQL queries in the project.
prestart
This script run before building the project. Here it will run the generate
script.
predev
This script run before running development mode of the project. Here it will run the generate
script.
With these scripts, you're almost ready to generate all the necessary typescript interfaces for your typescript application.
Optionally, you can also run the generate
script concurrently in watch mode with your development script and add it to your package.json
. In this example, it will be the dev
script, but some frameworks might use other development scripts such as start
instead:
This way you'll be able to easily modify your query during development without worrying any type changes, as @graphql-codegen/cli
will handle all the type changes live.
Step 4: Mark All GraphQL Queries
Now that all is setup, your last step before generating the TypeScript interfaces will be to add /* GraphQL */
on each of the GraphQL queries you would like to have the types generated:
Keep in mind to have the name of each query UNIQUE to each other as TypeScript interfaces will be generated based on the query name. Otherwise, the @graphql-codegen/cli
will throw error.
For example, the query below has name FetchWeb3Identity
. Therefore, the types generated will be:
FetchWeb3IdentityQuery
: response data type interfaceFetchWeb3IdentityVariables
: variable type interface
Step 5: Generate TypeScript Interfaces
Finally, you can run the generate
scripts to generate the necessary typescript interfaces:
Once successfully completed, you will see a new file src/graphql/types.ts
generated that contains all the necessary TypeScript interfaces for your GraphQL queries.
From src/graphql/types.ts
, you can import all the necessary types for your Airstack query:
Alternatively, if you added watch mode to run concurrently with your development or production build script, you can simply run those scripts and the types will be generated upon development or build time.
Developer Support
If you have any questions or need help regarding generating TypeScript interfaces into your Airstack query, please join our Airstack's Telegram group.
More Resources
Last updated
Was this helpful?