npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

gql-hook-codegen

v1.2.0

Published

A utility to generate graphql hook from query string and schema

Downloads

39

Readme

gql-hook-codegen

semantic-release

This tool generates TypeScript types for queries/mutations written in an GraphQL project given a valid GraphQL schema.

hook-generation-git

Install

Yarn

yarn add gql-hook-codegen

NPM

npm install gql-hook-codegen

How to use

Step 1: Create a Typescript file use-user.gql.ts with the following content

import gql from 'graphql-tag'

const query = gql`
  query {
    user {
      name
    }
  }
`

Step 2: Add schema file schema.gql

type User {
  id: ID!
  name: String
}

type Query {
  user(id: ID!): User
}

Step 3: Run the following code:

npx gql-hook-codegen generate --pattern="*.gql.ts"

Step 4: Script will update use-user.gql.ts to the following:

import { QueryHookOptions, useQuery } from '@apollo/client'
import gql from 'graphql-tag'

const query = gql`
  query fetchUser($id: ID!) {
    user(id: $id) {
      name
    }
  }
`

export interface RequestType {
  id: string | undefined
}

export interface QueryType {
  user?: UserType
}

export interface UserType {
  name?: string
  __typename?: 'User'
}

export function useUserQuery(
  request: RequestType,
  options?: QueryHookOptions<QueryType, RequestType>,
) {
  return useQuery<QueryType, RequestType>(query, {
    variables: request,
    skip: !request.id,
    ...options,
  })
}

VS Code Integration

Install Save and Run plugin and add the following code to .settings.json

{
  "saveAndRun": {
    "commands": [
      {
        "match": ".gql.ts",
        "cmd": "npx gql-hook-codegen generate --schema-file=../partner-portal-be/schema.gql --pattern='${file}'",
        "useShortcut": false,
        "silent": false
      }
    ]
  }
}

More Examples

  1. Schema
  2. Query
  3. Query with no parameters
  4. Batched Queries
  5. Query with multiple inputs
  6. Query with enum
  7. Query with date
  8. Query with shared variable
  9. Mutation
  10. Lazy query
  11. Query with union

Options

gql-hook-codegen   <generate> [--help] [--doc]

COMMANDS

generate

COMMON

--help     Show help

--doc      Generate documentation

gql-hook-codegen generate


gql-hook-codegen generate   [--pattern=<string>]
                                      [--file=<string>]
                                      [--schema-file=<string>]
                                      [--schema-url=<string>]
                                      [--ignore=<string>]
                                      [--package=<string>]
                                      [--save]

OPTIONS

--pattern=<string>       File pattern

--file=<string>          A specific file to process

--schema-file=<string>   Location of the schema file

--schema-url=<string>    Url to fetch graphql schema from

--ignore=<string>        Folders to ignore

--package=<string>       Default package to use

--save                   Save schema locally if --schema-url is
                         used

Automatic Release

Here is an example of the release type that will be done based on a commit messages:

| Commit message | Release type | | ------------------- | --------------------- | | fix: [comment] | Patch Release | | feat: [comment] | Minor Feature Release | | perf: [comment] | Major Feature Release | | doc: [comment] | No Release | | refactor: [comment] | No Release | | chore: [comment] | No Release |