@baeta/federation 0.0.0 → 2.0.0-next.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ # @baeta/federation
2
+
3
+ ## 2.0.0-next.13
4
+
5
+ ### Patch Changes
6
+
7
+ - [#389](https://github.com/andreisergiu98/baeta/pull/389) [`3e7a4d7`](https://github.com/andreisergiu98/baeta/commit/3e7a4d71a59543b8a506938f788aec8b5d907776) Thanks [@andreisergiu98](https://github.com/andreisergiu98)! - Add support for Apollo Federation
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Pampu Andrei
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,164 @@
1
+ <p align="center">
2
+ <img src="https://raw.githubusercontent.com/andreisergiu98/baeta/refs/heads/main/website/static/img/logo-baeta.svg" alt="Baeta Logo" width="150"/>
3
+ </p>
4
+
5
+ <div align="center">
6
+ <h1>Baeta</h1>
7
+ <a href="https://www.npmjs.com/package/@baeta/cli"><img src="https://img.shields.io/npm/v/@baeta/cli.svg?style=flat" /></a>
8
+ <a href="https://github.com/andreisergiu98/baeta/actions/workflows/checks.yml"><img src="https://img.shields.io/github/actions/workflow/status/andreisergiu98/baeta/checks.yml" /></a>
9
+ <a href="https://github.com/andreisergiu98/baeta/pulls"><img src="https://img.shields.io/badge/PRs-welcome-brightgreen.svg" /></a>
10
+ <a href="https://github.com/andreisergiu98/baeta/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-MIT-blue" /></a>
11
+ <br />
12
+ <br />
13
+ <a href="https://baeta.io/docs/getting-started/installation">Getting Started</a>
14
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
15
+ <a href="https://www.baeta.io/">Website</a>
16
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
17
+ <a href="https://baeta.io/docs/intro">Docs</a>
18
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
19
+ <a href="https://github.com/andreisergiu98/baeta/tree/main/examples">Examples</a>
20
+ <span>&nbsp;&nbsp;•&nbsp;&nbsp;</span>
21
+ <a href="https://discord.gg/BHFXHvyj">Discord</a>
22
+ <br />
23
+ <hr />
24
+ </div>
25
+
26
+ # What is Baeta?
27
+
28
+ 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.
29
+
30
+ ### Key Features
31
+
32
+ - **Modular Architecture**: Organize your API into manageable modules
33
+ - **Schema-First Development**: Define your API contract upfront
34
+ - **Type Safety**: Automatic code generation for type-safe development
35
+ - **Middleware & Directives**: Easy integration of custom behaviors
36
+ - **High Performance**: Built for scalability and efficiency
37
+
38
+ #### And optional extensions and plugins
39
+
40
+ - **@baeta/extension-auth**: Add powerful scope-based authorization
41
+ - **@baeta/extension-cache**: Implement automatic caching with simple update patterns
42
+ - ... and more!
43
+
44
+ ## Why use Baeta?
45
+
46
+ Baeta makes it easy to build better GraphQL APIs while staying flexible. Here's how:
47
+
48
+ **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.
49
+
50
+ **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.
51
+
52
+ **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.
53
+
54
+ ## How it Works
55
+
56
+ #### 1. Define your schema
57
+
58
+ ```graphql
59
+ type User {
60
+ id: ID!
61
+ name: String!
62
+ email: String!
63
+ age: Int
64
+ }
65
+
66
+ input UserWhereUnique {
67
+ id: ID
68
+ email: String
69
+ }
70
+
71
+ type Query {
72
+ user(where: UserWhereUnique!): User!
73
+ users: [User!]!
74
+ }
75
+ ```
76
+
77
+ #### 2. Implement your resolvers
78
+
79
+ ```typescript
80
+ import { UserModule } from "./typedef.ts";
81
+
82
+ const { Query } = UserModule;
83
+
84
+ const userQuery = Query.user.resolve(({ args }) => {
85
+ return dataSource.user.find(args.where);
86
+ });
87
+
88
+ const usersQuery = Query.users.resolve(() => {
89
+ return dataSource.user.findMany();
90
+ });
91
+
92
+ Query.$fields({
93
+ user: userQuery,
94
+ users: usersQuery,
95
+ });
96
+ ```
97
+
98
+ #### 3. Add authorization
99
+
100
+ ```typescript
101
+ import { UserModule } from "./typedef.ts";
102
+
103
+ const { Query } = UserModule;
104
+
105
+ const userQuery = Query.user
106
+ .$auth({
107
+ $or: {
108
+ isPublic: true,
109
+ isLoggedIn: true,
110
+ },
111
+ })
112
+ .resolve(async ({ args }) => {
113
+ // ...
114
+ });
115
+ ```
116
+
117
+ #### 4. Add caching
118
+
119
+ ```typescript
120
+ const { Query, Mutation, User } = UserModule;
121
+
122
+ export const userCache = User.$createCache();
123
+
124
+ const userQuery = Query.user
125
+ .$auth({
126
+ // ...
127
+ })
128
+ .$useCache(userCache)
129
+ .resolve(async ({ args }) => {
130
+ // ...
131
+ });
132
+
133
+ const updateUserMutation = Mutation.updateUser
134
+ .$use(async (next) => {
135
+ const user = await next();
136
+ await userCache.save(user);
137
+ return user;
138
+ })
139
+ .resolve(async ({ args }) => {
140
+ // ...
141
+ });
142
+ ```
143
+
144
+ ## Compatibility
145
+
146
+ 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**.
147
+
148
+ 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:
149
+
150
+ - Deno
151
+ - Cloudflare Workers
152
+ - AWS Lambda
153
+ - Vercel Edge Functions
154
+ - Bun
155
+ - Node.js
156
+ - Any other JavaScript runtime
157
+
158
+ ## Credits
159
+
160
+ Baeta was inspired by several amazing projects and people in the GraphQL ecosystem. Check out our [Credits page](https://baeta.io/docs/credits) to learn more about the individuals and projects that influenced Baeta's development.
161
+
162
+ ## License
163
+
164
+ Baeta is licensed under the [MIT License](./LICENSE).
@@ -0,0 +1,14 @@
1
+ import * as _$graphql from "graphql";
2
+
3
+ //#region lib/create-scalar.d.ts
4
+ declare function createFederationScalar<T extends 'string' | 'json'>(type: T, name: string, description?: string): _$graphql.GraphQLScalarType<any, unknown>;
5
+ //#endregion
6
+ //#region lib/resolve-entities.d.ts
7
+ type EntityRepresentation<T extends string, R extends Record<string, unknown>> = {
8
+ __typename: T;
9
+ } & R;
10
+ type EntityHandlerMap<T extends string, Ctx, Info> = Record<T, (representation: EntityRepresentation<T, any>, ctx: Ctx, info: Info) => any>;
11
+ declare function resolveEntities<T extends string, R extends Record<string, unknown>, Ctx, Info>(representations: Array<EntityRepresentation<T, R>>, entityHandlerMap: EntityHandlerMap<T, Ctx, Info>, ctx: Ctx, info: Info): Promise<any[]>;
12
+ //#endregion
13
+ export { createFederationScalar, resolveEntities };
14
+ //# sourceMappingURL=index.d.ts.map
package/dist/index.js ADDED
@@ -0,0 +1,81 @@
1
+ import { GraphQLError, GraphQLScalarType, Kind, print } from "graphql";
2
+ //#region lib/scalar-json.ts
3
+ /**
4
+ * Based on graphql-scalars JSON scalar implementation
5
+ * Sources:
6
+ * - https://github.com/graphql-hive/graphql-scalars/blob/master/src/scalars/json/JSON.ts
7
+ * - https://github.com/graphql-hive/graphql-scalars/blob/master/src/scalars/json/utils.ts
8
+ * Copyright (c) 2020-present The Guild
9
+ * Modified by Baeta developers
10
+ */
11
+ const specifiedByURL = "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf";
12
+ function createJSONScalar(name, description) {
13
+ return new GraphQLScalarType({
14
+ name,
15
+ description,
16
+ serialize: identity,
17
+ parseValue: identity,
18
+ parseLiteral,
19
+ specifiedByURL
20
+ });
21
+ }
22
+ function identity(value) {
23
+ return value;
24
+ }
25
+ function parseObject(ast, variables) {
26
+ if (ast.kind !== Kind.OBJECT) throw new GraphQLError(`JSONObject cannot represent non-object value: ${print(ast)}`, { nodes: ast });
27
+ return Object.fromEntries(ast.fields.map((field) => [field.name.value, parseLiteral(field.value, variables)]));
28
+ }
29
+ function parseLiteral(ast, variables) {
30
+ switch (ast.kind) {
31
+ case Kind.STRING:
32
+ case Kind.BOOLEAN: return ast.value;
33
+ case Kind.INT:
34
+ case Kind.FLOAT: return Number.parseFloat(ast.value);
35
+ case Kind.OBJECT: return parseObject(ast, variables);
36
+ case Kind.LIST: return ast.values.map((n) => parseLiteral(n, variables));
37
+ case Kind.NULL: return null;
38
+ case Kind.VARIABLE: {
39
+ const name = ast.name.value;
40
+ return variables ? variables[name] : void 0;
41
+ }
42
+ }
43
+ }
44
+ //#endregion
45
+ //#region lib/scalar-string.ts
46
+ function createStringScalar(name, description) {
47
+ return new GraphQLScalarType({
48
+ name,
49
+ description,
50
+ serialize: validateValue,
51
+ parseValue: validateValue,
52
+ parseLiteral: (ast) => {
53
+ if (ast.kind !== Kind.STRING) throw new GraphQLError(`Can only parse strings but got a: ${ast.kind}`, { nodes: ast });
54
+ return validateValue(ast.value, ast);
55
+ }
56
+ });
57
+ }
58
+ function validateValue(value, ast) {
59
+ if (typeof value !== "string") throw new GraphQLError(`Value is not a string: ${value}`, { nodes: ast });
60
+ return value;
61
+ }
62
+ //#endregion
63
+ //#region lib/create-scalar.ts
64
+ function createFederationScalar(type, name, description) {
65
+ if (type === "string") return createStringScalar(name, description);
66
+ if (type === "json") return createJSONScalar(name, description);
67
+ return type;
68
+ }
69
+ //#endregion
70
+ //#region lib/resolve-entities.ts
71
+ async function resolveEntities(representations, entityHandlerMap, ctx, info) {
72
+ const promises = representations.map(async (representation) => {
73
+ const handler = entityHandlerMap[representation.__typename];
74
+ return await handler(representation, ctx, info);
75
+ });
76
+ return await Promise.all(promises);
77
+ }
78
+ //#endregion
79
+ export { createFederationScalar, resolveEntities };
80
+
81
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","names":[],"sources":["../lib/scalar-json.ts","../lib/scalar-string.ts","../lib/create-scalar.ts","../lib/resolve-entities.ts"],"sourcesContent":["/**\n * Based on graphql-scalars JSON scalar implementation\n * Sources:\n * - https://github.com/graphql-hive/graphql-scalars/blob/master/src/scalars/json/JSON.ts\n * - https://github.com/graphql-hive/graphql-scalars/blob/master/src/scalars/json/utils.ts\n * Copyright (c) 2020-present The Guild\n * Modified by Baeta developers\n */\n\nimport { GraphQLError, GraphQLScalarType, Kind, print, type ValueNode } from 'graphql';\n\nconst specifiedByURL = 'http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf';\n\nexport function createJSONScalar(name: string, description?: string) {\n\treturn new GraphQLScalarType({\n\t\tname,\n\t\tdescription,\n\t\tserialize: identity,\n\t\tparseValue: identity,\n\t\tparseLiteral,\n\t\tspecifiedByURL,\n\t});\n}\n\nfunction identity<T>(value: T): T {\n\treturn value;\n}\n\nfunction parseObject(ast: ValueNode, variables: any): any {\n\tif (ast.kind !== Kind.OBJECT) {\n\t\tthrow new GraphQLError(`JSONObject cannot represent non-object value: ${print(ast)}`, {\n\t\t\tnodes: ast,\n\t\t});\n\t}\n\treturn Object.fromEntries(\n\t\tast.fields.map((field) => [field.name.value, parseLiteral(field.value, variables)]),\n\t);\n}\n\nfunction parseLiteral(ast: ValueNode, variables: any): any {\n\tswitch (ast.kind) {\n\t\tcase Kind.STRING:\n\t\tcase Kind.BOOLEAN:\n\t\t\treturn ast.value;\n\t\tcase Kind.INT:\n\t\tcase Kind.FLOAT:\n\t\t\treturn Number.parseFloat(ast.value);\n\t\tcase Kind.OBJECT:\n\t\t\treturn parseObject(ast, variables);\n\t\tcase Kind.LIST:\n\t\t\treturn ast.values.map((n) => parseLiteral(n, variables));\n\t\tcase Kind.NULL:\n\t\t\treturn null;\n\t\tcase Kind.VARIABLE: {\n\t\t\tconst name = ast.name.value;\n\t\t\treturn variables ? variables[name] : undefined;\n\t\t}\n\t}\n}\n","import { GraphQLError, GraphQLScalarType, Kind, type ValueNode } from 'graphql';\n\nexport function createStringScalar(name: string, description?: string) {\n\treturn new GraphQLScalarType<string, string>({\n\t\tname,\n\t\tdescription,\n\t\tserialize: validateValue,\n\t\tparseValue: validateValue,\n\t\tparseLiteral: (ast: ValueNode) => {\n\t\t\tif (ast.kind !== Kind.STRING) {\n\t\t\t\tthrow new GraphQLError(`Can only parse strings but got a: ${ast.kind}`, {\n\t\t\t\t\tnodes: ast,\n\t\t\t\t});\n\t\t\t}\n\t\t\treturn validateValue(ast.value, ast);\n\t\t},\n\t});\n}\n\nfunction validateValue(value: unknown, ast?: ValueNode): string {\n\tif (typeof value !== 'string') {\n\t\tthrow new GraphQLError(`Value is not a string: ${value}`, {\n\t\t\tnodes: ast,\n\t\t});\n\t}\n\treturn value;\n}\n","import { createJSONScalar } from './scalar-json.ts';\nimport { createStringScalar } from './scalar-string.ts';\n\nexport function createFederationScalar<T extends 'string' | 'json'>(\n\ttype: T,\n\tname: string,\n\tdescription?: string,\n) {\n\tif (type === 'string') {\n\t\treturn createStringScalar(name, description);\n\t}\n\tif (type === 'json') {\n\t\treturn createJSONScalar(name, description);\n\t}\n\treturn type satisfies never;\n}\n","type EntityRepresentation<T extends string, R extends Record<string, unknown>> = {\n\t__typename: T;\n} & R;\n\ntype EntityHandlerMap<T extends string, Ctx, Info> = Record<\n\tT,\n\t(representation: EntityRepresentation<T, any>, ctx: Ctx, info: Info) => any\n>;\n\nexport async function resolveEntities<\n\tT extends string,\n\tR extends Record<string, unknown>,\n\tCtx,\n\tInfo,\n>(\n\trepresentations: Array<EntityRepresentation<T, R>>,\n\tentityHandlerMap: EntityHandlerMap<T, Ctx, Info>,\n\tctx: Ctx,\n\tinfo: Info,\n) {\n\tconst promises = representations.map(async (representation) => {\n\t\tconst handler = entityHandlerMap[representation.__typename];\n\t\treturn await handler(representation, ctx, info);\n\t});\n\treturn await Promise.all(promises);\n}\n"],"mappings":";;;;;;;;;;AAWA,MAAM,iBAAiB;AAEvB,SAAgB,iBAAiB,MAAc,aAAsB;AACpE,QAAO,IAAI,kBAAkB;EAC5B;EACA;EACA,WAAW;EACX,YAAY;EACZ;EACA;EACA,CAAC;;AAGH,SAAS,SAAY,OAAa;AACjC,QAAO;;AAGR,SAAS,YAAY,KAAgB,WAAqB;AACzD,KAAI,IAAI,SAAS,KAAK,OACrB,OAAM,IAAI,aAAa,iDAAiD,MAAM,IAAI,IAAI,EACrF,OAAO,KACP,CAAC;AAEH,QAAO,OAAO,YACb,IAAI,OAAO,KAAK,UAAU,CAAC,MAAM,KAAK,OAAO,aAAa,MAAM,OAAO,UAAU,CAAC,CAAC,CACnF;;AAGF,SAAS,aAAa,KAAgB,WAAqB;AAC1D,SAAQ,IAAI,MAAZ;EACC,KAAK,KAAK;EACV,KAAK,KAAK,QACT,QAAO,IAAI;EACZ,KAAK,KAAK;EACV,KAAK,KAAK,MACT,QAAO,OAAO,WAAW,IAAI,MAAM;EACpC,KAAK,KAAK,OACT,QAAO,YAAY,KAAK,UAAU;EACnC,KAAK,KAAK,KACT,QAAO,IAAI,OAAO,KAAK,MAAM,aAAa,GAAG,UAAU,CAAC;EACzD,KAAK,KAAK,KACT,QAAO;EACR,KAAK,KAAK,UAAU;GACnB,MAAM,OAAO,IAAI,KAAK;AACtB,UAAO,YAAY,UAAU,QAAQ,KAAA;;;;;;ACrDxC,SAAgB,mBAAmB,MAAc,aAAsB;AACtE,QAAO,IAAI,kBAAkC;EAC5C;EACA;EACA,WAAW;EACX,YAAY;EACZ,eAAe,QAAmB;AACjC,OAAI,IAAI,SAAS,KAAK,OACrB,OAAM,IAAI,aAAa,qCAAqC,IAAI,QAAQ,EACvE,OAAO,KACP,CAAC;AAEH,UAAO,cAAc,IAAI,OAAO,IAAI;;EAErC,CAAC;;AAGH,SAAS,cAAc,OAAgB,KAAyB;AAC/D,KAAI,OAAO,UAAU,SACpB,OAAM,IAAI,aAAa,0BAA0B,SAAS,EACzD,OAAO,KACP,CAAC;AAEH,QAAO;;;;ACtBR,SAAgB,uBACf,MACA,MACA,aACC;AACD,KAAI,SAAS,SACZ,QAAO,mBAAmB,MAAM,YAAY;AAE7C,KAAI,SAAS,OACZ,QAAO,iBAAiB,MAAM,YAAY;AAE3C,QAAO;;;;ACLR,eAAsB,gBAMrB,iBACA,kBACA,KACA,MACC;CACD,MAAM,WAAW,gBAAgB,IAAI,OAAO,mBAAmB;EAC9D,MAAM,UAAU,iBAAiB,eAAe;AAChD,SAAO,MAAM,QAAQ,gBAAgB,KAAK,KAAK;GAC9C;AACF,QAAO,MAAM,QAAQ,IAAI,SAAS"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@baeta/federation",
3
- "version": "0.0.0",
3
+ "version": "2.0.0-next.13",
4
4
  "keywords": [
5
5
  "baeta",
6
6
  "graphql",
@@ -25,10 +25,64 @@
25
25
  "url": "https://github.com/andreisergiu98"
26
26
  },
27
27
  "type": "module",
28
+ "exports": {
29
+ ".": {
30
+ "types": "./dist/index.d.ts",
31
+ "default": "./dist/index.js"
32
+ }
33
+ },
34
+ "types": "dist/index.d.ts",
28
35
  "files": [
36
+ "dist",
29
37
  "package.json"
30
38
  ],
39
+ "scripts": {
40
+ "build": "builder build",
41
+ "check:deps": "builder check-deps",
42
+ "prepack": "builder prepare",
43
+ "postpack": "builder prepare --restore",
44
+ "test": "builder test",
45
+ "test:circular": "builder test-circular",
46
+ "types": "tsc --noEmit"
47
+ },
48
+ "ava": {
49
+ "extensions": [
50
+ "ts"
51
+ ]
52
+ },
53
+ "devDependencies": {
54
+ "@baeta/builder": "^0.0.0",
55
+ "@baeta/testing": "^0.0.0",
56
+ "@baeta/tsconfig": "^0.0.0",
57
+ "graphql": "^16.6.0",
58
+ "typescript": "^6.0.0"
59
+ },
60
+ "peerDependencies": {
61
+ "graphql": "^16.6.0"
62
+ },
63
+ "engines": {
64
+ "node": ">=22.20.0"
65
+ },
31
66
  "publishConfig": {
32
- "access": "public"
67
+ "access": "public",
68
+ "exports": {
69
+ ".": {
70
+ "types": "./dist/index.d.ts",
71
+ "default": "./dist/index.js"
72
+ }
73
+ }
74
+ },
75
+ "typedocOptions": {
76
+ "entryPoints": [
77
+ "./index.ts"
78
+ ],
79
+ "readme": "none",
80
+ "tsconfig": "./tsconfig.json",
81
+ "sort": [
82
+ "kind",
83
+ "instance-first",
84
+ "required-first",
85
+ "alphabetical-ignoring-documents"
86
+ ]
33
87
  }
34
- }
88
+ }