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

dgraph-lambda

v1.2.0

Published

Serverless Framework for Dgraph

Downloads

12

Readme

Dgraph Lambda

Dgraph Lambda is a serverless platform for running JS on Slash GraphQL (or Dgraph).

Running a script

A script looks something like this. There are two ways to add a resolver

  • addGraphQLResolver which recieves { parent, args } and returns a single value
  • addMultiParentGraphQLResolver which received { parents, args } and should return an array of results, each result matching to one parent. This method will have much better performance if you are able to club multiple requests together

If the query is a root query/mutation, parents will be set to [null].

const fullName = ({ parent: { firstName, lastName } }) => `${firstName} ${lastName}`

async function todoTitles({ graphql }) {
  const results = await graphql('{ queryTodo { title } }')
  return results.data.queryTodo.map(t => t.title)
}

self.addGraphQLResolvers({
  "User.fullName": fullName,
  "Query.todoTitles": todoTitles,
})

async function reallyComplexDql({parents, dql}) {
  const ids = parents.map(p => p.id);
  const someComplexResults = await dql(`really-complex-query-here with ${ids}`);
  return parents.map(parent => someComplexResults[parent.id])
}

self.addMultiParentGraphQLResolvers({
  "User.reallyComplexProperty": reallyComplexDql
})

Running Locally

First launch dgraph and load it with the todo schema (and add a todo or two).

type User {
   id: ID!
   firstName: String!
   lastName: String!
   fullName: String @lambda
}

type Todo {
   id: ID!
   title: String
}

type Query {
  todoTitles: [String] @lambda
}
# host.docker.internal may not work on old versions of docker
docker run -it --rm -p 8686:8686 -v /path/to/script.js:/app/script.js -e DGRAPH_URL=http://host.docker.internal:8080 dgraph/dgraph-lambda

Note for linux: host.docker.internal doesn't work on older versions of docker on linux. You can use DGRAPH_URL=http://172.17.0.1:8080 instead

Then test it out with the following curls

curl localhost:8686/graphql-worker -H "Content-Type: application/json" -d '{"resolver":"User.fullName","parents":[{"firstName":"Dgraph","lastName":"Labs"}]}'

Environment

We are trying to make the environment match the environment you'd get from ServiceWorker.

  • [x] fetch
  • [x] graphql / dql
  • [x] base64
  • [x] URL
  • [ ] crypto - should test this

Adding libraries

If you would like to add libraries, then use webpack --target=webworker to compile your script. We'll fill out these instructions later.

Working with Typescript

You can import @slash-graph/lambda-types to get types for addGraphQLResolver and addGraphQLMultiParentResolver.

Security

Currently, this uses node context to try and make sure that users aren't up to any fishy business. However, contexts aren't true security, and we should eventually switch to isolates. In the meanwhile, we will basically have kube kill this if it takes a lot of CPU for say 5 secs

Publishing

Currently, the publishing of this isn't automated. In order to publish:

  • Publish the types in slash-graphql-lambda-types if needed with (npm version minor; npm publish)
  • The docker-image auto publishes, but pushing a tag will create a tagged version that is more stable