Skip to content

Commit 7676d34

Browse files
committed
Improve implements generation
1 parent ab5345a commit 7676d34

File tree

5 files changed

+52
-16
lines changed

5 files changed

+52
-16
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
[![version (scoped)](https://img.shields.io/npm/v/@manifoldco/graphql-gen.svg)](https://www.npmjs.com/package/@manifoldco/graphql-gen)
2+
13
# ⚛️ graphql-gen
24

35
Node client for generating crude GraphQL specs from Swagger OpenAPI.
@@ -10,8 +12,7 @@ Node client for generating crude GraphQL specs from Swagger OpenAPI.
1012
| :----------------------------------- | :-: |
1113
| Enum ||
1214
| ID ||
13-
| Implements (`allOf`) ||
14-
| Polymorphism (`oneOf`) ||
15+
| Union (`oneOf`) ||
1516
| Non-nullable ||
1617
| Primitives (string, boolean, number) ||
1718
| Query | 🚫 |

example/output.graphql

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,21 @@ enum ExpandedProductType {
296296
enum ExpandedProductVersion {
297297
Version1
298298
}
299-
type ExpandedPlanBody implements PlanBody {
299+
type ExpandedPlanBody {
300+
providerId: ID
301+
productId: ID
302+
name: String
303+
label: String
304+
state: String
305+
resizableTo: [ID]
306+
"Array of Region IDs"
307+
regions: [ID]
308+
"Array of Feature Values"
309+
features: [FeatureValue]
310+
"The number of days a user gets as a free trial when subscribing to this plan. Trials are valid only once per product; changing plans or adding an additional subscription will not start a new trial."
311+
trialDays: Int
312+
"Dollar value in cents."
313+
cost: Int
300314
"An array of feature definitions for the plan, as defined on the Product."
301315
expandedFeatures: [ExpandedFeature]
302316
"A boolean flag that indicates if a plan is free or not based on it's cost and features."
@@ -318,11 +332,28 @@ enum ExpandedPlanType {
318332
enum ExpandedPlanVersion {
319333
Version1
320334
}
321-
type ExpandedFeature implements FeatureType {
335+
type ExpandedFeature {
336+
label: String
337+
name: String
338+
type: ExpandedFeatureType
339+
"This sets whether or not the feature can be customized by a consumer."
340+
customizable: Boolean
341+
"This sets whether or not the feature can be upgraded by the consumer after the resource has provisioned. Upgrading means setting a higher value or selecting a higher element in the list."
342+
upgradable: Boolean
343+
"This sets whether or not the feature can be downgraded by the consumer after the resource has provisioned. Downgrading means setting a lower value or selecting a lower element in the list."
344+
downgradable: Boolean
345+
"Sets if this feature’s value is trackable from the provider, this only really affects numeric constraints."
346+
measurable: Boolean
347+
values: [FeatureValueDetails]
322348
"The string value set for the feature on the plan, this should only be used if the value property is null."
323349
valueString: String
324350
value: FeatureValueDetails
325351
}
352+
enum ExpandedFeatureType {
353+
BOOLEAN
354+
STRING
355+
NUMBER
356+
}
326357
type Error {
327358
"The error type"
328359
type: String!

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@manifoldco/graphql-gen",
3-
"version": "1.1.3",
3+
"version": "1.1.5",
44
"description": "Generate GraphQL schemas from Swagger OpenAPI specs ",
55
"main": "dist/index.js",
66
"bin": {

src/swagger-2.ts

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -133,35 +133,38 @@ function parse(spec: Swagger2) {
133133
}
134134
const [ID, { allOf, properties, required }] = nextObject;
135135

136-
let allProperties = properties || {};
137-
const implementations: string[] = [];
136+
let allProperties = {};
138137

139138
// Include allOf, if specified
140139
if (Array.isArray(allOf)) {
141140
allOf.forEach(item => {
142141
// Add “implements“ if this references other items
143142
if (item.$ref) {
144-
const [refName] = getRef(item.$ref);
145-
implementations.push(refName);
143+
const refProperties = getRef(item.$ref)[1];
144+
if (refProperties && refProperties.properties) {
145+
allProperties = { ...allProperties, ...refProperties.properties };
146+
}
146147
} else if (item.properties) {
147148
allProperties = { ...allProperties, ...item.properties };
148149
}
149150
});
150151
}
151152

153+
// Then add new properties
154+
if (properties && Object.keys(properties).length) {
155+
allProperties = { ...allProperties, ...properties };
156+
}
157+
152158
// If nothing’s here, let’s skip this one.
153159
if (!Object.keys(allProperties).length) {
154160
return;
155161
}
156-
// Open type
157-
const isImplementing = implementations.length
158-
? ` implements ${implementations.join(', ')}`
159-
: '';
160162

161-
output.push(`type ${camelCase(ID)}${isImplementing} {`);
163+
// Open type
164+
output.push(`type ${camelCase(ID)} {`);
162165

163166
// Populate type
164-
Object.entries(allProperties).forEach(([key, value]) => {
167+
Object.entries(allProperties).forEach(([key, value]: [string, Swagger2Definition]) => {
165168
const optional = !Array.isArray(required) || required.indexOf(key) === -1;
166169
const nonNullable = optional ? '' : '!';
167170
const name = camelCase(key);

tests/swagger-2.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,8 @@ describe('Swagger 2 spec', () => {
284284
type User {
285285
email: String
286286
}
287-
type Admin implements User {
287+
type Admin {
288+
email: String
288289
rbac: String
289290
}`);
290291

0 commit comments

Comments
 (0)