1.1.1 • Published 3 months ago

gql-hook-codegen v1.1.1

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

gql-hook-codegen

semantic-release

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

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 --schemaFile './schema.gql'

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,
  })
}

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

Usage

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

OPTIONS

--pattern=<string>       File pattern

--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

COMMON

--help                   Show help

Automatic Release

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

Commit messageRelease type
fix: commentPatch Release
feat: commentMinor Feature Release
perf: commentMajor Feature Release
doc: commentNo Release
refactor: commentNo Release
chore: commentNo Release
1.1.1

3 months ago

1.1.0

3 months ago

1.0.23

3 months ago

0.0.0-development

3 months ago

1.0.19

7 months ago

1.0.18

7 months ago

1.0.22

6 months ago

1.0.21

7 months ago

1.0.20

7 months ago

1.0.17

1 year ago

1.0.16

2 years ago

1.0.15

2 years ago

1.0.14

2 years ago

1.0.11

2 years ago

1.0.13

2 years ago

1.0.12

2 years ago

1.0.10

3 years ago

1.0.9

3 years ago

1.0.8

3 years ago

1.0.7

3 years ago

1.0.6

3 years ago

1.0.5

3 years ago

1.0.4

3 years ago

1.0.3

3 years ago

1.0.2

3 years ago

1.0.1

3 years ago

1.0.0

3 years ago