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

salesforce-graphql-helper

v0.0.1

Published

Salesforce GraphQL API client for browsers and Node with functionality to dynamically, quickly and easily create Salesforce GraphQL queries.

Downloads

3

Readme

Salesforce GraphQL Helper

Salesforce GraphQL API client for browsers and Node with functionality to dynamically, quickly and easily create Salesforce GraphQL queries.

Installation

Use the Node package manager npm to install Salesforce GraphQL Helper.

npm install salesforce-graphql-helper

Usage

Required:

  • Set an environment variable called SALESFORCE_ACCESS_TOKEN that stores a valid Salesforce access token that will be used for authentication.
  • Set an environment variable called SALESFORCE_API_URL that stores your Salesforce API URL (e,g. https://MyDomainName.my.salesforce.com/services/data/v55.0).

GraphQlHelper Class:

Dynamically, quickly and easily create and invoke Salesforce GraphQL queries without worrying about syntax.

RootSObject:

Query field values from root object.

import { GraphQlHelper, RootSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper(
    new RootSObject("Account").addField("Id").addField("Name")
  );
  const response = await queryHelper.query();
})();

Query field values from multiple root objects.

import { GraphQlHelper, RootSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper();
  const account = new RootSObject("Account")
    .addField("Id")
    .addField("Name")
    .addFilter({ Name: { like: "Test%" } });
  queryHelper.addRootObject(account);
  queryHelper.addRootObject(
    new RootSObject("Contact").addField("Id").addField("Name")
  );
  const response = await queryHelper.query();
})();

ParentSObject:

Query field values from root object as well as field values from parent objects.

import { GraphQlHelper, RootSObject, ParentSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper(
    new RootSObject("Contact")
      .addField("Id")
      .addField("Name")
      .addParentSObject(new ParentSObject("Account").addField("Name"))
  );
  const response = await queryHelper.query();
})();

ChildSObject:

Query field values from root object as well as field values from child objects. Child relationships may only be requested as direct descendants of the root object type. You can't query fields from a child relationship pertaining to a parent of the root object.

import { GraphQlHelper, RootSObject, ChildSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper(
    new RootSObject("Account")
      .addField("Id")
      .addField("Name")
      .addChildSObject(new ChildSObject("Contacts").addField("Name"))
  );
  const response = await queryHelper.query();
})();

Filtering:

Set the where argument and filter type value (see the Filtering section of the GraphQL docs).

import { GraphQlHelper, RootSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper(
    new RootSObject("Account")
      .addField("Id")
      .addField("Name")
      .addFilter({ Name: { like: "Test%" } })
  );
  const response = await queryHelper.query();
})();

Ordering:

Set the orderBy argument and OrderBy type value (see the Ordering section of the GraphQL docs).

import { GraphQlHelper, RootSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper(
    new RootSObject("Account")
      .addField("Id")
      .addField("Name")
      .setOrder("Name", "ASC")
  );
  const response = await queryHelper.query();
})();

Putting it all together:

Example portraying a combination GraphQlHelper functionality.

import { GraphQlHelper, RootSObject, ParentSObject, ChildSObject } from "salesforce-graphql-helper";

(async function start() {
  const queryHelper = new GraphQlHelper(
    new RootSObject("Account")
      .addFields(["Id", "Name"])
      .addFilter({ Name: { like: "Test%" } })
      .setOrder("Name", "ASC")
      .addParentSObject(new ParentSObject("Parent").addField("Name"))
      .addChildSObject(
        new ChildSObject("Contacts")
          .addFields(["Id", "Name"])
          .addParentSObject(new ParentSObject("CreatedBy").addField("Name"))
      )
  );
  const response = await queryHelper.query();
})();

Query Function:

Invoke a Salesforce GraphQL query as a string and optionally pass filters as a variable.

import { query } from "salesforce-graphql-helper";

(async function start() {
  const queryString = `
    query accounts {
      uiapi {
        query {
          Account {
            edges {
              node {
                Id
                Name {
                  value
                }
              }
            }
          }
        }
      }
    }
  `;

  const response = await query(queryString);
})();
import { query } from "salesforce-graphql-helper";
(async function start() {
  const queryString = `
    query accountsWithFilter($where: Account_Filter) {
      uiapi {
        query {
          Account(where: $where) {
            edges {
              node {
                Id
                Name {
                  value
                }
              }
            }
          }
        }
      }
    }
  `;
  const accountNameFilter = { Name: { like: "Test%" } };
  const response = await query(queryString, accountNameFilter);
})();

To do

  • Add auto-pagination functionality

License

This project is licensed under the GNU General Public License v3.0 - see the LICENSE.md file for details.

Supporting Docs