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

codegen-graphql-yup

v1.0.4

Published

Its a library to parse from Graphql Schema to a Yup Schema

Downloads

13

Readme

GRAPHQL SCHEMA TO YUP SCHEMA

The reason for this project is to maintain a single source of truth, between graphql and yup.

Configs

  • defaultRequiredMessage: string, this message is overwrited if you use requeridMessage in the directive.
  • onlyWithConstrain: boolean ( default false) If you want to generate an schema for all your input objects definitions with or without the directive put it in true.

You can use this pluggin de dos formas distintas

Simple use

If you only want to validate the required fields, what you can do is use the plugin in the following way

generates:
  schemas.ts:
    plugins:
      - codegen-graphql-yup:
          defaultRequiredMessage: "You can put it or not"

Full Use

If you need more validations than only required fields, you have to follow this steps.

this is because in graphql instrospection we dont have access to directives

  1. Add the directive in your schema.

  2. Generate a file, the result of merge of all schemas. Example:

    import path from 'path';
    import fs from 'fs';
    const mergeGraphqlSchemas = require('merge-graphql-schemas');
    
    const { fileLoader } = mergeGraphqlSchemas;
    const { mergeTypes } = mergeGraphqlSchemas;
    
    const types = fileLoader(path.join(__dirname, '/entities/**/*.graphql'));
    
    const mt = mergeTypes(types, { all: true });
    
    try {
        fs.writeFileSync('./result.graphql', mt);
    } catch (error) { }
    
    export default mt;
  3. In codegen.yml config use that file like schema.

Directive Schema

directive @constraint(
  pattern: String
  min: Int
  max: Int
  requiredMessage: String
  typeOf: String
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

Example

result.graphql

directive @constraint(
  pattern: String
  min: Int
  max: Int
  requiredMessage: String
  typeOf: String
) on INPUT_FIELD_DEFINITION | ARGUMENT_DEFINITION

input TestInput {
  something: String!
}

input RegisterAddressInput {
  postalCode: TestInput! @constraint(requiredMessage: "It field have custom message.")
  state: [String]!
  city: String!
  someNumber: Int @constraint(min: 10, max: 20)
  someNumberFloat: Float @constraint(min: 10.50, max: 20.50)
  someBoolean: Boolean
  line2: String @constraint(min: 10, max: 20)
}

codegen.yml

overwrite: true
schema: "./result.graphql"
generates:
  schemas.ts:
    plugins:
      - codegen-graphql-yup:
          defaultRequiredMessage: "This field have generic message"
          onlyWithConstrain: false

schemas.ts ( THE RESULT )

import * as yup from 'yup'

export const TestInputSchema = yup.object().shape({
    something: yup.string().required('This field have generic message')
});

export const RegisterAddressInputSchema = yup.object().shape({
    postalCode: TestInputSchema.required('It field have custom message.'),
    state: yup.arrayOf(yup.string()).required('This field have generic message'),
    city: yup.string().required('This field have generic message'),
    someNumber: yup.number().min(10).max(20),
    someNumberFloat: yup.number().min(10.5).max(20.5),
    someBoolean: yup.boolean(),
    line2: yup.string().min(10).max(20)
})

COLABORATE

Getting Started

These instructions will get you a copy of the project up and running on your local machine for development and testing purposes.

Prerequisites

You will need to install:

Node
npm
type script

You will need to have:

an schema from graphql. or use the default.

Installing

The steps to use this pluggin are:

Clone the project
npm install
npm run generate

Author

  • Matias Martinez
  • BeFish
  • Nemac