1.0.1 • Published 2 years ago

graphql-codegen-arguments-plugin v1.0.1

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

graphql-codegen-arguments-plugin

A GraphQL Codegen plugin that generates TypeScript types for arguments in GraphQL queries and mutations.

Installation

To use this plugin, you must first install GraphQL Codegen and its TypeScript and TypeScript Resolvers plugins:

npm install --save-dev graphql-codegen graphql-codegen-typescript graphql-codegen-typescript-resolvers

Then, install the graphql-codegen-arguments-plugin package:

npm install --save-dev graphql-codegen-arguments-plugin

Usage

Add graphql-codegen-arguments-plugin to your codegen.yml file as a plugin:

overwrite: true
schema: "path/to/your/schema.graphql"
generates:
  path/to/generated/file.ts:
    plugins:
      - "typescript"
      - "typescript-resolvers"
      - "graphql-codegen-arguments-plugin"

The plugin will generate TypeScript types for query and mutation arguments in your GraphQL schema, which can be used in your code to ensure type safety:

import { QueryArgs, QuerysArguments } from './generated/file';
 // TypeScript error if 'myQuery' doesn't have arguments
const args: QueryArgs<'myQuery'> = {
  myQuery: {
    "arg1": 'value1',
    "arg2": 123,
  };

Example

Given the following GraphQL schema:

type Query {
  myQuery(arg1: String!, arg2: Int!): String!
}

type Mutation {
  myMutation(arg1: String!, arg2: Int!): String!
}

With the expectation that the dependent plugins are configured correctly and are generating the following:

export type QueryMyQueryArgs = {
  arg1: Scalars["String"];
  arg2: Scalars["Int"];
};

export type MutationMyMutationArgs = {
  arg1: Scalars["String"];
  arg2: Scalars["Int"];
};

The plugin will generate the following TypeScript types:

export type QuerysArguments = {
  myQuery: QueryMyQueryArgs;
};

export type QueryArgs<T extends keyof QuerysArguments> = QuerysArguments[T];

export type MutationsArguments = {
  myMutation: MutationMyMutationArgs;
};

export type MutationArgs<T extends keyof MutationsArguments> = MutationsArguments[T];

Contributing

Contributions are welcome! If you have any issues or feature requests, please open an issue on GitHub. If you would like to contribute code, please fork the repository and submit a pull request.

License

This plugin is released under the MIT License.