@simpleview/sv-graphql-client

4.0.0 • Public • Published

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

Readme

Keywords

none

Package Sidebar

Install

npm i @simpleview/sv-graphql-client

Weekly Downloads

297

Version

4.0.0

License

MIT

Unpacked Size

102 kB

Total Files

61

Last publish

Collaborators

  • georgeyb-sv
  • sloutrel
  • dlford
  • korychinn_sv
  • sv-mikael
  • actionawesome
  • arkmuntasser
  • leandrodalmassoglb
  • kristian.gonzalez
  • davidlpons
  • mauriciodeleonc
  • mpcarolin
  • csarantidessv
  • mikesan789
  • owenallenaz
  • qman33
  • svjoshua
  • mkes99
  • colin.booen
  • ssmith_sv
  • aglazebrook
  • rmaynes