New:Introducing Socket Scanning for VS Code Marketplace Extensions.Learn more →
Get Started

@stackup/codegen

Package Overview
Dependencies
Maintainers
1
Versions
29
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@stackup/codegen

Generate types for a GraphQL API

Source
npmnpm
Version
0.8.2
Version published
Weekly downloads
6
-92.77%
Maintainers
1
Weekly downloads
 
Created
Source

@stackup/codegen

Build Version License

A zero-config CLI tool to generate GraphQL type definitions and React Hooks. It's a thin wrapper around GraphQL code generator.

Install

$ yarn add @stackup/codegen --dev
$ yarn codegen --help
Usage: codegen [ROOT] [options]

Generate type definitions from GraphQL queries.

Arguments:
  ROOT (required)
    The directory where your GraphQL queries live.

Options:
  -s, --schema <SCHEMA> (required)
    URL or file path to a GraphQL schema. This option will be overridden
    when the SCHEMA environment variable is set.

  -c, --client <react-query|react-apollo> (default: react-apollo)
    The preferred GraphQL client.

  --suffix
    Append a suffix to operations (e.g. usePersonQuery). This
    is helpful for avoiding naming collisions.

  --immutable
    Generate readonly types.

  --colocate <common path>
    Generate files adjacent to their GraphQL source.

  -v, --version
    Output the version number

  -h, --help
    Display this help information

Usage

You have a directory of queries:

src/
  queries/
    posts.graphql
    users.graphql

Run the code generator:

$ yarn codegen src/queries --schema http://localhost:3000/graphql

Now, you have code!

src/
  queries/
    index.ts       <- This module exports typed React Hooks
    schema.graphql <- This file contains your GraphQL schema
    posts.graphql
    users.graphql

Colocated Queries

What if you want your queries to live next to your components?

src/
  components/
    User/
      User.tsx
      queries.graphql

Run the code generator:

$ yarn codegen src --colocate src/queries --schema http://localhost:3000/graphql

Now, your file tree should look like this:

src/
  queries/
    index.ts       <- This module exports your schema's types
    schema.graphql <- This file contains your GraphQL schema
  components/
    User/
      User.tsx
      queries.graphql
      queries.ts   <- This module exports typed React hooks

Supported Clients

react-apollo

This is the default client. It'll just work out of the box.

react-query

To generate code for use with react-query, you'll need to run the command with --client react-query.

This will also create a file client.ts if it does not exist. You should feel free to edit this file.

FAQs

Package last updated on 15 Feb 2021

Related posts