
Security News
GitHub Actions Checkout Now Blocks Risky pull_request_target Checkouts
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.
generate-graphql-code
Advanced tools
Generate TypeScript code from GraphQL introspection.
This module will do the following for you:
To get started, checkout the example directory or take a look at the generated files.
Table of contents:
# with pnpm
pnpm i generate-graphql-code -D
# or with npm
npm i generate-graphql-code --save-dev
In the following preview, the client is created with the factory function generated by generate-graphql-code.

When creating your GraphQL client with the factory function, you can use the generate-query module to convert the parameters to GraphQL code that you will send to the server. For more details, please check the example file.
With the command:
npx generate-graphql-code --config ./config.json
An example configuration file:
{
"files": [
{
"filename": "./graphql/example-introspection.json",
"output": "./generated/example.ts"
},
{
"endpoint": { "url": "https://countries.trevorblades.com/" },
"output": "./generated/countries.ts"
}
]
}
Or you can use the generate function programmatically:
import generate from 'generate-graphql-code'
generate({
files: [
{
filename: './graphql/example-introspection.json',
output: './generated/example.ts'
},
{
endpoint: { url: 'https://countries.trevorblades.com/' },
output: './generated/countries.ts'
}
]
})
export interface ConfigurationFile {
/**
* Global options. Default options for every schema files.
*/
options?: Options
/**
* Schema files.
*/
files?: SchemaFile[]
}
export interface SchemaFile {
/**
* The output path of the generated typescript file.
* The path is relative the configuration file.
*/
output: string
/**
* The filename of the schema introspection json file.
* The path is relative the configuration file.
*/
filename?: string
/**
* The endpoint to fetch the schema. If `filename` is defined,
* `endpoint` will be ignored.
*/
endpoint?: {
/**
* The url to fetch schema.
*/
url: string
/**
* The headers to add when requesting schema.
*/
headers?: Record<string, any>
/**
* Path to a json file. The json value will be used as `headers`.
* The path is relative the configuration file.
*/
headersFile?: string
}
/**
* The options of the current schema file. If a option of `options` is
* not set or set to `null`, the corresponding option in global options
* will be used.
*/
options?: Options
/**
* By default, `options.scalarTypes` will extend the `scalarTypes`
* defined in the global options. You can set `skipGlobalScalarTypes`
* to avoid this.
*/
skipGlobalScalarTypes?: boolean
/**
* Skip this file.
*/
skip?: boolean
}
export interface Options {
/**
* Specify scalar type mapping. The default mapping is:
*
* - Int: number
* - Float: number
* - String: string
* - Boolean: boolean
* - ID: string
*
* Example value:
*
* - `{ "Int": "number", "ID": "string" }`
* - `[ ["Int", "number"], ["ID", "string"] ]`
*
* If the a scalar type is not specified, it will be mapped to `unknown`.
*/
scalarTypes?: Record<string, string> | [string, string][]
/**
* The suffix of args types. Default: "Args".
*/
argsSuffix?: string
/**
* The suffix of fields types. Default: "Fields".
*/
fieldsSuffix?: string
/**
* Skip generating comments for disabling lint.
*/
skipLintComments?: boolean
/**
* Skip generating warning tip.
*/
skipWarningTip?: boolean
/**
* Skip wrapping enum in the args as `{ $enum: EnumType }`.
*/
skipWrappingArgsEnum?: boolean
/**
* Skip generating `xxxArgs` types. If this option is
* `true`, the factory function will not be generated too.
*/
skipArgs?: boolean
/**
* Skip generating `xxxFields` types. If this option is
* `true`, the factory function will not be generated too.
*/
skipFields?: boolean
/**
* Skip generating factory function.
*/
skipFactory?: boolean
/**
* Skip generating query method.
*/
skipQuery?: boolean
/**
* Skip generating queries object.
*/
skipQueries?: boolean
/**
* Skip generating mutation method.
*/
skipMutation?: boolean
/**
* Skip generating mutations object.
*/
skipMutations?: boolean
/**
* The file headers.
*/
headers?: string[]
/**
* The file footers.
*/
footers?: string[]
}
Open your GraphiQL page, paste the following GraphQL code to query the introspection:
query IntrospectionQuery {
__schema {
queryType {
name
}
mutationType {
name
}
subscriptionType {
name
}
types {
...FullType
}
directives {
name
description
locations
args {
...InputValue
}
}
}
}
fragment FullType on __Type {
kind
name
description
fields(includeDeprecated: true) {
name
description
args {
...InputValue
}
type {
...TypeRef
}
isDeprecated
deprecationReason
}
inputFields {
...InputValue
}
interfaces {
...TypeRef
}
enumValues(includeDeprecated: true) {
name
description
isDeprecated
deprecationReason
}
possibleTypes {
...TypeRef
}
}
fragment InputValue on __InputValue {
name
description
type {
...TypeRef
}
defaultValue
}
fragment TypeRef on __Type {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
FAQs
Generate TypeScript code from GraphQL introspection.
We found that generate-graphql-code demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
GitHub Actions checkout now blocks risky pull_request_target checkouts by default to help prevent pwn request supply chain attacks.

Product
Socket now supports Custom Roles and Repository Access Permissions so organizations can control who can access specific repositories and actions.

Product
Socket MCP now lets AI assistants review org alerts, investigate threats using the Socket threat feed, and inspect package files in addition to dependency scoring.