GraphQL Codegen is a powerful tool that helps you create GraphQL schemas using JSON. With an input schema based on the GraphQL type API, it should feel familiar to most users.
You can install graphql-codegen
by running one of the commands below:
# npm
npm install @gwesseling/graphql-codegen
# yarn
yarn add @gwesseling/graphql-codegen
# pnpm
pnpm install @gwesseling/graphql-codegen
The usage of this package is straightforward.
import {codegen} from "@gwesseling/graphql-codegen";
// Using the default config
codegen()
.then((result) => console.log(result))
.catch((err) => console.log(err));
or
import {codegen} from "@gwesseling/graphql-codegen";
// Using a custom config
codegen({
inputFile: "input.json",
outputFile: "schema.ts",
})
.then((result) => console.log(result))
.catch((err) => console.log(err));
For a complete input examples, please refer to the examples folder. This folder contains a comprehensive JSON input, as well as the resulting output generated by the tool.
GraphQL-Codegen accepts the following options:
Option | Description | Type | Default |
---|---|---|---|
inputFile | Path to a file containing a JSON schema | string |
schema.json |
outputFile | Output file for the schema | string |
schema.js |