Skip to content

Commit

Permalink
Support deprecated annotation in OAS and JSON Schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
ardatan committed Oct 9, 2023
1 parent 4a8abe2 commit a13969c
Show file tree
Hide file tree
Showing 12 changed files with 130 additions and 44 deletions.
8 changes: 8 additions & 0 deletions .changeset/lazy-mangos-double.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
'@graphql-mesh/json-schema': patch
'@omnigraph/json-schema': patch
'@omnigraph/openapi': patch
'@graphql-mesh/types': patch
---

Support \`deprecated\` in OpenAPI and JSON Schemas
10 changes: 10 additions & 0 deletions packages/handlers/json-schema/yaml-config.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ interface JsonSchemaBaseOperation {
responseTypeName: String
exposeResponseMetadata: Boolean
argTypeMap: JSON
deprecated: Boolean
}

type JsonSchemaHTTPOperation implements JsonSchemaBaseOperation {
Expand Down Expand Up @@ -215,6 +216,11 @@ type JsonSchemaHTTPOperation implements JsonSchemaBaseOperation {
unless you define an explicit Content-Type header
"""
binary: Boolean

"""
If true, `@deprecated` will be added to the field definition
"""
deprecated: Boolean
}

type JsonSchemaPubSubOperation implements JsonSchemaBaseOperation {
Expand All @@ -234,6 +240,10 @@ type JsonSchemaPubSubOperation implements JsonSchemaBaseOperation {
responseTypeName: String
argTypeMap: JSON
pubsubTopic: String!
"""
If true, `@deprecated` will be added to the field definition
"""
deprecated: Boolean
}

union JsonSchemaOperation = JsonSchemaHTTPOperation | JsonSchemaPubSubOperation
43 changes: 43 additions & 0 deletions packages/loaders/json-schema/src/getComposerFromJSONSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ export interface TypeComposers {
readOnly?: boolean;
writeOnly?: boolean;
flatten?: boolean;
deprecated?: boolean;
}

export function getComposerFromJSONSchema(
Expand Down Expand Up @@ -139,6 +140,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
// If it doesn't have any clue
Expand All @@ -158,6 +160,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
}
Expand Down Expand Up @@ -207,6 +210,7 @@ export function getComposerFromJSONSchema(
nullable: subSchema.nullable,
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
deprecated: subSchema.deprecated,
};
}
if (subSchema.const) {
Expand Down Expand Up @@ -256,6 +260,7 @@ export function getComposerFromJSONSchema(
nullable: subSchema.nullable,
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
deprecated: subSchema.deprecated,
};
}
if (subSchema.enum && subSchema.type !== 'boolean') {
Expand Down Expand Up @@ -318,6 +323,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}

Expand All @@ -335,6 +341,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
}
Expand All @@ -349,6 +356,7 @@ export function getComposerFromJSONSchema(
description: subSchema.description,
nullable: subSchema.nullable,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'binary': {
Expand All @@ -359,6 +367,7 @@ export function getComposerFromJSONSchema(
description: subSchema.description,
nullable: subSchema.nullable,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'date-time': {
Expand All @@ -371,6 +380,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'time': {
Expand All @@ -383,6 +393,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'email': {
Expand All @@ -395,6 +406,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'ipv4': {
Expand All @@ -407,6 +419,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'ipv6': {
Expand All @@ -419,6 +432,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'uri': {
Expand All @@ -431,6 +445,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'uuid': {
Expand All @@ -443,6 +458,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'unix-time': {
Expand All @@ -455,6 +471,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'int64': {
Expand All @@ -467,6 +484,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'int32': {
Expand All @@ -479,6 +497,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'decimal':
Expand All @@ -492,6 +511,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
default: {
Expand All @@ -506,6 +526,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
}
Expand All @@ -524,6 +545,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
} else if (subSchema.minimum > 0) {
const typeComposer = schemaComposer.getAnyTC(
Expand All @@ -537,6 +559,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
if (subSchema.maximum === 0) {
Expand All @@ -551,6 +574,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
} else if (subSchema.maximum < 0) {
const typeComposer = schemaComposer.getAnyTC(
Expand All @@ -564,6 +588,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
if (
Expand All @@ -579,6 +604,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}

Expand All @@ -593,6 +619,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'null': {
Expand All @@ -605,6 +632,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'integer': {
Expand All @@ -617,6 +645,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'number': {
Expand All @@ -629,6 +658,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'string': {
Expand All @@ -642,6 +672,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
if (subSchema.minLength || subSchema.maxLength) {
Expand Down Expand Up @@ -671,6 +702,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
const typeComposer = schemaComposer.getAnyTC(GraphQLString);
Expand All @@ -682,6 +714,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
case 'object': {
Expand Down Expand Up @@ -773,6 +806,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchema.readOnly,
writeOnly: subSchema.writeOnly,
default: subSchema.default,
deprecated: subSchema.deprecated,
};
}
const config = {
Expand Down Expand Up @@ -907,6 +941,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchemaAndTypeComposers.readOnly,
writeOnly: subSchemaAndTypeComposers.writeOnly,
flatten,
deprecated: subSchemaAndTypeComposers.deprecated,
};
}
return getUnionTypeComposers({
Expand Down Expand Up @@ -1321,6 +1356,9 @@ export function getComposerFromJSONSchema(
description:
subSchemaAndTypeComposers.properties[propertyName].description ||
subSchemaAndTypeComposers.properties[propertyName].output?.description,
deprecationReason: subSchemaAndTypeComposers.properties[propertyName].deprecated
? 'deprecated'
: undefined,
};
const directives: Directive[] = [];
if (fieldName !== propertyName) {
Expand Down Expand Up @@ -1359,6 +1397,7 @@ export function getComposerFromJSONSchema(
subSchemaAndTypeComposers.properties[propertyName]?.default ||
subSchemaAndTypeComposers.properties[propertyName]?.extensions?.default ||
subSchemaAndTypeComposers.properties[propertyName]?.input?.default,
// deprecationReason: subSchemaAndTypeComposers.properties[propertyName].deprecated ? 'deprecated' : undefined,
};
}
}
Expand Down Expand Up @@ -1416,6 +1455,7 @@ export function getComposerFromJSONSchema(
default: subSchemaAndTypeComposers.default,
readOnly: subSchemaAndTypeComposers.readOnly,
writeOnly: subSchemaAndTypeComposers.writeOnly,
deprecated: subSchemaAndTypeComposers.deprecated,
};
}
}
Expand Down Expand Up @@ -1464,6 +1504,7 @@ export function getComposerFromJSONSchema(
default: subSchemaAndTypeComposers.default,
readOnly: subSchemaAndTypeComposers.readOnly,
writeOnly: subSchemaAndTypeComposers.writeOnly,
deprecated: subSchemaAndTypeComposers.deprecated,
};
}

Expand All @@ -1476,6 +1517,7 @@ export function getComposerFromJSONSchema(
default: subSchemaAndTypeComposers.default,
readOnly: subSchemaAndTypeComposers.readOnly,
writeOnly: subSchemaAndTypeComposers.writeOnly,
deprecated: subSchemaAndTypeComposers.deprecated,
};
} else {
logger.debug(`GraphQL Type cannot be created for this JSON Schema definition;`, {
Expand All @@ -1491,6 +1533,7 @@ export function getComposerFromJSONSchema(
readOnly: subSchemaAndTypeComposers.readOnly,
writeOnly: subSchemaAndTypeComposers.writeOnly,
default: subSchemaAndTypeComposers.default,
deprecated: subSchemaAndTypeComposers.deprecated,
};
}
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ export async function getReferencedJSONSchemaFromOperations({
);
}

if (operationConfig.deprecated) {
rootTypeDefinition.properties[fieldName].deprecated = true;
}

const rootTypeInputPropertyName = operationType + 'Input';
const rootInputTypeName = rootTypeName + 'Input';
const rootTypeInputTypeDefinition = (finalJsonSchema.properties[rootTypeInputPropertyName] =
Expand Down
2 changes: 2 additions & 0 deletions packages/loaders/json-schema/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ export type JSONSchemaBaseOperationConfig = {

argTypeMap?: Record<string, string | JSONSchemaObject>;
responseByStatusCode?: Record<string, JSONSchemaOperationResponseConfig>;

deprecated?: boolean;
} & JSONSchemaOperationResponseConfig;

export type JSONSchemaBaseOperationConfigWithJSONRequest = JSONSchemaBaseOperationConfig & {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ export async function getJSONSchemaOptionsFromOpenAPIOptions(
schemaHeaders,
operationHeaders,
responseByStatusCode: {},
deprecated: methodObj.deprecated,
...(baseOperationArgTypeMap
? {
argTypeMap: {
Expand Down
Loading

0 comments on commit a13969c

Please sign in to comment.