@wundergraph/protographic 0.1.0
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/LICENSE +201 -0
- package/README.md +162 -0
- package/dist/src/index.d.ts +37 -0
- package/dist/src/index.js +53 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/naming-conventions.d.ts +50 -0
- package/dist/src/naming-conventions.js +62 -0
- package/dist/src/naming-conventions.js.map +1 -0
- package/dist/src/proto-lock.d.ts +66 -0
- package/dist/src/proto-lock.js +150 -0
- package/dist/src/proto-lock.js.map +1 -0
- package/dist/src/sdl-to-mapping-visitor.d.ts +172 -0
- package/dist/src/sdl-to-mapping-visitor.js +365 -0
- package/dist/src/sdl-to-mapping-visitor.js.map +1 -0
- package/dist/src/sdl-to-proto-visitor.d.ts +278 -0
- package/dist/src/sdl-to-proto-visitor.js +1200 -0
- package/dist/src/sdl-to-proto-visitor.js.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +45 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
# Protographic
|
|
2
|
+
|
|
3
|
+
A tool for converting GraphQL Schema Definition Language (SDL) to Protocol Buffers and mapping files.
|
|
4
|
+
|
|
5
|
+
## Overview
|
|
6
|
+
|
|
7
|
+
Protographic bridges GraphQL and Protocol Buffers (protobuf) ecosystems through two core functions:
|
|
8
|
+
|
|
9
|
+
1. **GraphQL SDL to Protocol Buffer (Proto) Compiler**: Transforms GraphQL schemas into Proto3 format, allowing developers to write gRPC services using GraphQL SDL and integrate them seamlessly into the Cosmo Router as standard subgraphs. This is used at build-time.
|
|
10
|
+
|
|
11
|
+
2. **GraphQL SDL to Mapping Compiler**: Creates mapping definitions that maintain the semantic relationships between GraphQL types while adapting them to Protocol Buffer's structural model. This is used by the Cosmo Router at runtime.
|
|
12
|
+
|
|
13
|
+
## Key Features
|
|
14
|
+
|
|
15
|
+
- Precise conversion of GraphQL types to Protocol Buffer messages
|
|
16
|
+
- Consistent naming conventions across GraphQL and Proto definitions
|
|
17
|
+
- Streamlined mapping of GraphQL operations to RPC methods
|
|
18
|
+
- Robust handling of complex GraphQL features (unions, interfaces, directives)
|
|
19
|
+
- First-class support for Federation entity mapping
|
|
20
|
+
- Deterministic field ordering with proto.lock.json for backward compatibility
|
|
21
|
+
|
|
22
|
+
## Installation
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @wundergraph/protographic
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Usage
|
|
29
|
+
|
|
30
|
+
### Converting GraphQL SDL to Protocol Buffer
|
|
31
|
+
|
|
32
|
+
```typescript
|
|
33
|
+
import { compileGraphQLToProto } from '@wundergraph/protographic';
|
|
34
|
+
|
|
35
|
+
const graphqlSchema = `
|
|
36
|
+
type User {
|
|
37
|
+
id: ID!
|
|
38
|
+
name: String!
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
type Query {
|
|
42
|
+
user(id: ID!): User
|
|
43
|
+
}
|
|
44
|
+
`;
|
|
45
|
+
|
|
46
|
+
const protoOutput = compileGraphQLToProto(
|
|
47
|
+
graphqlSchema, // String or GraphQLSchema object
|
|
48
|
+
{
|
|
49
|
+
serviceName: 'UserService', // Service name
|
|
50
|
+
packageName: 'user.v1', // Package name
|
|
51
|
+
goPackage: 'cosmo/pkg/my_package', // Go package name
|
|
52
|
+
lockFilePath: './proto.lock.json', // Optional: Path to proto.lock.json for deterministic field ordering
|
|
53
|
+
}
|
|
54
|
+
);
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
### Using proto.lock.json for Deterministic Field Ordering
|
|
58
|
+
|
|
59
|
+
Protographic supports deterministic field ordering for Protocol Buffer files through a lock file mechanism. This ensures backward compatibility across schema changes:
|
|
60
|
+
|
|
61
|
+
```typescript
|
|
62
|
+
import { compileGraphQLToProto } from '@wundergraph/protographic';
|
|
63
|
+
|
|
64
|
+
// First generation with a new lock file
|
|
65
|
+
const result1 = compileGraphQLToProto(initialSchema, {
|
|
66
|
+
serviceName: 'MyService',
|
|
67
|
+
lockFilePath: './proto.lock.json' // Creates lock file if it doesn't exist
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
// Later generation with schema changes but preserving field order
|
|
71
|
+
const result2 = compileGraphQLToProto(updatedSchema, {
|
|
72
|
+
serviceName: 'MyService',
|
|
73
|
+
lockFilePath: './proto.lock.json' // Uses existing lock file
|
|
74
|
+
});
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
When providing a `lockFilePath`, the function returns an object with both the proto definition and the lock data:
|
|
78
|
+
|
|
79
|
+
```typescript
|
|
80
|
+
const { proto, lockData } = compileGraphQLToProto(schema, {
|
|
81
|
+
lockFilePath: './proto.lock.json'
|
|
82
|
+
});
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
If you prefer to manage the lock data directly without file I/O, you can use the lock data directly:
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import { compileGraphQLToProto, ProtoLock } from '@wundergraph/protographic';
|
|
89
|
+
|
|
90
|
+
// First generation - creates initial lock data
|
|
91
|
+
const result1 = compileGraphQLToProto(initialSchema, {
|
|
92
|
+
serviceName: 'MyService'
|
|
93
|
+
});
|
|
94
|
+
const proto1 = result1.proto;
|
|
95
|
+
const lockData = result1.lockData;
|
|
96
|
+
|
|
97
|
+
// Store the lock data however you want (database, state management, etc.)
|
|
98
|
+
// ...
|
|
99
|
+
|
|
100
|
+
// Later generation with the saved lock data
|
|
101
|
+
const result2 = compileGraphQLToProto(updatedSchema, {
|
|
102
|
+
serviceName: 'MyService',
|
|
103
|
+
lockData: lockData // Use previously generated lock data
|
|
104
|
+
});
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
The lock data records the order of:
|
|
108
|
+
- Service methods
|
|
109
|
+
- Message fields
|
|
110
|
+
- Enum values
|
|
111
|
+
- Implementers of interfaces
|
|
112
|
+
- Members of unions
|
|
113
|
+
|
|
114
|
+
New fields are always added at the end, maintaining backward compatibility with existing proto messages.
|
|
115
|
+
|
|
116
|
+
### Generating Mapping Definitions
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { compileGraphQLToMapping } from '@wundergraph/protographic';
|
|
120
|
+
|
|
121
|
+
const graphqlSchema = `
|
|
122
|
+
type Product @key(fields: "id") {
|
|
123
|
+
id: ID!
|
|
124
|
+
name: String!
|
|
125
|
+
price: Float!
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
type Query {
|
|
129
|
+
product(id: ID!): Product
|
|
130
|
+
}
|
|
131
|
+
`;
|
|
132
|
+
|
|
133
|
+
const mappingOutput = compileGraphQLToMapping(
|
|
134
|
+
graphqlSchema, // String or GraphQLSchema object
|
|
135
|
+
'ProductService', // Service name
|
|
136
|
+
);
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Conversion Rules
|
|
140
|
+
|
|
141
|
+
Protographic follows a set of conventions when converting GraphQL SDL to Protocol Buffers. Please take a look at the following rules:
|
|
142
|
+
|
|
143
|
+
1. GraphQL SDL to Mapping, see [SDL_MAPPING_RULES.md](SDL_MAPPING_RULES.md)
|
|
144
|
+
2. GraphQL SDL to Proto, see [SDL_PROTO_RULES.md](SDL_PROTO_RULES.md)
|
|
145
|
+
|
|
146
|
+
## Integration with graphql-go-tools
|
|
147
|
+
|
|
148
|
+
Protographic generates a mapping file as well as a proto file that can be used with [graphql-go-tools](https://github.com/wundergraph/graphql-go-tools) to enable seamless GraphQL-to-gRPC translation. This integration allows you to:
|
|
149
|
+
|
|
150
|
+
- Translate GraphQL queries to gRPC requests
|
|
151
|
+
- Translate gRPC responses back to GraphQL format
|
|
152
|
+
- Handle advanced GraphQL features (fragments, variables, aliases) within a gRPC context
|
|
153
|
+
|
|
154
|
+
This bidirectional translation preserves type safety while combining the benefits of both GraphQL and gRPC ecosystems.
|
|
155
|
+
|
|
156
|
+
## Contributing
|
|
157
|
+
|
|
158
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
159
|
+
|
|
160
|
+
## License
|
|
161
|
+
|
|
162
|
+
This project is licensed under the Apache-2.0 License.
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { GraphQLSchema } from 'graphql';
|
|
2
|
+
import { GRPCMapping } from '@wundergraph/cosmo-connect/dist/node/v1/node_pb';
|
|
3
|
+
import type { GraphQLToProtoTextVisitorOptions } from './sdl-to-proto-visitor.js';
|
|
4
|
+
import type { ProtoLock } from './proto-lock.js';
|
|
5
|
+
/**
|
|
6
|
+
* Compiles a GraphQL schema to a mapping structure
|
|
7
|
+
*
|
|
8
|
+
* @param schemaOrSDL GraphQL Schema object or SDL string
|
|
9
|
+
* @param serviceName Name of the Proto service to generate
|
|
10
|
+
* @returns Mapping structure
|
|
11
|
+
*/
|
|
12
|
+
export declare function compileGraphQLToMapping(schemaOrSDL: GraphQLSchema | string, serviceName?: string): GRPCMapping;
|
|
13
|
+
/**
|
|
14
|
+
* Compiles a GraphQL schema directly to Protocol Buffer text definition
|
|
15
|
+
*
|
|
16
|
+
* @param schemaOrSDL GraphQL Schema object or SDL string
|
|
17
|
+
* @param options Optional configuration options
|
|
18
|
+
* @returns Protocol Buffer text definition and generated lock data
|
|
19
|
+
*/
|
|
20
|
+
export interface CompileGraphQLToProtoResult {
|
|
21
|
+
proto: string;
|
|
22
|
+
lockData: ProtoLock | null;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Compiles a GraphQL schema directly to Protocol Buffer text definition
|
|
26
|
+
*
|
|
27
|
+
* @param schemaOrSDL GraphQL Schema object or SDL string
|
|
28
|
+
* @param options Optional configuration options
|
|
29
|
+
* @returns Protocol Buffer text definition and lock data
|
|
30
|
+
*/
|
|
31
|
+
export declare function compileGraphQLToProto(schemaOrSDL: GraphQLSchema | string, options?: GraphQLToProtoTextVisitorOptions): CompileGraphQLToProtoResult;
|
|
32
|
+
export * from './sdl-to-mapping-visitor.js';
|
|
33
|
+
export { GraphQLToProtoTextVisitor } from './sdl-to-proto-visitor.js';
|
|
34
|
+
export { ProtoLockManager } from './proto-lock.js';
|
|
35
|
+
export type { GraphQLToProtoTextVisitorOptions } from './sdl-to-proto-visitor.js';
|
|
36
|
+
export type { ProtoLock } from './proto-lock.js';
|
|
37
|
+
export { GRPCMapping, OperationMapping, EntityMapping, TypeFieldMapping, FieldMapping, ArgumentMapping, EnumMapping, EnumValueMapping, OperationType, } from '@wundergraph/cosmo-connect/dist/node/v1/node_pb';
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { buildSchema } from 'graphql';
|
|
2
|
+
import { GraphQLToProtoVisitor } from './sdl-to-mapping-visitor.js';
|
|
3
|
+
import { GraphQLToProtoTextVisitor } from './sdl-to-proto-visitor.js';
|
|
4
|
+
/**
|
|
5
|
+
* Compiles a GraphQL schema to a mapping structure
|
|
6
|
+
*
|
|
7
|
+
* @param schemaOrSDL GraphQL Schema object or SDL string
|
|
8
|
+
* @param serviceName Name of the Proto service to generate
|
|
9
|
+
* @returns Mapping structure
|
|
10
|
+
*/
|
|
11
|
+
export function compileGraphQLToMapping(schemaOrSDL, serviceName = 'DefaultService') {
|
|
12
|
+
// If a string was provided, build the schema
|
|
13
|
+
const schema = typeof schemaOrSDL === 'string'
|
|
14
|
+
? buildSchema(schemaOrSDL, {
|
|
15
|
+
assumeValid: true, // Don't throw on unknown directives
|
|
16
|
+
assumeValidSDL: true, // Skip SDL validation
|
|
17
|
+
})
|
|
18
|
+
: schemaOrSDL;
|
|
19
|
+
// Create and run the visitor
|
|
20
|
+
const visitor = new GraphQLToProtoVisitor(schema, serviceName);
|
|
21
|
+
return visitor.visit();
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Compiles a GraphQL schema directly to Protocol Buffer text definition
|
|
25
|
+
*
|
|
26
|
+
* @param schemaOrSDL GraphQL Schema object or SDL string
|
|
27
|
+
* @param options Optional configuration options
|
|
28
|
+
* @returns Protocol Buffer text definition and lock data
|
|
29
|
+
*/
|
|
30
|
+
export function compileGraphQLToProto(schemaOrSDL, options) {
|
|
31
|
+
// If a string was provided, build the schema
|
|
32
|
+
const schema = typeof schemaOrSDL === 'string'
|
|
33
|
+
? buildSchema(schemaOrSDL, {
|
|
34
|
+
assumeValid: true, // Don't throw on unknown directives
|
|
35
|
+
assumeValidSDL: true, // Skip SDL validation
|
|
36
|
+
})
|
|
37
|
+
: schemaOrSDL;
|
|
38
|
+
// Create and run the visitor with lock data if available
|
|
39
|
+
const visitor = new GraphQLToProtoTextVisitor(schema, options);
|
|
40
|
+
const proto = visitor.visit();
|
|
41
|
+
// Get the generated lock data
|
|
42
|
+
const generatedLockData = visitor.getGeneratedLockData();
|
|
43
|
+
// Always return the object with both proto and lockData
|
|
44
|
+
return {
|
|
45
|
+
proto,
|
|
46
|
+
lockData: generatedLockData,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
export * from './sdl-to-mapping-visitor.js';
|
|
50
|
+
export { GraphQLToProtoTextVisitor } from './sdl-to-proto-visitor.js';
|
|
51
|
+
export { ProtoLockManager } from './proto-lock.js';
|
|
52
|
+
export { GRPCMapping, OperationMapping, EntityMapping, TypeFieldMapping, FieldMapping, ArgumentMapping, EnumMapping, EnumValueMapping, OperationType, } from '@wundergraph/cosmo-connect/dist/node/v1/node_pb';
|
|
53
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EA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C5C,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,gBAAgB,EAAE,MAAM,iBAAiB,CAAC;AAInD,OAAO,EACL,WAAW,EACX,gBAAgB,EAChB,aAAa,EACb,gBAAgB,EAChB,YAAY,EACZ,eAAe,EACf,WAAW,EACX,gBAAgB,EAChB,aAAa,GACd,MAAM,iDAAiD,CAAC"}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared naming conventions for converting GraphQL to Protocol Buffers
|
|
3
|
+
*
|
|
4
|
+
* This utility file ensures consistent naming transformations between
|
|
5
|
+
* the Proto compiler and mapping compiler implementations.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* The names of the GraphQL operation types
|
|
9
|
+
*/
|
|
10
|
+
export type OperationTypeName = 'Query' | 'Mutation' | 'Subscription';
|
|
11
|
+
/**
|
|
12
|
+
* Converts a GraphQL field name to a Protocol Buffer field name (snake_case)
|
|
13
|
+
*/
|
|
14
|
+
export declare function graphqlFieldToProtoField(fieldName: string): string;
|
|
15
|
+
/**
|
|
16
|
+
* Converts a GraphQL argument name to a Protocol Buffer field name (snake_case)
|
|
17
|
+
*/
|
|
18
|
+
export declare function graphqlArgumentToProtoField(argName: string): string;
|
|
19
|
+
/**
|
|
20
|
+
* Creates an operation method name from an operation type and field name
|
|
21
|
+
*/
|
|
22
|
+
export declare function createOperationMethodName(operationType: OperationTypeName, fieldName: string): string;
|
|
23
|
+
/**
|
|
24
|
+
* Creates a request message name for an operation
|
|
25
|
+
*/
|
|
26
|
+
export declare function createRequestMessageName(methodName: string): string;
|
|
27
|
+
/**
|
|
28
|
+
* Creates a response message name for an operation
|
|
29
|
+
*/
|
|
30
|
+
export declare function createResponseMessageName(methodName: string): string;
|
|
31
|
+
/**
|
|
32
|
+
* Creates an entity lookup method name for an entity type
|
|
33
|
+
*/
|
|
34
|
+
export declare function createEntityLookupMethodName(typeName: string, keyField?: string): string;
|
|
35
|
+
/**
|
|
36
|
+
* Creates a request message name for an entity lookup
|
|
37
|
+
*/
|
|
38
|
+
export declare function createEntityLookupRequestName(typeName: string, keyField?: string): string;
|
|
39
|
+
/**
|
|
40
|
+
* Creates a response message name for an entity lookup
|
|
41
|
+
*/
|
|
42
|
+
export declare function createEntityLookupResponseName(typeName: string, keyField?: string): string;
|
|
43
|
+
/**
|
|
44
|
+
* Converts a GraphQL enum value to a Protocol Buffer enum value
|
|
45
|
+
*/
|
|
46
|
+
export declare function graphqlEnumValueToProtoEnumValue(enumTypeName: string, enumValue: string): string;
|
|
47
|
+
/**
|
|
48
|
+
* Creates a Proto enum unspecified value (required as first value in proto3)
|
|
49
|
+
*/
|
|
50
|
+
export declare function createEnumUnspecifiedValue(enumTypeName: string): string;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { camelCase, snakeCase, upperFirst } from 'lodash-es';
|
|
2
|
+
/**
|
|
3
|
+
* Converts a GraphQL field name to a Protocol Buffer field name (snake_case)
|
|
4
|
+
*/
|
|
5
|
+
export function graphqlFieldToProtoField(fieldName) {
|
|
6
|
+
return snakeCase(fieldName);
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Converts a GraphQL argument name to a Protocol Buffer field name (snake_case)
|
|
10
|
+
*/
|
|
11
|
+
export function graphqlArgumentToProtoField(argName) {
|
|
12
|
+
return snakeCase(argName);
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Creates an operation method name from an operation type and field name
|
|
16
|
+
*/
|
|
17
|
+
export function createOperationMethodName(operationType, fieldName) {
|
|
18
|
+
return `${operationType}${upperFirst(camelCase(fieldName))}`;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Creates a request message name for an operation
|
|
22
|
+
*/
|
|
23
|
+
export function createRequestMessageName(methodName) {
|
|
24
|
+
return `${methodName}Request`;
|
|
25
|
+
}
|
|
26
|
+
/**
|
|
27
|
+
* Creates a response message name for an operation
|
|
28
|
+
*/
|
|
29
|
+
export function createResponseMessageName(methodName) {
|
|
30
|
+
return `${methodName}Response`;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Creates an entity lookup method name for an entity type
|
|
34
|
+
*/
|
|
35
|
+
export function createEntityLookupMethodName(typeName, keyField = 'id') {
|
|
36
|
+
return `Lookup${typeName}By${upperFirst(camelCase(keyField))}`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Creates a request message name for an entity lookup
|
|
40
|
+
*/
|
|
41
|
+
export function createEntityLookupRequestName(typeName, keyField = 'id') {
|
|
42
|
+
return `Lookup${typeName}By${upperFirst(camelCase(keyField))}Request`;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Creates a response message name for an entity lookup
|
|
46
|
+
*/
|
|
47
|
+
export function createEntityLookupResponseName(typeName, keyField = 'id') {
|
|
48
|
+
return `Lookup${typeName}By${upperFirst(camelCase(keyField))}Response`;
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Converts a GraphQL enum value to a Protocol Buffer enum value
|
|
52
|
+
*/
|
|
53
|
+
export function graphqlEnumValueToProtoEnumValue(enumTypeName, enumValue) {
|
|
54
|
+
return `${snakeCase(enumTypeName).toUpperCase()}_${enumValue}`;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Creates a Proto enum unspecified value (required as first value in proto3)
|
|
58
|
+
*/
|
|
59
|
+
export function createEnumUnspecifiedValue(enumTypeName) {
|
|
60
|
+
return `${snakeCase(enumTypeName).toUpperCase()}_UNSPECIFIED`;
|
|
61
|
+
}
|
|
62
|
+
//# sourceMappingURL=naming-conventions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"naming-conventions.js","sourceRoot":"","sources":["../../src/naming-conventions.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,SAAS,EAAE,UAAU,EAAE,MAAM,WAAW,CAAC;AAc7D;;GAEG;AACH,MAAM,UA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M,UAAU,0BAA0B,CAAC,YAAoB;IAC7D,OAAO,GAAG,SAAS,CAAC,YAAY,CAAC,CAAC,WAAW,EAAE,cAAc,CAAC;AAChE,CAAC"}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for Proto Lock file structure
|
|
3
|
+
*/
|
|
4
|
+
export interface ProtoLock {
|
|
5
|
+
version: string;
|
|
6
|
+
messages: Record<string, MessageLock>;
|
|
7
|
+
enums: Record<string, EnumLock>;
|
|
8
|
+
}
|
|
9
|
+
interface MessageLock {
|
|
10
|
+
fields: Record<string, number>;
|
|
11
|
+
reservedNumbers?: number[];
|
|
12
|
+
}
|
|
13
|
+
interface EnumLock {
|
|
14
|
+
fields: Record<string, number>;
|
|
15
|
+
reservedNumbers?: number[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Class to manage proto lock data for deterministic field ordering
|
|
19
|
+
*/
|
|
20
|
+
export declare class ProtoLockManager {
|
|
21
|
+
private readonly lockData;
|
|
22
|
+
/**
|
|
23
|
+
* Create a new ProtoLockManager
|
|
24
|
+
*
|
|
25
|
+
* @param initialLockData - Initial lock data to use, if any
|
|
26
|
+
*/
|
|
27
|
+
constructor(initialLockData?: ProtoLock);
|
|
28
|
+
/**
|
|
29
|
+
* Generic method to reconcile items and their numbers
|
|
30
|
+
*
|
|
31
|
+
* @param container - The container object (messages or enums)
|
|
32
|
+
* @param itemName - Name of the item (message or enum)
|
|
33
|
+
* @param availableItems - Available item names (fields or enum values)
|
|
34
|
+
* @returns Ordered array of item names
|
|
35
|
+
*/
|
|
36
|
+
private reconcileItems;
|
|
37
|
+
/**
|
|
38
|
+
* Reconcile and get the ordered field names for a message
|
|
39
|
+
*
|
|
40
|
+
* @param messageName - Name of the message
|
|
41
|
+
* @param availableFields - Available field names (used when no lock exists)
|
|
42
|
+
* @returns Ordered array of field names
|
|
43
|
+
*/
|
|
44
|
+
reconcileMessageFieldOrder(messageName: string, availableFields: string[]): string[];
|
|
45
|
+
/**
|
|
46
|
+
* Reconcile and get the ordered enum values
|
|
47
|
+
*
|
|
48
|
+
* @param enumName - Name of the enum
|
|
49
|
+
* @param availableValues - Available enum values (used when no lock exists)
|
|
50
|
+
* @returns Ordered array of enum values
|
|
51
|
+
*/
|
|
52
|
+
reconcileEnumValueOrder(enumName: string, availableValues: string[]): string[];
|
|
53
|
+
/**
|
|
54
|
+
* Reconcile and get the ordered argument names for a field
|
|
55
|
+
*
|
|
56
|
+
* @param fieldPath - Path to the field in the format "TypeName.fieldName"
|
|
57
|
+
* @param availableArgs - Available argument names (used when no lock exists)
|
|
58
|
+
* @returns Ordered array of argument names
|
|
59
|
+
*/
|
|
60
|
+
reconcileArgumentOrder(fieldPath: string, availableArgs: string[]): string[];
|
|
61
|
+
/**
|
|
62
|
+
* Get the current lock data
|
|
63
|
+
*/
|
|
64
|
+
getLockData(): ProtoLock;
|
|
65
|
+
}
|
|
66
|
+
export {};
|