Skip to content

Commit b65c168

Browse files
committed
Add graphQLSchemaPrefix option for generate graphQL schema add prefix
1 parent 8951518 commit b65c168

File tree

5 files changed

+112
-51
lines changed

5 files changed

+112
-51
lines changed

packages/openapi-to-graphql/src/auth_builder.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import debug from 'debug'
3030
import { handleWarning, sortObject, MitigationTypes } from './utils'
3131
import { createDataDef } from './preprocessor'
3232
import crossFetch from 'cross-fetch'
33+
import {InternalOptions} from './types/options';
3334

3435
const translationLog = debug('translation')
3536

@@ -43,7 +44,8 @@ export function createAndLoadViewer<TSource, TContext, TArgs>(
4344
queryFields: object,
4445
operationType: GraphQLOperationType,
4546
data: PreprocessingData<TSource, TContext, TArgs>,
46-
fetch: typeof crossFetch
47+
fetch: typeof crossFetch,
48+
options: InternalOptions<TSource, TContext, TArgs>
4749
): { [key: string]: GraphQLFieldConfig<TSource, TContext, TArgs> } {
4850
const results = {}
4951
/**
@@ -153,7 +155,8 @@ export function createAndLoadViewer<TSource, TContext, TArgs>(
153155
anyAuthObjectName,
154156
anyAuthFields,
155157
data,
156-
fetch
158+
fetch,
159+
options,
157160
)
158161

159162
return results
@@ -252,7 +255,8 @@ function getViewerAnyAuthOT<TSource, TContext, TArgs>(
252255
name: string,
253256
queryFields: GraphQLFieldConfigMap<any, any>,
254257
data: PreprocessingData<TSource, TContext, TArgs>,
255-
fetch: typeof crossFetch
258+
fetch: typeof crossFetch,
259+
options: InternalOptions<TSource, TContext, TArgs>
256260
): GraphQLFieldConfig<TSource, TContext, TArgs> {
257261
// Resolve function:
258262
const resolve: GraphQLFieldResolver<TSource, TContext, TArgs> = (
@@ -277,14 +281,16 @@ function getViewerAnyAuthOT<TSource, TContext, TArgs>(
277281
data.security[protocolName].schema,
278282
true,
279283
data,
280-
data.security[protocolName].oas
284+
data.security[protocolName].oas,
285+
options,
281286
)
282287

283288
const type = getGraphQLType({
284289
def,
285290
data,
286291
isInputObjectType: true,
287-
fetch
292+
fetch,
293+
options
288294
})
289295

290296
const saneProtocolName = Oas3Tools.sanitize(

packages/openapi-to-graphql/src/index.ts

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,9 @@ const DEFAULT_OPTIONS: InternalOptions<any, any, any> = {
127127
provideErrorExtensions: true,
128128
equivalentToMessages: true,
129129

130-
fetch: crossFetch
130+
fetch: crossFetch,
131+
132+
graphQLSchemaPrefix: '',
131133
}
132134

133135
/**
@@ -214,7 +216,9 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
214216
provideErrorExtensions,
215217
equivalentToMessages,
216218

217-
fetch
219+
fetch,
220+
221+
graphQLSchemaPrefix
218222
}: InternalOptions<TSource, TContext, TArgs>
219223
): Result<TSource, TContext, TArgs> {
220224
const options = {
@@ -256,7 +260,9 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
256260
provideErrorExtensions,
257261
equivalentToMessages,
258262

259-
fetch
263+
fetch,
264+
265+
graphQLSchemaPrefix,
260266
}
261267
translationLog(`Options: ${JSON.stringify(options)}`)
262268

@@ -383,7 +389,8 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
383389
authQueryFields,
384390
GraphQLOperationType.Query,
385391
data,
386-
fetch
392+
fetch,
393+
options
387394
)
388395
)
389396
}
@@ -395,7 +402,8 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
395402
authMutationFields,
396403
GraphQLOperationType.Mutation,
397404
data,
398-
fetch
405+
fetch,
406+
options
399407
)
400408
)
401409
}
@@ -407,7 +415,8 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
407415
authSubscriptionFields,
408416
GraphQLOperationType.Subscription,
409417
data,
410-
fetch
418+
fetch,
419+
options
411420
)
412421
)
413422
}
@@ -493,7 +502,8 @@ function addQueryFields<TSource, TContext, TArgs>({
493502
requestOptions,
494503
fileUploadOptions,
495504
connectOptions,
496-
fetch
505+
fetch,
506+
options
497507
)
498508

499509
const saneOperationId = Oas3Tools.sanitize(
@@ -672,7 +682,8 @@ function addMutationFields<TSource, TContext, TArgs>({
672682
requestOptions,
673683
fileUploadOptions,
674684
connectOptions,
675-
fetch
685+
fetch,
686+
options,
676687
)
677688

678689
const saneOperationId = Oas3Tools.sanitize(
@@ -819,7 +830,8 @@ function addSubscriptionFields<TSource, TContext, TArgs>({
819830
requestOptions,
820831
fileUploadOptions,
821832
connectOptions,
822-
fetch
833+
fetch,
834+
options
823835
)
824836

825837
const saneOperationId = Oas3Tools.sanitize(
@@ -923,14 +935,16 @@ function getFieldForOperation<TSource, TContext, TArgs>(
923935
requestOptions: Partial<RequestOptions<TSource, TContext, TArgs>>,
924936
fileUploadOptions: FileUploadOptions,
925937
connectOptions: ConnectOptions,
926-
fetch: typeof crossFetch
938+
fetch: typeof crossFetch,
939+
options: InternalOptions<TSource, TContext, TArgs>
927940
): GraphQLFieldConfig<TSource, TContext | SubscriptionContext, TArgs> {
928941
// Create GraphQL Type for response:
929942
const type = getGraphQLType({
930943
def: operation.responseDefinition,
931944
data,
932945
operation,
933-
fetch
946+
fetch,
947+
options
934948
}) as GraphQLOutputType
935949

936950
const payloadSchemaName = operation.payloadDefinition
@@ -948,7 +962,8 @@ function getFieldForOperation<TSource, TContext, TArgs>(
948962
parameters: operation.parameters,
949963
operation,
950964
data,
951-
fetch
965+
fetch,
966+
options
952967
})
953968

954969
// Get resolver and subscribe function for Subscription fields

packages/openapi-to-graphql/src/preprocessor.ts

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ function processOperation<TSource, TContext, TArgs>(
118118
payloadSchema as SchemaObject,
119119
true,
120120
data,
121-
oas
121+
oas,
122+
options
122123
)
123124
: undefined
124125

@@ -132,6 +133,7 @@ function processOperation<TSource, TContext, TArgs>(
132133
false,
133134
data,
134135
oas,
136+
options,
135137
links
136138
)
137139

@@ -675,6 +677,7 @@ export function createDataDef<TSource, TContext, TArgs>(
675677
isInputObjectType: boolean,
676678
data: PreprocessingData<TSource, TContext, TArgs>,
677679
oas: Oas3,
680+
options: InternalOptions<TSource, TContext, TArgs>,
678681
links?: { [key: string]: LinkObject }
679682
): DataDefinition {
680683
const preferredName = getPreferredName(names)
@@ -747,6 +750,8 @@ export function createDataDef<TSource, TContext, TArgs>(
747750
return existingDataDef
748751
}
749752

753+
names = Object.fromEntries(Object.entries(names).map(([key, value]) => [key, value ? options.graphQLSchemaPrefix + '-' + value : value]))
754+
750755
// There is no preexisting data definition, so create a new one
751756

752757
const name = getSchemaName(names, data.usedTypeNames)
@@ -828,7 +833,8 @@ export function createDataDef<TSource, TContext, TArgs>(
828833
def.required,
829834
isInputObjectType,
830835
data,
831-
oas
836+
oas,
837+
options
832838
)
833839
} else {
834840
handleWarning({
@@ -869,7 +875,8 @@ export function createDataDef<TSource, TContext, TArgs>(
869875
itemsSchema as SchemaObject,
870876
isInputObjectType,
871877
data,
872-
oas
878+
oas,
879+
options
873880
)
874881

875882
// Add list item reference
@@ -893,7 +900,8 @@ export function createDataDef<TSource, TContext, TArgs>(
893900
isInputObjectType,
894901
def,
895902
data,
896-
oas
903+
oas,
904+
options
897905
)
898906
} else {
899907
throw new Error(
@@ -919,7 +927,8 @@ export function createDataDef<TSource, TContext, TArgs>(
919927
isInputObjectType,
920928
def,
921929
data,
922-
oas
930+
oas,
931+
options
923932
)
924933
} else {
925934
throw new Error(
@@ -1226,7 +1235,8 @@ function addObjectPropertiesToDataDef<TSource, TContext, TArgs>(
12261235
required: string[],
12271236
isInputObjectType: boolean,
12281237
data: PreprocessingData<TSource, TContext, TArgs>,
1229-
oas: Oas3
1238+
oas: Oas3,
1239+
options: InternalOptions<TSource, TContext, TArgs>
12301240
) {
12311241
/**
12321242
* Resolve all required properties
@@ -1268,7 +1278,8 @@ function addObjectPropertiesToDataDef<TSource, TContext, TArgs>(
12681278
propSchema,
12691279
isInputObjectType,
12701280
data,
1271-
oas
1281+
oas,
1282+
options
12721283
)
12731284

12741285
// Add field type references
@@ -1351,7 +1362,8 @@ function createAnyOfObject<TSource, TContext, TArgs>(
13511362
isInputObjectType: boolean,
13521363
def: DataDefinition,
13531364
data: PreprocessingData<TSource, TContext, TArgs>,
1354-
oas: Oas3
1365+
oas: Oas3,
1366+
options: InternalOptions<TSource, TContext, TArgs>
13551367
) {
13561368
/**
13571369
* Used to find incompatible properties
@@ -1481,7 +1493,8 @@ function createAnyOfObject<TSource, TContext, TArgs>(
14811493
def.required,
14821494
isInputObjectType,
14831495
data,
1484-
oas
1496+
oas,
1497+
options
14851498
)
14861499
}
14871500

@@ -1503,7 +1516,8 @@ function createAnyOfObject<TSource, TContext, TArgs>(
15031516
propertySchema,
15041517
isInputObjectType,
15051518
data,
1506-
oas
1519+
oas,
1520+
options,
15071521
)
15081522

15091523
/**
@@ -1540,7 +1554,8 @@ function createOneOfUnion<TSource, TContext, TArgs>(
15401554
isInputObjectType: boolean,
15411555
def: DataDefinition,
15421556
data: PreprocessingData<TSource, TContext, TArgs>,
1543-
oas: Oas3
1557+
oas: Oas3,
1558+
options: InternalOptions<TSource, TContext, TArgs>
15441559
) {
15451560
if (isInputObjectType) {
15461561
handleWarning({
@@ -1584,6 +1599,7 @@ function createOneOfUnion<TSource, TContext, TArgs>(
15841599
isInputObjectType,
15851600
data,
15861601
oas,
1602+
options,
15871603
def.links
15881604
)
15891605
;(def.subDefinitions as DataDefinition[]).push(subDefinition)

0 commit comments

Comments
 (0)