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

snap-templates

v1.0.2

Published

Easily integrate the following services with GraphQL, without having to write resolvers:

Downloads

3

Readme

Snap

Easily integrate the following services with GraphQL, without having to write resolvers:

  • DynamoDB
  • AWS Lambda
  • JSON
  • MongoDB

And easily add more.

Still in a proof-of-concept stage, but very usable.

Getting Started

  • npm i --save snap-templates
  • Write a mapping configuration, see Mapping Templates below
  • Change the root value of your GraphQL schema to use the buildResolver() method:
rootValue: buildResolver(mapping, [/* add resolvers here */]),

DynamoDB Example

const app = express()
const ddb = new DynamoDB()

const schema = buildSchema(`
  type Song {
    id: String
    SpotifyURL: String
    Genre: String
  }
  type Query {
    song(id: Int): Song
  }
`)

const mappingTemplate: MappingConfiguration = {
  song: {
    kind: "DynamoDB",
    operation: "GetItem",
    query: {
      TableName: "ambliss-songs",
      Key: {
        id: {
          S: "$context.arguments.id"
        }
      }
    }
  }
}

app.use(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    rootValue: buildResolver(mapping, [DynamoResolver(ddb)]),
    graphiql: true
  })
)

JSON Example

const schema = buildSchema(`
  type Bear {
    name: String
    breed: String
  }
  type Query {
    bear(name: String): Bear
  }
`)

const bears = [
  {
    name: "Carl",
    breed: "black bear"
  },
  {
    name: "Steve",
    breed: "polar bear"
  }
]

const mappingTemplate: MappingConfiguration = {
  bear: {
    kind: "JSON",
    query: {
      name: "$context.arguments.name"
    }
  }
}

app.use(
  "/graphql",
  graphqlHTTP({
    schema: schema,
    rootValue: buildResolver(mapping, [JSONResolver(bears)]),
    graphiql: true
  })
)

Roadmap

Immediate features:

  • Response mapping templates
  • Client identity access within context
  • Mutation support

If you want to add support for a kind of template that isn't shown here, you can create a new resolver type by following the format provided in src/resolvers/JSON.ts. I'd like to provide resolver support for any client / service a developer would want to interface with, so if there's a service you'd like to see, open an Issue.

Some clients I'm interested in seeing:

  • MongoDB
  • S3
  • MySQL
  • IPFS

Long term features:

  • CircleCI for this repo
  • Graphical playground for writing mapping templates
  • A better solution for remotely loading and storing schemas, such as DynamoDB
  • Simple server that can be set up without needing to write JavaScript; add a GUI for writing templates and setting up auth (a la AppSync)

API Overview

buildResolver(mappingTemplate: MappingConfiguration, clients: ClientMapping): ResolverMapping

This is the main way you implement Snap. It accepts an object whose keys are the mappings from your data sources to a GraphQL query. The function returns a mapping of GraphQL resolvers, that can be consumed as the rootValue of GraphQL Express.

parseParams( resolverMappingParams: ResolverMappingTemplate, graphQLQueryParams: GraphQLParams ): ResolverMappingTemplate

This parses a query and replaces context and other arguments with the correct values. Usually called within a resolver.

Mapping Templates

A mapping template defines how Snap accepts parameters for a request coming from GraphQL, and transforms those to requests it makes to resolver clients, which make queries against their respective clients.

For example, a mapping template looks like this:

songByGenre: {
  kind: "DynamoDB",
  operation: "Scan",
  query: {
    TableName: "ambliss-songs",
    FilterExpression: "genre = :genre",
    ExpressionAttributeValues: {
      ":genre": {
        S: "$context.arguments.genre"
      }
    }
  }
}

which takes the genre argument from the GraphQL query, and inserts the value of genre in the ExpressionAttributeValues value of the query that is made to DynamoDB. You can do this for any field in the mapping template- even the operation being performed.

Template Types

type DynamoQueryTemplate = {
  kind: "DynamoDB"
  operation: "GetItem" | "Query"
  query: DynamoDB.Types.GetItemInput | DynamoDB.Types.QueryInput
}
type LambdaTemplate = {
  kind: "Lambda"
  FunctionName: string
}
type JSONMappingTemplate = {
  kind: "JSON"
  query: object
}