ts-to-gql
TypeScript icon, indicating that this package has built-in type declarations

0.0.1-beta.3 • Public • Published

TS to GQL - BETA PROJECT

NPM package TypeScript Testing-Library Eslint Prettier

Introduction

Generate graphql types based on your typescript types. DO NOT ADD the schema generated in .gitignore. Schemas are generated at development time and versioned!

💻 🚀 Api example here 🚀 💻

Codacy Badge Codacy Badge

Example

// ./src/models/post.ts
export type GqlModelPostSelect = {
  id: string;
  body?: string;
};

// ./src/graphql/resolvers/mutations/post.ts
import { GqlModelPostSelect } from '@/models/Post';

interface CreatePostPayload {
  createPostPayload: {
    body: string;
  };
}

type GqlMutationPost = {
  createPost: (
    _: unknown,
    createPostPayload: CreatePostPayload
  ) => Promise<GqlModelPostSelect>;
};

export const MutationPostResolver: GqlMutationPost = {
  // code resolvers
}

configure ts-to-gql

import { searchGqlSchemaAndBuild } from 'ts-to-gql';


const typeDefs = searchGqlSchemaAndBuild({
  isProduction: false,
  pathScanProject: './src',
});

this.server = new ApolloServer<MyContext>({
  resolvers,
  typeDefs,
});

And magic, your types in graphql

type PostSelect  {
  id: String!
  body: String
};

interface CreatePostPayload {
  createPostPayload: {
    body: string;
  };
}

type Mutation {
  createPost(createPostPayload: CreatePostPayload): PostSelect!
}

Options searchGqlSchemaAndBuild

command example description
(required) isProduction true or false
true will use the generated and versioned schema in the source code. false, it will generate a new schema every hot reload (or reload) of your application.
(required) pathScanProject './src' path to search models, queries and mutations
pathSaveSchema './schema.graphql' path to save schema
prefixModel 'Model' or 'GqlModel' for example prefix to search models
prefixMutation 'Mutation' or 'GqlMutation' for example prefix to search mutations
prefixQuery 'Query' or 'GqlQuery' for example prefix to find queries
removePrefixFromSchema true or false if true, remove prefix schema in final schema
fixSchema (schemaGql: string): string => schemaGql function to fix schema, add new values or modify existents. Use to add things the library doesn't support for now

Special types

if necessary use Float, Int, ID or similar, import special types from 'ts-to-gql'. Typescript types like string, number, boolean are automatically converted to String, Number and Boolean respectively.

import {  Int, ID, DateTime, Float } from 'ts-to-gql';

Int, Float is string, DateTime is Date, and ID is string

Common errors on migration

Migration is manual, for now.

  1. Not use Partial, extends, implements, or advanced typescript. for now

  2. use types, not interfaces (for now)

  3. ts-to-gql use only second param, define first param or contexts for example

type MutationPost = {
 createPost: (input: CreatePost) => Promise<ModePost>;
}

to

type MutationPost = {
 createPost: (_: unknown, input: CreatePost) => Promise<ModePost>;
}

  1. replace your resolver to arrow types, this
type MutationPost = {
 createPost(_: unknown, input: CreatePost): Promise<ModePost>;
}

to

type MutationPost = {
 createPost: (_: unknown, input: CreatePost) => Promise<ModePost>;
}
  1. use prefix, for ts-to-gql find your models, queries and mutations (other not necessary)
type MyPost = {}

to

type MutationMyPost = {}

Package Sidebar

Install

npm i ts-to-gql

Weekly Downloads

0

Version

0.0.1-beta.3

License

ISC

Unpacked Size

57.9 kB

Total Files

77

Last publish

Collaborators

  • gabrielogregorio