@omnigraph/openapi 1.0.0-alpha-3fc47d119.0 → 1.0.0-alpha-20220804093904-8e2e41f7f

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.
Files changed (3) hide show
  1. package/index.js +30 -6
  2. package/index.mjs +31 -7
  3. package/package.json +5 -5
package/index.js CHANGED
@@ -46,7 +46,7 @@ function getFieldNameFromPath(path, method, responseTypeSchemaRef) {
46
46
  }
47
47
 
48
48
  async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFormat, cwd, fetch: fetchFn, baseUrl, schemaHeaders, operationHeaders, selectQueryOrMutationField = [], logger = new utils.DefaultLogger('getJSONSchemaOptionsFromOpenAPIOptions'), }) {
49
- var _a, _b, _c, _d, _e;
49
+ var _a, _b, _c, _d, _e, _f;
50
50
  const fieldTypeMap = {};
51
51
  for (const { fieldName, type } of selectQueryOrMutationField) {
52
52
  fieldTypeMap[fieldName] = type;
@@ -69,7 +69,11 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
69
69
  }
70
70
  for (const relativePath in oasOrSwagger.paths) {
71
71
  const pathObj = oasOrSwagger.paths[relativePath];
72
+ const pathParameters = pathObj.parameters;
72
73
  for (const method in pathObj) {
74
+ if (method === 'parameters') {
75
+ continue;
76
+ }
73
77
  const methodObj = pathObj[method];
74
78
  const operationConfig = {
75
79
  method: method.toUpperCase(),
@@ -82,8 +86,21 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
82
86
  responseByStatusCode: {},
83
87
  };
84
88
  operations.push(operationConfig);
85
- for (const paramObjIndex in methodObj.parameters) {
86
- const paramObj = methodObj.parameters[paramObjIndex];
89
+ let allParams;
90
+ if (methodObj.parameters && Array.isArray(methodObj.parameters)) {
91
+ allParams = [...(pathParameters || []), ...methodObj.parameters];
92
+ }
93
+ else {
94
+ allParams = {
95
+ ...(pathParameters || {}),
96
+ ...(methodObj.parameters || {}),
97
+ };
98
+ }
99
+ for (const paramObjIndex in allParams) {
100
+ let paramObj = allParams[paramObjIndex];
101
+ if ('$ref' in paramObj) {
102
+ paramObj = jsonMachete.resolvePath(paramObj.$ref.split('#')[1], oasOrSwagger);
103
+ }
87
104
  const argName = utils.sanitizeNameForGraphQL(paramObj.name);
88
105
  switch (paramObj.in) {
89
106
  case 'query':
@@ -107,11 +124,18 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
107
124
  requestSchema.required = requestSchema.required || [];
108
125
  requestSchema.required.push(paramObj.name);
109
126
  }
127
+ // Fix the reference
128
+ if ((_c = requestSchema.properties[paramObj.name].$ref) === null || _c === void 0 ? void 0 : _c.startsWith('#')) {
129
+ requestSchema.properties[paramObj.name].$ref = `${oasFilePath}${requestSchema.properties[paramObj.name].$ref}`;
130
+ }
110
131
  }
111
132
  else {
112
133
  if (!operationConfig.path.includes('?')) {
113
134
  operationConfig.path += '?';
114
135
  }
136
+ if (operationConfig.path !== '?') {
137
+ operationConfig.path += '&';
138
+ }
115
139
  operationConfig.path += `${paramObj.name}={args.${argName}}`;
116
140
  }
117
141
  break;
@@ -147,7 +171,7 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
147
171
  }
148
172
  break;
149
173
  }
150
- switch (((_c = paramObj.schema) === null || _c === void 0 ? void 0 : _c.type) || paramObj.type) {
174
+ switch (((_d = paramObj.schema) === null || _d === void 0 ? void 0 : _d.type) || paramObj.type) {
151
175
  case 'string':
152
176
  operationConfig.argTypeMap = operationConfig.argTypeMap || {};
153
177
  operationConfig.argTypeMap[argName] = 'String';
@@ -175,13 +199,13 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
175
199
  const requestBodyObj = methodObj.requestBody;
176
200
  if ('content' in requestBodyObj) {
177
201
  const contentKey = Object.keys(requestBodyObj.content)[0];
178
- const contentSchema = (_d = requestBodyObj.content[contentKey]) === null || _d === void 0 ? void 0 : _d.schema;
202
+ const contentSchema = (_e = requestBodyObj.content[contentKey]) === null || _e === void 0 ? void 0 : _e.schema;
179
203
  if (contentSchema && Object.keys(contentSchema).length > 0) {
180
204
  operationConfig.requestSchema = `${oasFilePath}#/paths/${relativePath
181
205
  .split('/')
182
206
  .join('~1')}/${method}/requestBody/content/${contentKey === null || contentKey === void 0 ? void 0 : contentKey.toString().split('/').join('~1')}/schema`;
183
207
  }
184
- const examplesObj = (_e = requestBodyObj.content[contentKey]) === null || _e === void 0 ? void 0 : _e.examples;
208
+ const examplesObj = (_f = requestBodyObj.content[contentKey]) === null || _f === void 0 ? void 0 : _f.examples;
185
209
  if (examplesObj) {
186
210
  operationConfig.requestSample = Object.values(examplesObj)[0];
187
211
  }
package/index.mjs CHANGED
@@ -1,7 +1,7 @@
1
1
  import { loadGraphQLSchemaFromJSONSchemas, createBundle as createBundle$1 } from '@omnigraph/json-schema';
2
2
  export { getGraphQLSchemaFromBundle } from '@omnigraph/json-schema';
3
3
  import { DefaultLogger, readFileOrUrl, defaultImportFn, sanitizeNameForGraphQL } from '@graphql-mesh/utils';
4
- import { dereferenceObject, resolvePath } from 'json-machete';
4
+ import { resolvePath, dereferenceObject } from 'json-machete';
5
5
  import { camelCase } from 'change-case';
6
6
  import { getInterpolatedHeadersFactory } from '@graphql-mesh/string-interpolation';
7
7
  import { process } from '@graphql-mesh/cross-helpers';
@@ -43,7 +43,7 @@ function getFieldNameFromPath(path, method, responseTypeSchemaRef) {
43
43
  }
44
44
 
45
45
  async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFormat, cwd, fetch: fetchFn, baseUrl, schemaHeaders, operationHeaders, selectQueryOrMutationField = [], logger = new DefaultLogger('getJSONSchemaOptionsFromOpenAPIOptions'), }) {
46
- var _a, _b, _c, _d, _e;
46
+ var _a, _b, _c, _d, _e, _f;
47
47
  const fieldTypeMap = {};
48
48
  for (const { fieldName, type } of selectQueryOrMutationField) {
49
49
  fieldTypeMap[fieldName] = type;
@@ -66,7 +66,11 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
66
66
  }
67
67
  for (const relativePath in oasOrSwagger.paths) {
68
68
  const pathObj = oasOrSwagger.paths[relativePath];
69
+ const pathParameters = pathObj.parameters;
69
70
  for (const method in pathObj) {
71
+ if (method === 'parameters') {
72
+ continue;
73
+ }
70
74
  const methodObj = pathObj[method];
71
75
  const operationConfig = {
72
76
  method: method.toUpperCase(),
@@ -79,8 +83,21 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
79
83
  responseByStatusCode: {},
80
84
  };
81
85
  operations.push(operationConfig);
82
- for (const paramObjIndex in methodObj.parameters) {
83
- const paramObj = methodObj.parameters[paramObjIndex];
86
+ let allParams;
87
+ if (methodObj.parameters && Array.isArray(methodObj.parameters)) {
88
+ allParams = [...(pathParameters || []), ...methodObj.parameters];
89
+ }
90
+ else {
91
+ allParams = {
92
+ ...(pathParameters || {}),
93
+ ...(methodObj.parameters || {}),
94
+ };
95
+ }
96
+ for (const paramObjIndex in allParams) {
97
+ let paramObj = allParams[paramObjIndex];
98
+ if ('$ref' in paramObj) {
99
+ paramObj = resolvePath(paramObj.$ref.split('#')[1], oasOrSwagger);
100
+ }
84
101
  const argName = sanitizeNameForGraphQL(paramObj.name);
85
102
  switch (paramObj.in) {
86
103
  case 'query':
@@ -104,11 +121,18 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
104
121
  requestSchema.required = requestSchema.required || [];
105
122
  requestSchema.required.push(paramObj.name);
106
123
  }
124
+ // Fix the reference
125
+ if ((_c = requestSchema.properties[paramObj.name].$ref) === null || _c === void 0 ? void 0 : _c.startsWith('#')) {
126
+ requestSchema.properties[paramObj.name].$ref = `${oasFilePath}${requestSchema.properties[paramObj.name].$ref}`;
127
+ }
107
128
  }
108
129
  else {
109
130
  if (!operationConfig.path.includes('?')) {
110
131
  operationConfig.path += '?';
111
132
  }
133
+ if (operationConfig.path !== '?') {
134
+ operationConfig.path += '&';
135
+ }
112
136
  operationConfig.path += `${paramObj.name}={args.${argName}}`;
113
137
  }
114
138
  break;
@@ -144,7 +168,7 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
144
168
  }
145
169
  break;
146
170
  }
147
- switch (((_c = paramObj.schema) === null || _c === void 0 ? void 0 : _c.type) || paramObj.type) {
171
+ switch (((_d = paramObj.schema) === null || _d === void 0 ? void 0 : _d.type) || paramObj.type) {
148
172
  case 'string':
149
173
  operationConfig.argTypeMap = operationConfig.argTypeMap || {};
150
174
  operationConfig.argTypeMap[argName] = 'String';
@@ -172,13 +196,13 @@ async function getJSONSchemaOptionsFromOpenAPIOptions({ oasFilePath, fallbackFor
172
196
  const requestBodyObj = methodObj.requestBody;
173
197
  if ('content' in requestBodyObj) {
174
198
  const contentKey = Object.keys(requestBodyObj.content)[0];
175
- const contentSchema = (_d = requestBodyObj.content[contentKey]) === null || _d === void 0 ? void 0 : _d.schema;
199
+ const contentSchema = (_e = requestBodyObj.content[contentKey]) === null || _e === void 0 ? void 0 : _e.schema;
176
200
  if (contentSchema && Object.keys(contentSchema).length > 0) {
177
201
  operationConfig.requestSchema = `${oasFilePath}#/paths/${relativePath
178
202
  .split('/')
179
203
  .join('~1')}/${method}/requestBody/content/${contentKey === null || contentKey === void 0 ? void 0 : contentKey.toString().split('/').join('~1')}/schema`;
180
204
  }
181
- const examplesObj = (_e = requestBodyObj.content[contentKey]) === null || _e === void 0 ? void 0 : _e.examples;
205
+ const examplesObj = (_f = requestBodyObj.content[contentKey]) === null || _f === void 0 ? void 0 : _f.examples;
182
206
  if (examplesObj) {
183
207
  operationConfig.requestSample = Object.values(examplesObj)[0];
184
208
  }
package/package.json CHANGED
@@ -1,18 +1,18 @@
1
1
  {
2
2
  "name": "@omnigraph/openapi",
3
- "version": "1.0.0-alpha-3fc47d119.0",
3
+ "version": "1.0.0-alpha-20220804093904-8e2e41f7f",
4
4
  "sideEffects": false,
5
5
  "peerDependencies": {
6
- "@graphql-mesh/types": "0.79.0-alpha-3fc47d119.0",
7
- "@graphql-mesh/utils": "1.0.0-alpha-3fc47d119.0",
6
+ "@graphql-mesh/types": "0.79.0-alpha-20220804093904-8e2e41f7f",
7
+ "@graphql-mesh/utils": "1.0.0-alpha-20220804093904-8e2e41f7f",
8
8
  "graphql": "*"
9
9
  },
10
10
  "dependencies": {
11
11
  "@graphql-mesh/cross-helpers": "0.2.0",
12
12
  "@graphql-mesh/string-interpolation": "0.3.0",
13
- "@omnigraph/json-schema": "1.0.0-alpha-3fc47d119.0",
13
+ "@omnigraph/json-schema": "1.0.0-alpha-20220804093904-8e2e41f7f",
14
14
  "change-case": "4.1.2",
15
- "json-machete": "1.0.0-alpha-3fc47d119.0",
15
+ "json-machete": "1.0.0-alpha-20220804093904-8e2e41f7f",
16
16
  "openapi-types": "12.0.0",
17
17
  "tslib": "^2.4.0"
18
18
  },