diff --git a/packages/openapi-to-graphql/src/index.ts b/packages/openapi-to-graphql/src/index.ts index 5d2d72aa..81f68c65 100644 --- a/packages/openapi-to-graphql/src/index.ts +++ b/packages/openapi-to-graphql/src/index.ts @@ -951,6 +951,8 @@ function getFieldForOperation( fetch }) + const deprecationReason = operation.operation.deprecated ? 'No longer supported' : undefined + // Get resolver and subscribe function for Subscription fields if (operation.operationType === GraphQLOperationType.Subscription) { const responseSchemaName = operation.responseDefinition @@ -977,6 +979,7 @@ function getFieldForOperation( resolve, subscribe, args, + deprecationReason, description: operation.description } @@ -996,6 +999,7 @@ function getFieldForOperation( type, resolve, args, + deprecationReason, description: operation.description } } diff --git a/packages/openapi-to-graphql/src/schema_builder.ts b/packages/openapi-to-graphql/src/schema_builder.ts index 591971d3..6b62cb64 100644 --- a/packages/openapi-to-graphql/src/schema_builder.ts +++ b/packages/openapi-to-graphql/src/schema_builder.ts @@ -709,7 +709,7 @@ function createFields({ type: requiredProperty ? new GraphQLNonNull(objectType as GraphQLOutputType) : (objectType as GraphQLOutputType), - + deprecationReason: fieldSchema?.deprecated ? 'No longer supported' : undefined, description: typeof fieldSchema === 'object' ? fieldSchema.description : null } @@ -1305,6 +1305,7 @@ export function getArgs({ args[saneName] = { type: paramRequired ? new GraphQLNonNull(type) : type, + deprecationReason: parameter.deprecated ? 'No longer supported' : undefined, description: parameter.description // Might be undefined } }) diff --git a/packages/openapi-to-graphql/src/types/oas3.ts b/packages/openapi-to-graphql/src/types/oas3.ts index f022b3d9..31c236c1 100644 --- a/packages/openapi-to-graphql/src/types/oas3.ts +++ b/packages/openapi-to-graphql/src/types/oas3.ts @@ -31,6 +31,7 @@ export type SchemaObject = { anyOf?: (SchemaObject | ReferenceObject)[] oneOf?: (SchemaObject | ReferenceObject)[] not?: (SchemaObject | ReferenceObject)[] + deprecated?: boolean } export type ReferenceObject = { diff --git a/packages/openapi-to-graphql/test/instagram.test.ts b/packages/openapi-to-graphql/test/instagram.test.ts index c701da06..34532527 100644 --- a/packages/openapi-to-graphql/test/instagram.test.ts +++ b/packages/openapi-to-graphql/test/instagram.test.ts @@ -34,3 +34,28 @@ test('All Instagram query endpoints present', () => { ).length expect(gqlTypes).toEqual(oasGetCount) }) + +test('Instagram deprecated directives test', () => { + const deprecatedOperations = [] + for (let path in oas.paths) { + for (let method in oas.paths[path]) { + const operation = oas.paths[path][method] + if (operation.deprecated) deprecatedOperations.push(operation) + } + } + const gqlQueryTypes = + ((createdSchema.getTypeMap().Query as GraphQLObjectType).getFields().viewerAnyAuth.type as GraphQLObjectType).getFields() + const gqlMutationTypes = + ((createdSchema.getTypeMap().Mutation as GraphQLObjectType).getFields().mutationViewerAnyAuth.type as GraphQLObjectType).getFields() + const gqlTypes = {...gqlQueryTypes, ...gqlMutationTypes} + const deprecatedTypes = [] + for (let type in gqlTypes) { + if (gqlTypes[type].deprecationReason) { + deprecatedTypes.push(gqlTypes[type]) + } + } + expect(deprecatedOperations.length).toEqual(deprecatedTypes.length) + const includesDescription = deprecatedOperations.map(operation => deprecatedTypes.some(type => type.description.includes(operation.description))) + const everyTrue = includesDescription.every(v => v) + expect(everyTrue).toEqual(true) +}) \ No newline at end of file diff --git a/packages/openapi-to-graphql/test/stripe.test.ts b/packages/openapi-to-graphql/test/stripe.test.ts index 286506e7..44d6e32b 100644 --- a/packages/openapi-to-graphql/test/stripe.test.ts +++ b/packages/openapi-to-graphql/test/stripe.test.ts @@ -35,3 +35,28 @@ test('All Stripe query endpoints present', () => { expect(gqlTypes).toEqual(oasGetCount) }) + +test('Stripe deprecated directives test', () => { + const deprecatedOperations = [] + for (let path in oas.paths) { + for (let method in oas.paths[path]) { + const operation = oas.paths[path][method] + if (operation.deprecated) deprecatedOperations.push(operation) + } + } + const gqlQueryTypes = + ((createdSchema.getTypeMap().Query as GraphQLObjectType).getFields().viewerAnyAuth.type as GraphQLObjectType).getFields() + const gqlMutationTypes = + ((createdSchema.getTypeMap().Mutation as GraphQLObjectType).getFields().mutationViewerAnyAuth.type as GraphQLObjectType).getFields() + const gqlTypes = {...gqlQueryTypes, ...gqlMutationTypes} + const deprecatedTypes = [] + for (let type in gqlTypes) { + if (gqlTypes[type].deprecationReason) { + deprecatedTypes.push(gqlTypes[type]) + } + } + expect(deprecatedOperations.length).toEqual(deprecatedTypes.length) + const includesDescription = deprecatedOperations.map(operation => deprecatedTypes.some(type => type.description.includes(operation.description))) + const everyTrue = includesDescription.every(v => v) + expect(everyTrue).toEqual(true) +})