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

@yoonit/utils

v1.2.2

Published

Couple of functions in JS to speed up development and give some help

Downloads

2

Readme

Couple of functions in JS to speed up development and give some help with:

  • A GraphQl Query Builder
  • A GraphQl Mutation Builder

Table Of Contents

Installation

npm i -s @yoonit/utils

Usage

We used a functional technique called currying at the main constructor function, this means that we pass a subset of arguments and each function returns a function that uses the next subset of arguments.

The first parameters is a string and its the the endpoint name (such as 'getUsers'), it returns the function that uses the next parameter, the arguments object. This function will build the arguments body for your query/mutation and will return another function. This third function uses the third parameter you need to pass, the expected response fields. This will build the response filds and will return you query/mutation ready to use. You can use the builders without a arguments object, but you need to pass the endpoint name and the response fields, otherwise it will return false and a warning.

Parameters:

  • Endpoint Name e.g., 'getUsers'
  • Object parameters e.g., '{ value: 'value' }'
  • Expected response fields e.g, 'status', 'message'

Query Builder

import { graphql } from '@yoonit/utils'

const query = graphql.query('getUsers')({ value: 'value', valueTwo: 123 })('status', 'message', 'messageTwo')

Output

query {
  getUsers (
    value: "value",
    valueTwo: 123,
  ){
    status,
    message,
    messageTwo
  }
}

Mutation Builder

import { graphql } from '@yoonit/utils'

const mutation = graphql.mutation('createUser')(name: 'Mutation', surname: 'Builder')('status', 'message')

Output

mutation {
  createUser (
    name: "Mutation",
    surname: "Builder"
  ){
    status,
    message
  }
}

Advanced Usage

Our builders also suports nested response fields and arrays on arguments object, see beelow how to use it:

import { graphql } from '@yoonit/utils'

const mutation = graphql.mutation('createUser')({ value: ['value', '123', 'John Doe']})('status', 'message', { 'messageTwo': ['messageTitle', 'messageBody', { 'messageAlt': 'test' }, { 'messageAtt': ['att1', 'att2'] }]})

Output

mutation {
  createUser (
    value: [
      'value',
      '123',
      'John Doe'
    ],
  ){
    status,
    message,
    messageTwo {
      messageTitle,
      messageBody,
      messageAlt {
        test
      },
      messageAtt {
        att1,
        att2
      }
    }
  }
}

Using it for real

You can use it with js fetch or any other HTTP client you like. See below how to use it with Fetch

import { graphql } from '@yoonit/utils'

const body = graphql.mutation('createUser')({value: ['value', '123', 'John Doe'] })('status', 'message', { 'messageTwo': ['messageTitle', 'messageBody', { 'messageAlt': 'test' }, { 'messageAtt': ['att1', 'att2'] }]})

fetch('http://yourapi:5000', {
  method: 'POST',
  headers: { 'Content-Type': 'application/json' },
  body // Use the builder response here, as your body :) 
  })
})

To contribute and make it better

Clone the repo, change what you want and send PR. For commit messages we use Conventional Commits.

Contributions are always welcome!


Code with ❤ by the Yoonit Team