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

@simpleview/sv-graphql-client

v4.0.0

Published

Client for communicating with sv-graphql

Downloads

846

Readme

sv-graphql-client

Client and tools for communicating with sv-graphql.

installation

npm install @simpleview/sv-graphql-client

Package API

GraphServer

GraphServer is an for communicating with the sv-graphql system. It helps eliminate some of the bloat of individual graphQL calls and makes it a little easier to work with.

  • args
    • graphUrl - string - Fully qualified URL pointing to the graphURL server.
    • context - object - Context object used for handling token/acct_id
      • acct_id - string - The acct_id that the user is attempting to access. acct_id is required for any endpoints on admin.
      • token - string | async () => string - The token returned from the auth system. Token is required for accessing any of the non-login mechanics.
    • prefixes - array of class - Prefixes which encasulate the behavior of the graphQL apis.

In most cases you will be setting the context at runtime. If so, manually update the context via setting graphServer.context.acct_id = "x".

Available prefixes, see the following packages to install the prefixes.

const { GraphServer } = require("@simpleview/sv-graphql-client");
const graphServer = new GraphServer({
	graphUrl : GRAPH_URL,
	prefixes : [PrefixOne, PrefixTwo]
});

The endpoints on the graphServer prefix should, in general, match 1-to-1 with the arguments and syntax of the GraphQL schema browser.

TestServer

TestServer is a tool for spinning up a simple GraphQL server that listens on a port and loads some graphql files via schemaLoader for it's operations. It's usually used to simplify unit tests.

See TestServer for arguments.

const testServer = new TestServer({
	port: 8080,
	paths: [
		"test/graphqlTest"
	]
});

// start the server, listen on the port
await testServer.boot();

// ... run tests ... //

// close the server
await testServer.close();

nullToUndefined

This function will take a graphQL response and convert null values to undefined. It can be helpful for trimming away fields that weren't returned from graph.

This does an by reference modification of the object. It does not return a clone.

  • obj - object - The object that you wish to clean.
const { nullToUndefined } = require("@simpleview/sv-graphql-client");
const result = await someGraphCall();
nullToUndefined(result);

query

Wrapper function to make it easier to talk to sv-graphql directly.

  • args
    • query - string - The graphQL query string. Usually best passed with JS template tag syntax.
    • variables - obj - If you're query utilizes variables, pass them on this object.
    • url - string - The URL of the graphQL endpoint.
    • token - string | async () => string - The token which will be passed on the Authorization header as a Bearer token. If using a function it will be called to return the token.
    • headers - obj - An object of headers to append to the request
    • key - string - Whether to reach and return a specific sub-key of the return.
    • clean - boolean - Whether to automatically run nullToUndefined on the result set to clean it.
const { query } = require("@simpleview/sv-graphql-client");
const result = await query({
	query : `
		query($token: String) {
			auth {
				current
			}
		}
	`,
	variables : {
		token : "my fake token"
	},
	url : "https://graphql.simpleviewinc.com/"
});

isPlainObject

Simple function for testing if a function is a plain JS object.

schemaLoader

schemaLoader takes all of the files present in paths or loaders and merges them into a single schema. This allows you to make your larger schema more maintainable by splitting it up into logical units. An example of a modular schema is outlined in Modularizing Your GraphQL Schema.

  • args
    • paths - string[] - Folders containing graphql schema files which export a GraphModule.
    • loaders - function[] - Loaders that will return a GraphModule. Use this when you need to load graph definitions that are dynamically generated.

A GraphModule is an object of { typeDefs, resolvers, schemaTransformers }, all keys are optional.

Example GraphModule:

const { gql } = require("apollo-server");

const typeDefs = gql`
	extend type Query {
		test_path2: Boolean
	}
`;

const resolvers = {
	Query: {
		test_path2: function() {
			return true;
		}
	}
}

module.exports = {
	typeDefs,
	resolvers
}
const { schemaLoader } = require("@simpleview/sv-graphql-client");

const schema = await schemaLoader({
	paths : ['/app/lib/graphql']
});

const server = new ApolloServer({
  schema,
  ...
});

Publishing

sudo npm run docker yarn run build exit sudo npm run publish VERSION