Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add supports for deprecated #499

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/openapi-to-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -951,6 +951,8 @@ function getFieldForOperation<TSource, TContext, TArgs extends object>(
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
Expand All @@ -977,6 +979,7 @@ function getFieldForOperation<TSource, TContext, TArgs extends object>(
resolve,
subscribe,
args,
deprecationReason,
description: operation.description
}

Expand All @@ -996,6 +999,7 @@ function getFieldForOperation<TSource, TContext, TArgs extends object>(
type,
resolve,
args,
deprecationReason,
description: operation.description
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-to-graphql/src/schema_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ function createFields<TSource, TContext, TArgs extends object>({
type: requiredProperty
? new GraphQLNonNull(objectType as GraphQLOutputType)
: (objectType as GraphQLOutputType),

deprecationReason: fieldSchema?.deprecated ? 'No longer supported' : undefined,
description:
typeof fieldSchema === 'object' ? fieldSchema.description : null
}
Expand Down Expand Up @@ -1305,6 +1305,7 @@ export function getArgs<TSource, TContext, TArgs extends object>({

args[saneName] = {
type: paramRequired ? new GraphQLNonNull(type) : type,
deprecationReason: parameter.deprecated ? 'No longer supported' : undefined,
description: parameter.description // Might be undefined
}
})
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-to-graphql/src/types/oas3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export type SchemaObject = {
anyOf?: (SchemaObject | ReferenceObject)[]
oneOf?: (SchemaObject | ReferenceObject)[]
not?: (SchemaObject | ReferenceObject)[]
deprecated?: boolean
}

export type ReferenceObject = {
Expand Down
25 changes: 25 additions & 0 deletions packages/openapi-to-graphql/test/instagram.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
25 changes: 25 additions & 0 deletions packages/openapi-to-graphql/test/stripe.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})