0.0.1-beta.3 ā€¢ Published 1 year ago

ts-to-gql v0.0.1-beta.3

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

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

commandexampledescription
(required) isProductiontrue 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 exampleprefix to search models
prefixMutation'Mutation' or 'GqlMutation' for exampleprefix to search mutations
prefixQuery'Query' or 'GqlQuery' for exampleprefix to find queries
removePrefixFromSchematrue or falseif true, remove prefix schema in final schema
fixSchema(schemaGql: string): string => schemaGqlfunction 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 = {}