graphql-2-json-schema
Advanced tools
Comparing version 0.1.0-1 to 0.1.0
@@ -5,2 +5,7 @@ # Change Log | ||
<a name="0.1.0"></a> | ||
# [0.1.0](https://github.com/wittydeveloper/graphql-to-json-schema/compare/v0.1.0-1...v0.1.0) (2018-12-29) | ||
<a name="0.1.0-1"></a> | ||
@@ -7,0 +12,0 @@ # [0.1.0-1](https://github.com/wittydeveloper/graphql-to-json-schema/compare/v0.1.0-0...v0.1.0-1) (2018-05-31) |
@@ -66,3 +66,16 @@ "use strict"; | ||
} | ||
else if (typeGuards_1.isIntrospectionEnumType(curr)) { | ||
acc[curr.name] = { | ||
type: 'string', | ||
anyOf: curr.enumValues.map(function (item) { | ||
return { | ||
"enum": [ | ||
item.name, | ||
], | ||
title: item.description || item.name | ||
}; | ||
}) | ||
}; | ||
} | ||
return acc; | ||
}; }; |
@@ -1,9 +0,9 @@ | ||
import { IntrospectionEnumType, IntrospectionField, IntrospectionInputObjectType, IntrospectionInputValue, IntrospectionListTypeRef, IntrospectionNamedTypeRef, IntrospectionNonNullTypeRef, IntrospectionObjectType, IntrospectionType } from 'graphql'; | ||
import { IntrospectionEnumType, IntrospectionField, IntrospectionInputObjectType, IntrospectionInputValue, IntrospectionListTypeRef, IntrospectionNamedTypeRef, IntrospectionNonNullTypeRef, IntrospectionObjectType, IntrospectionType, IntrospectionTypeRef } from 'graphql'; | ||
export declare const isIntrospectionField: (type: IntrospectionField | IntrospectionInputValue) => type is IntrospectionField; | ||
export declare const isIntrospectionInputValue: (type: IntrospectionField | IntrospectionInputValue) => type is IntrospectionInputValue; | ||
export declare const isIntrospectionListTypeRef: (type: any) => type is IntrospectionListTypeRef<any>; | ||
export declare const isIntrospectionListTypeRef: (type: IntrospectionNamedTypeRef<IntrospectionType> | IntrospectionListTypeRef<any> | IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<IntrospectionType> | IntrospectionListTypeRef<any>> | IntrospectionNamedTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionInputType> | IntrospectionNonNullTypeRef<IntrospectionListTypeRef<any> | IntrospectionNamedTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionInputType>> | IntrospectionNamedTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionOutputType> | IntrospectionNonNullTypeRef<IntrospectionListTypeRef<any> | IntrospectionNamedTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionOutputType>>) => type is IntrospectionListTypeRef<IntrospectionTypeRef>; | ||
export declare const isIntrospectionObjectType: (type: IntrospectionType) => type is IntrospectionObjectType; | ||
export declare const isIntrospectionInputObjectType: (type: IntrospectionType) => type is IntrospectionInputObjectType; | ||
export declare const isIntrospectionEnumType: (type: IntrospectionType) => type is IntrospectionEnumType; | ||
export declare const isNonNullIntrospectionType: (type: any) => type is IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<IntrospectionType>>; | ||
export declare const isNonNullIntrospectionType: (type: IntrospectionTypeRef) => type is IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<IntrospectionType>>; | ||
export interface FilterDefinitionsTypesOptions { | ||
@@ -10,0 +10,0 @@ ignoreInternals?: boolean; |
@@ -18,4 +18,5 @@ "use strict"; | ||
return lodash_1.filter(types, function (type) { return ((exports.isIntrospectionObjectType(type) && !!type.fields) || | ||
(exports.isIntrospectionInputObjectType(type) && !!type.inputFields)) && | ||
(exports.isIntrospectionInputObjectType(type) && !!type.inputFields) || | ||
(exports.isIntrospectionEnumType(type) && !!type.enumValues)) && | ||
(!ignoreInternals || (ignoreInternals && !lodash_1.startsWith(type.name, '__'))); }); | ||
}; |
@@ -1,2 +0,2 @@ | ||
import { IntrospectionInputTypeRef, IntrospectionOutputTypeRef, IntrospectionTypeRef } from 'graphql'; | ||
import { IntrospectionInputType, IntrospectionInputTypeRef, IntrospectionNamedTypeRef, IntrospectionOutputType, IntrospectionOutputTypeRef, IntrospectionTypeRef } from 'graphql'; | ||
import { JSONSchema6, JSONSchema6TypeName } from 'json-schema'; | ||
@@ -8,2 +8,2 @@ export declare type GraphQLTypeNames = 'String' | 'Int' | 'Float' | 'Boolean'; | ||
export declare type GraphqlToJSONTypeArg = IntrospectionTypeRef | IntrospectionInputTypeRef | IntrospectionOutputTypeRef; | ||
export declare const graphqlToJSONType: (k: any) => JSONSchema6; | ||
export declare const graphqlToJSONType: (k: IntrospectionNamedTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionType> | import("graphql/utilities/introspectionQuery").IntrospectionListTypeRef<any> | import("graphql/utilities/introspectionQuery").IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionType> | import("graphql/utilities/introspectionQuery").IntrospectionListTypeRef<any>> | IntrospectionNamedTypeRef<IntrospectionInputType> | import("graphql/utilities/introspectionQuery").IntrospectionNonNullTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionListTypeRef<any> | IntrospectionNamedTypeRef<IntrospectionInputType>> | IntrospectionNamedTypeRef<IntrospectionOutputType> | import("graphql/utilities/introspectionQuery").IntrospectionNonNullTypeRef<import("graphql/utilities/introspectionQuery").IntrospectionListTypeRef<any> | IntrospectionNamedTypeRef<IntrospectionOutputType>>) => JSONSchema6; |
@@ -23,3 +23,3 @@ "use strict"; | ||
var name_1 = k.name; | ||
return lodash_1.includes(['OBJECT', 'INPUT_OBJECT'], k.kind) ? | ||
return lodash_1.includes(['OBJECT', 'INPUT_OBJECT', 'ENUM'], k.kind) ? | ||
{ $ref: "#/definitions/" + name_1 } : | ||
@@ -26,0 +26,0 @@ { type: exports.typesMapping[name_1] }; |
import { GraphQLSchema, IntrospectionQuery } from 'graphql'; | ||
import { JSONSchema6 } from 'json-schema'; | ||
export declare const getTodoSchemaIntrospection: () => { | ||
declare type GetTodoSchemaIntrospectionResult = { | ||
schema: GraphQLSchema; | ||
introspection: IntrospectionQuery; | ||
}; | ||
export declare const getTodoSchemaIntrospection: () => GetTodoSchemaIntrospectionResult; | ||
export declare const todoSchemaAsJsonSchema: JSONSchema6; | ||
export {}; |
@@ -5,3 +5,3 @@ "use strict"; | ||
exports.getTodoSchemaIntrospection = function () { | ||
var schema = graphql_1.buildSchema("\n type Todo {\n id: String!\n name: String!\n completed: Boolean\n }\n\n input TodoInputType {\n name: String!\n completed: Boolean\n }\n\n type Query {\n todo(id: String!): Todo!\n todos: [Todo!]!\n }\n\n type Mutation {\n update_todo(id: String!, todo: TodoInputType!): Todo\n create_todo(todo: TodoInputType!): Todo\n }\n"); | ||
var schema = graphql_1.buildSchema("\n type Todo {\n id: String!\n name: String!\n completed: Boolean\n color: Color\n }\n\n input TodoInputType {\n name: String!\n completed: Boolean\n color: Color\n }\n \n enum Color {\n \"Red color\"\n RED\n \"Green color\"\n GREEN\n }\n\n type Query {\n todo(id: String!): Todo!\n todos: [Todo!]!\n }\n\n type Mutation {\n update_todo(id: String!, todo: TodoInputType!): Todo\n create_todo(todo: TodoInputType!): Todo\n }\n"); | ||
var result = graphql_1.graphqlSync(schema, graphql_1.introspectionQuery); | ||
@@ -98,6 +98,20 @@ return { | ||
name: { type: 'string' }, | ||
completed: { type: 'boolean' } | ||
completed: { type: 'boolean' }, | ||
color: { $ref: '#/definitions/Color' } | ||
}, | ||
required: ['id', 'name'] | ||
}, | ||
'Color': { | ||
type: 'string', | ||
anyOf: [ | ||
{ | ||
"enum": ['RED'], | ||
title: 'Red color' | ||
}, | ||
{ | ||
"enum": ['GREEN'], | ||
title: 'Green color' | ||
} | ||
] | ||
}, | ||
'TodoInputType': { | ||
@@ -107,3 +121,4 @@ type: 'object', | ||
name: { type: 'string' }, | ||
completed: { type: 'boolean' } | ||
completed: { type: 'boolean' }, | ||
color: { $ref: '#/definitions/Color' } | ||
}, | ||
@@ -110,0 +125,0 @@ required: ['name'] |
@@ -7,2 +7,3 @@ import { IntrospectionQuery, IntrospectionType } from 'graphql'; | ||
// FIXME: finish this type | ||
export interface GraphQLJSONSchema6 extends JSONSchema6 { | ||
@@ -17,3 +18,3 @@ properties: { | ||
export interface FromIntrospectionQueryOptions { | ||
ignoreInternals?: boolean; | ||
ignoreInternals?: boolean; // true by default | ||
} | ||
@@ -27,3 +28,5 @@ export const fromIntrospectionQuery = ( | ||
const propertiesTypes = [queryType ? queryType.name : 'Query', mutationType ? mutationType.name : 'Mutation']; | ||
// Query and Mutation are properties, others are definitions | ||
////////////////////////////////////////////////////////////////////// | ||
//// Query and Mutation are properties, custom Types are definitions | ||
////////////////////////////////////////////////////////////////////// | ||
const [properties, definitions] = partition( | ||
@@ -30,0 +33,0 @@ introspection.__schema.types, |
@@ -7,2 +7,3 @@ import { types } from 'functional-json-schema'; | ||
import { | ||
isIntrospectionEnumType, | ||
isIntrospectionField, | ||
@@ -21,2 +22,3 @@ isIntrospectionInputObjectType, | ||
// Extract GraphQL no-nullable types | ||
type GetRequiredFieldsType = ReadonlyArray<IntrospectionInputValue | IntrospectionField>; | ||
@@ -33,2 +35,3 @@ export const getRequiredFields = (fields: GetRequiredFieldsType) => map( | ||
// reducer for a queries/mutations | ||
export const propertiesIntrospectionFieldReducer: | ||
@@ -66,2 +69,3 @@ MemoListIterator<IntrospectionFieldReducerItem, JSONSchema6Acc, ReadonlyArray<IntrospectionFieldReducerItem>> = | ||
// reducer for a custom types | ||
export const definitionsIntrospectionFieldReducer: | ||
@@ -86,2 +90,3 @@ MemoListIterator<IntrospectionFieldReducerItem, JSONSchema6Acc, ReadonlyArray<IntrospectionFieldReducerItem>> = | ||
// Reducer for each type exposed by the GraphQL Schema | ||
export const introspectionTypeReducer: | ||
@@ -100,2 +105,3 @@ (type: 'definitions' | 'properties') => MemoListIterator<IntrospectionType, JSONSchema6Acc, IntrospectionType[]> = | ||
), | ||
// ignore required for Mutations/Queries | ||
required: type === 'definitions' ? getRequiredFields(curr.fields) : [] | ||
@@ -111,4 +117,16 @@ }; | ||
}; | ||
} else if (isIntrospectionEnumType(curr)) { | ||
acc[curr.name] = { | ||
type: 'string', | ||
anyOf: curr.enumValues.map((item) => { | ||
return { | ||
enum: [ | ||
item.name, | ||
], | ||
title: item.description || item.name, | ||
}; | ||
}), | ||
}; | ||
} | ||
return acc; | ||
}; |
@@ -18,2 +18,6 @@ import { | ||
/////////////////// | ||
/// Type guards /// | ||
/////////////////// | ||
export const isIntrospectionField = (type: IntrospectionField | IntrospectionInputValue): type is IntrospectionField => | ||
@@ -51,2 +55,3 @@ has(type, 'args'); | ||
// Ignore all GraphQL native Scalars, directives, etc... | ||
export interface FilterDefinitionsTypesOptions { ignoreInternals?: boolean; } | ||
@@ -60,3 +65,4 @@ export const filterDefinitionsTypes = | ||
(isIntrospectionObjectType(type) && !!type.fields) || | ||
(isIntrospectionInputObjectType(type) && !!type.inputFields) | ||
(isIntrospectionInputObjectType(type) && !!type.inputFields) || | ||
(isIntrospectionEnumType(type) && !!type.enumValues) | ||
) && | ||
@@ -63,0 +69,0 @@ (!ignoreInternals || (ignoreInternals && !startsWith(type.name, '__'))) |
@@ -29,2 +29,3 @@ import { | ||
// Convert a GraphQL Type to a valid JSON Schema type | ||
export type GraphqlToJSONTypeArg = IntrospectionTypeRef | IntrospectionInputTypeRef | IntrospectionOutputTypeRef; | ||
@@ -41,3 +42,3 @@ export const graphqlToJSONType = (k: GraphqlToJSONTypeArg): JSONSchema6 => { | ||
const name = (k as IntrospectionNamedTypeRef<IntrospectionInputType | IntrospectionOutputType>).name; | ||
return includes(['OBJECT', 'INPUT_OBJECT'], k.kind) ? | ||
return includes(['OBJECT', 'INPUT_OBJECT', 'ENUM'], k.kind) ? | ||
{ $ref: `#/definitions/${name}` } : | ||
@@ -44,0 +45,0 @@ // tslint:disable-next-line:no-any |
{ | ||
"name": "graphql-2-json-schema", | ||
"version": "0.1.0-1", | ||
"version": "0.1.0", | ||
"main": "dist/index.js", | ||
@@ -34,3 +34,3 @@ "repository": "git@github.com:wittydeveloper/graphql-to-json-schema.git", | ||
}, | ||
"testRegex": "(/__tests__/.*|(\\.|/)(test|spec))\\.(jsx?)$", | ||
"testRegex": "(/__tests__/([^\\.d]*)|(\\.|/)(test|spec))\\.(jsx?|tsx?)$", | ||
"moduleFileExtensions": [ | ||
@@ -37,0 +37,0 @@ "ts", |
@@ -1,3 +0,7 @@ | ||
# GraphQL Schema to JSON Schema (`graphql-2-json-schema`) [![npm version](https://badge.fury.io/js/graphql-2-json-schema.svg)](https://badge.fury.io/js/graphql-2-json-schema) | ||
# GraphQL Schema to JSON Schema [![npm version](https://badge.fury.io/js/graphql-2-json-schema.svg)](https://badge.fury.io/js/graphql-2-json-schema) | ||
`graphql-2-json-schema` package | ||
----------- | ||
Transform a GraphQL Schema introspection file to a valid JSON Schema. | ||
@@ -32,2 +36,3 @@ | ||
completed: Boolean | ||
color: Color | ||
} | ||
@@ -38,4 +43,12 @@ | ||
completed: Boolean | ||
color: Color | ||
} | ||
enum Color { | ||
"Red color" | ||
RED | ||
"Green color" | ||
GREEN | ||
} | ||
type Query { | ||
@@ -141,5 +154,19 @@ todo(id: String!): Todo | ||
completed: { type: 'boolean' }, | ||
color: { $ref: '#/definitions/Color' }, | ||
}, | ||
required: ['id', 'name'] | ||
}, | ||
'Color': { | ||
type: 'string', | ||
anyOf: [ | ||
{ | ||
enum: ['RED'], | ||
title: 'Red color', | ||
}, | ||
{ | ||
enum: ['GREEN'], | ||
title: 'Green color', | ||
} | ||
] | ||
}, | ||
'TodoInputType': { | ||
@@ -150,2 +177,3 @@ type: 'object', | ||
completed: { type: 'boolean' }, | ||
color: { $ref: '#/definitions/Color' }, | ||
}, | ||
@@ -156,2 +184,2 @@ required: ['name'] | ||
} | ||
``` | ||
``` |
@@ -20,2 +20,3 @@ import { | ||
completed: Boolean | ||
color: Color | ||
} | ||
@@ -26,3 +27,11 @@ | ||
completed: Boolean | ||
color: Color | ||
} | ||
enum Color { | ||
"Red color" | ||
RED | ||
"Green color" | ||
GREEN | ||
} | ||
@@ -133,5 +142,19 @@ type Query { | ||
completed: { type: 'boolean' }, | ||
color: { $ref: '#/definitions/Color' }, | ||
}, | ||
required: ['id', 'name'] | ||
}, | ||
'Color': { | ||
type: 'string', | ||
anyOf: [ | ||
{ | ||
enum: ['RED'], | ||
title: 'Red color', | ||
}, | ||
{ | ||
enum: ['GREEN'], | ||
title: 'Green color', | ||
} | ||
] | ||
}, | ||
'TodoInputType': { | ||
@@ -142,2 +165,3 @@ type: 'object', | ||
completed: { type: 'boolean' }, | ||
color: { $ref: '#/definitions/Color' }, | ||
}, | ||
@@ -144,0 +168,0 @@ required: ['name'] |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
41780
787
181
27