npm.io
0.1.4 • Published 1 year ago

@baeta/cloudflare-subscriptions

Licence
MIT
Version
0.1.4
Deps
1
Size
61 kB
Vulns
0
Weekly
0
Stars
13
DeprecatedThis package is deprecated

Baeta Logo

Baeta

nycrc config on GitHub

Getting Started   •   Website   •   Docs   •   Examples   •   Discord

What is Baeta?

Building GraphQL APIs shouldn't be complicated. Baeta is a modern, modular, open-source GraphQL framework designed with flexibility in mind. It follows a granular approach where you only add what you need, helping developers focus on what matters most - creating powerful, scalable APIs without the boilerplate.

Key Features

  • Modular Architecture: Organize your API into manageable modules
  • Schema-First Development: Define your API contract upfront
  • Type Safety: Automatic code generation for type-safe development
  • Middleware & Directives: Easy integration of custom behaviors
  • High Performance: Built for scalability and efficiency

And optional app plugins and libraries

  • @baeta/auth: Add powerful scope-based authorization
  • @baeta/complexity: Reject resource-exhausting queries with depth, breadth and complexity limits
  • @baeta/cache: Type-safe caching with declarative queries and automatic reconciliation
  • ... and more!

Why use Baeta?

Baeta makes it easy to build better GraphQL APIs while staying flexible. Here's how:

Granular and Progressive: Start small and add features as you need them. Whether you're building a simple API or a complex system, Baeta scales with your needs.

Modular architecture: Baeta's modular design allows you to organize your GraphQL API into smaller, more manageable modules that can be added or removed as needed. This makes it easier to maintain and scale your API over time.

Schema-first approach: With Baeta, you define your schema first, and then logic and resolvers. This approach ensures a consistent and well-defined API for your clients and reduces boilerplate code.

How it Works

1. Define your schema
type User {
  id: ID!
  name: String!
  email: String!
  age: Int
}

input UserWhereUnique {
  id: ID
  email: String
}

type Query {
  user(where: UserWhereUnique!): User!
  users: [User!]!
}
2. Implement your resolvers
import { UserModule } from "./typedef.ts";

const { Query } = UserModule;

const userQuery = Query.user.resolve(({ args }) => {
  return dataSource.user.find(args.where);
});

const usersQuery = Query.users.resolve(() => {
  return dataSource.user.findMany();
});

export default Query.$fields({
  user: userQuery,
  users: usersQuery,
});
3. Add authorization
import { auth, rule, scope } from "./lib/auth.ts";
import { UserModule } from "./typedef.ts";

const { Query } = UserModule;

const userQuery = Query.user
  .$use(auth(rule.or(scope.isPublic, scope.isLoggedIn)))
  .resolve(async ({ args }) => {
    // ...
  });
4. Add caching
import { createCache, defineQuery } from "@baeta/cache";
import { redisClient } from "./lib/redis.ts";

const { Query, Mutation } = UserModule;

export const userCache = createCache(redisClient, {
  name: "UserCache",
  parse: JSON.parse,
  serialize: JSON.stringify,
})
  .withQueries({
    findUser: defineQuery({
      resolve: async (args: { id: string }) => {
        return dataSource.user.findUnique({ where: args });
      },
    }),
  })
  .build();

const userQuery = Query.user.map(({ args }) =>
  userCache.queries.findUser({ id: args.where.id }),
);

const updateUserMutation = Mutation.updateUser
  .$use(async (next) => {
    const user = await next();
    if (user) await userCache.update(user);
    return user;
  })
  .resolve(async ({ args }) => {
    // ...
  });

Compatibility

Baeta is compatible with all GraphQL servers, which makes it easy to integrate with your existing stack. It works seamlessly with popular GraphQL server libraries such as Graphql Yoga and Apollo Server, as well as other popular tools like Prisma, Drizzle and Kysely.

Baeta's development tools are built for Node.js, but the runtime code is environment-agnostic. This means your Baeta applications can run anywhere JavaScript runs, including:

  • Deno
  • Cloudflare Workers
  • AWS Lambda
  • Vercel Edge Functions
  • Bun
  • Node.js
  • Any other JavaScript runtime

Credits

Baeta was inspired by several amazing projects and people in the GraphQL ecosystem. Check out our Credits page to learn more about the individuals and projects that influenced Baeta's development.

License

Baeta is licensed under the MIT License.