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

Expose tags, original operation and preprocessing data #303

Merged
merged 1 commit into from
Mar 3, 2021
Merged
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
1,499 changes: 83 additions & 1,416 deletions packages/openapi-to-graphql-cli/package-lock.json

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions packages/openapi-to-graphql/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@
import { Options, Report } from './types/options';
import { Oas3 } from './types/oas3';
import { Oas2 } from './types/oas2';
import { PreprocessingData } from './types/preprocessing_data';
import { GraphQLSchema } from 'graphql';
declare type Result = {
declare type Result<TSource, TContext, TArgs> = {
schema: GraphQLSchema;
report: Report;
data: PreprocessingData<TSource, TContext, TArgs>;
};
/**
* Creates a GraphQL interface from the given OpenAPI Specification (2 or 3).
*/
export declare function createGraphQLSchema<TSource, TContext, TArgs>(spec: Oas3 | Oas2 | (Oas3 | Oas2)[], options?: Options<TSource, TContext, TArgs>): Promise<Result>;
export declare function createGraphQLSchema<TSource, TContext, TArgs>(spec: Oas3 | Oas2 | (Oas3 | Oas2)[], options?: Options<TSource, TContext, TArgs>): Promise<Result<TSource, TContext, TArgs>>;
export { sanitize, CaseStyle } from './oas_3_tools';
export { GraphQLOperationType } from './types/graphql';
2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/index.js.map

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions packages/openapi-to-graphql/lib/preprocessor.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/preprocessor.js.map

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion packages/openapi-to-graphql/lib/types/operation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Type definitions for the objects created during preprocessing for every
* operation in the OAS.
*/
import { Oas3, LinkObject, ParameterObject, ServerObject, SchemaObject } from './oas3';
import { Oas3, LinkObject, OperationObject, ParameterObject, ServerObject, SchemaObject } from './oas3';
import { GraphQLOperationType } from './graphql';
import { GraphQLScalarType, GraphQLObjectType, GraphQLInputObjectType, GraphQLList, GraphQLEnumType, GraphQLUnionType } from 'graphql';
import { HTTP_METHODS } from '../oas_3_tools';
Expand Down Expand Up @@ -40,6 +40,10 @@ export declare type DataDefinition = {
graphQLInputObjectType?: GraphQLInputObjectType | GraphQLList<any>;
};
export declare type Operation = {
/**
* The raw operation object from the OAS
*/
operation: OperationObject;
/**
* Identifier of the operation - may be created by concatenating method & path
*/
Expand All @@ -58,6 +62,10 @@ export declare type Operation = {
* Human-readable description of the operation
*/
description: string;
/**
* Tags of this operation
*/
tags: string[];
/**
* URL path of this operation
*/
Expand Down
9 changes: 5 additions & 4 deletions packages/openapi-to-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,10 @@ import debug from 'debug'
import { GraphQLSchemaConfig } from 'graphql/type/schema'
import { sortObject, handleWarning, MitigationTypes } from './utils'

type Result = {
type Result<TSource, TContext, TArgs> = {
schema: GraphQLSchema
report: Report
data: PreprocessingData<TSource, TContext, TArgs>
}

const translationLog = debug('translation')
Expand All @@ -82,7 +83,7 @@ const translationLog = debug('translation')
export function createGraphQLSchema<TSource, TContext, TArgs>(
spec: Oas3 | Oas2 | (Oas3 | Oas2)[],
options?: Options<TSource, TContext, TArgs>
): Promise<Result> {
): Promise<Result<TSource, TContext, TArgs>> {
return new Promise((resolve, reject) => {
if (typeof options === 'undefined') {
options = {}
Expand Down Expand Up @@ -224,7 +225,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
provideErrorExtensions,
equivalentToMessages
}: InternalOptions<TSource, TContext, TArgs>
): { schema: GraphQLSchema; report: Report } {
): Result<TSource, TContext, TArgs> {
const options = {
strict,
report,
Expand Down Expand Up @@ -630,7 +631,7 @@ function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(

const schema = new GraphQLSchema(schemaConfig)

return { schema, report: options.report }
return { schema, report: options.report, data }
}

/**
Expand Down
5 changes: 5 additions & 0 deletions packages/openapi-to-graphql/src/preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,9 @@ function processOperation<TSource, TContext, TArgs>(
description += `\n\nEquivalent to ${operationString}`
}

// Determine tags
const tags = operation.tags || []

// Hold on to the operationId
const operationId =
typeof operation.operationId !== 'undefined'
Expand Down Expand Up @@ -161,10 +164,12 @@ function processOperation<TSource, TContext, TArgs>(
securityRequirements.length > 0 && data.options.viewer !== false

return {
operation,
operationId,
operationString,
operationType,
description,
tags,
path,
method,
payloadContentType,
Expand Down
11 changes: 11 additions & 0 deletions packages/openapi-to-graphql/src/types/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import {
Oas3,
LinkObject,
OperationObject,
ParameterObject,
ServerObject,
SchemaObject
Expand Down Expand Up @@ -89,6 +90,11 @@ export type DataDefinition = {
}

export type Operation = {
/**
* The raw operation object from the OAS
*/
operation: OperationObject

/**
* Identifier of the operation - may be created by concatenating method & path
*/
Expand All @@ -110,6 +116,11 @@ export type Operation = {
*/
description: string

/**
* Tags of this operation
*/
tags: string[]

/**
* URL path of this operation
*/
Expand Down