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

graphql-obj2arg

v0.1.0

Published

Convert a JavaScript object to a GraphQL arguments string

Downloads

565

Readme

graphql-obj2arg

Converts a JavaScript Object to a GraphQL arguments string


GraphQL is great, but it can sometimes be a pain to programatically create arguments for a query. This library can help create an argument string from a JavaScript object.

Example (ES6)

import graphql from 'graphql'
import obj2arg from 'graphql-obj2arg'
import PeopleSchema from './PeopleSchema'

let args = {
  id: 'jd8675309',
  name: 'John',
  age: 20,
  gpa: 3.9,
  role: 'Enum::STUDENT',
  courses: [
    {
      id: 101,
      name: 'CS101'
    },
    {
      id: 203,
      name: 'CS144'
    }
  ]
}

let query = `mutation updateStudentData {
  updateStudent(${obj2arg(args, { noOuterBraces: true })}) {
    id
  }
}`

graphql(PeopleSchema, query).then((result) => {
  ...
})

API

obj2arg ( arguments, [ options ] )

Converts an object into a GraphQL arguments string

Parameters:

  • arguments { Object } - Object containing arguments to convert
  • [ options ] { Object } - Options hash
    • [ keepNulls=false ] { Boolean } - Allows nulls to be included in the argument string if true
    • [ noOuterBraces=false ] { Boolean } - Removes the outer {} or [] from the argument string

Returns: String - Argument string

obj2arg.Enum ( value )

Creates an Enum object. When part of an arguments object the Enum will not be enclosed in double quotes

Parameters:

  • value { String } - Enum name

Returns: Enum - Enum object

Declaring Types

Types can be declared in the argument object using declaration notation <Declaration><Value>. This is useful for data types that either do not exist in JavaScript (i.e. Enum), can be tricky to determine (i.e. Floats), or simply to ensure a type is parsed a certain way.

Declarations

  • Boolean - Boolean::
  • Date - Date::
  • Enum - Enum::
  • Float - Float::
  • Int - Int::

Example

let args = {
  bool: 'Boolean::true',
  date: 'Date::2100-01-01',
  enum: 'Enum::MYENUM',
  float: 'Float::1.2',
  int: 'Int::100'
}