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

lambda2graphqljs

v0.0.2-beta

Published

js版本的lambda to Graphql

Downloads

2

Readme

lambda2Graphql

js版本的Linq to Graphql

如何使用


   npm i  lambda2graphqljs

import { extendFunction, gql, ILinqDb, Lambda } from 'lambda2graphqljs'

export class User {
   public Id: string
   public Name: string
   public Mobile: string
}

// tslint:disable-next-line: max-classes-per-file
export class Db {
   public Users: User[]
}
// 当作函数使用
const code = gql((a) =>  a.Users.map(({ Id, Mobile, Name}) => ({ Mobile, Name, Id})))
// tslint:disable-next-line:no-console
console.log(code)
/*
 {
     Users  {

       Mobile,
       Name,
       Id

     }
   }
*/

// 封装成对象使用
const lambda1 = new Lambda<Db>(
  ((a) =>  a.Users.filter((a) => a.Mobile === '1232323').map(({ Id, Mobile, Name}) => ({ Mobile}))),
  )
// tslint:disable-next-line:no-console
console.log(lambda1.gql())
/*
 {
     Users (Mobile = 1232323) {

       Mobile

     }
   }
*/



// 扩展方法使用
extendFunction()
const lambda: ILinqDb<Db> = (a) =>  a.Users.map(({ Id, Mobile, Name}) => ({ Mobile, Name}))
// tslint:disable-next-line:no-console
console.log(lambda.gql())
/*
{
     Users  {

       Mobile,
       Name

     }
   }
*/

设计思路

灵感来源于 C# 的 Linq to SQL


{
  user(Id: 4) {
    id
    name
    profilePic(size: 100)
  }
}

The above GraphQL query could be translated to:

db.Users
  .Where(u => u.Id == 4)
  .Select(u => new
  {
      id = u.Id,
      name = u.Name,
      profilePic = db.ProfilePics
                     .FirstOrDefault(p => p.UserId == u.Id && p.Size == 100)
                     .Url
  })
  .FirstOrDefault();
  

这个接口返回不是立即执行的,而是一个表达式,最终会转化为sql语句给数据库 也就是说上面的c# 表达式跟sql语句是互相等价的,

如果这个换成js版本的就是:

db.Users
.filter(u => u.Id == 4)
.map(u => {
  return
  {
    id : u.Id,
    name : u.Name,
    profilePic : db.ProfilePics
                   .find(p => p.UserId == u.Id && p.Size == 100)
                   .Url
  }
 })
.find(()=> true);

然后把sql 语句换成graphql字符

那能否把 js 函数语句 变回 graphql字符串呢?

首先,高阶函数包装


 const linqExpress1 = () => {return db.Users.filter(u => u.Id === 4).map(a => { return { id:a.id } }) }

然后,通过 Function.toString() 获取 Native Code 分析表达式源码,转成真正的 graphql 语句


linqExpress1.toString()

// "() => {  return db.Users.filter(u => u.Id === 4).map(a => { return { id:a.id } }) } "

AST代码转换

  "() => {  return db.Users.filter(u => u.Id === 4).map(a => { return { id:a.id } })}"

换成


   {
  user(Id: 4) {
    id
  }
}

好处

  1. 受限的Lambda 的语句可读性强,更重要的是支持ts 泛型 静态类型检测
  2. 支持map 默认所有字段,不需要显示声明字段