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

@awsless/graphql

v0.0.9

Published

[![npm version](https://img.shields.io/npm/dw/@awsless/graphql)](https://www.npmjs.org/package/@awsless/graphql) [![npm version](https://img.shields.io/npm/v/@awsless/graphql.svg?style=flat-square)](https://www.npmjs.org/package/@awsless/graphql) [![licen

Downloads

119

Readme

npm version npm version license

Type safe GraphQL query builder

The @awsless/graphql package provides a fully typed graphql client. We generate the types needed for the client to be fully typesafe while still being super lightweight to use. Compared to other solutions we don't generate big config files that will increase your bundle size.

Features

  • Fully type safe
  • Type completion
  • Tree shakable API
  • Super lightweight
  • GQL Unions & Interfaces
  • GQL Aliases
  • Works in node and the browser

For features like batching or special authentication you can easily add your own fetcher that supports that.

Setup

Install with (NPM):

npm i @awsless/graphql

Basic Usage

First generate the types needed for the client.

import { generate } from '@awsless/graphql';
import { buildSchema } from 'graphql';
import { readFileSync, writeFileSync } from 'fs'
import { join } from 'path'

const string = readFileSync(join(__dirname, 'schema.gql'), 'utf8')
const schema = buildSchema(string)
const output = generate(schema, {
	scalarTypes: {
		AWSDateTime: 'string'
	}
})

writeFileSync(join(__dirname, 'generated.ts'), output)

Then you can use your client as follows:

import { createClient, createFetcher } from '@awsless/graphql';
import type { Schema } from './generated.ts';

const client = createClient<Schema>(
	createFetcher({
		url: 'https://example.com/graphql'
	})
)

client.query({
	countries: {
		name: true,
		code: true
	}
})

The code above will result in the graphql query below.

query {
  countries {
    name
    code
  }
}

Query name

You can set the query name by adding the __name property.

client.query({
	__name: 'GetCountries',
	countries: {
		name: true,
	}
})
query GetCountries {
  countries {
    name
  }
}

Passing arguments

Arguments can be passed via the __args property.

client.mutate({
	addTodo: {
		__args: {
			// Pass an argument directly in the query.
			title: 'Pick up groceries',

			// Pass an argument as dynamic variable.
			done: $('Boolean', true)
		},
		id: true
	}
})
mutation ($v1: Boolean) {
  addTodo(title: "Pick up groceries", done: $v1) {
    id
  }
}

Unions and interfaces

To query some fields only on a certain type you can use graphql unions.

You can express the union and interface types using the "...on" prefixed fields in a query.

const { account } = await client.query({
	account: {
		__typename: true,
		id: true
		'...on User': {
			name: true
		}
		'...on Guest': {
			surname: true
		}
	}
})

// The ID prop will be available without type guards
console.log(account.id)

// You can check the value of __typename to restrict an union type to one of its sub types and let typescript infer its correct fields
if (account.__typename === 'User') {
	// Now account has type User
	console.log(account.name)
}
query {
  account {
	__typename
	id
	...on User {
		name
	}
	...on Guest {
		surname
	}
  }
}

Aliases

const { account } = await client.query({
	account: {
		'other:name': true
	}
})

// The "name" property is now available via "other"
console.log(account.other)
query {
  account {
    other: name
  }
}

Reuseable fragments

Reuseable fragments can be defined with simple JS objects.

const getUserFragment = (id:string) => ({
	__args: { id },
	id: true,
	name: true
 })

const { account } = await client.query({
	'one:getUser': getUserFragment('1'),
	'two:getUser': getUserFragment('2'),
})
query {
	one: getUser(id: "1") {
		id
		name
	}
	two: getUser(id: "2") {
		id
		name
	}
}