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

ts-to-gql

v0.0.1-beta.3

Published

Generate gql types from ts types

Downloads

4

Readme

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 = {}