graphql-js-tree
Advanced tools
Comparing version 0.4.0 to 1.0.0
@@ -16,8 +16,15 @@ "use strict"; | ||
const composeDefinition = (d) => { | ||
var _a, _b; | ||
const node = nodes.find((n) => { var _a; return (_a = n.type.operations) === null || _a === void 0 ? void 0 : _a.includes(d.operation); }); | ||
var _a, _b, _c, _d; | ||
const schemaNode = nodes.find((n) => n.data.type === Models_1.TypeSystemDefinition.SchemaDefinition); | ||
const operationField = schemaNode === null || schemaNode === void 0 ? void 0 : schemaNode.args.find((a) => a.name === d.operation); | ||
if (!operationField) { | ||
console.log(JSON.stringify(schemaNode, null, 2)); | ||
throw new Error(`Operation ${(_a = d.name) === null || _a === void 0 ? void 0 : _a.value} does not exist in schema`); | ||
} | ||
const operationType = (0, shared_1.getTypeName)(operationField.type.fieldType); | ||
const node = nodes.find((n) => n.name === operationType); | ||
if (!node) { | ||
throw new Error(`Operation ${d.name} does not exist in schema`); | ||
throw new Error(`Operation ${(_b = d.name) === null || _b === void 0 ? void 0 : _b.value} does not exist in schema`); | ||
} | ||
return Object.assign(Object.assign(Object.assign({ name: (_a = d.name) === null || _a === void 0 ? void 0 : _a.value, node }, (d.operation | ||
return Object.assign(Object.assign(Object.assign({ name: (_c = d.name) === null || _c === void 0 ? void 0 : _c.value, node }, (d.operation | ||
? { | ||
@@ -30,3 +37,3 @@ operation: d.operation === 'query' | ||
} | ||
: {})), (((_b = d.variableDefinitions) === null || _b === void 0 ? void 0 : _b.length) | ||
: {})), (((_d = d.variableDefinitions) === null || _d === void 0 ? void 0 : _d.length) | ||
? { | ||
@@ -33,0 +40,0 @@ variableDefinitions: d.variableDefinitions.map((vd) => composeVariableDefinition(vd)), |
@@ -1,2 +0,2 @@ | ||
import { Directive, OperationType, FieldType, Value } from './Spec'; | ||
import { Directive, FieldType, Value } from './Spec'; | ||
import { GraphQLNodeParams } from './Types'; | ||
@@ -8,3 +8,2 @@ export interface ParserField { | ||
fieldType: FieldType; | ||
operations?: OperationType[]; | ||
directiveOptions?: Directive[]; | ||
@@ -11,0 +10,0 @@ }; |
import { TypeDefinitionDisplayStrings, TypeSystemDefinitionDisplayStrings } from './DisplayMap'; | ||
import { Helpers, Instances, ScalarTypes, Type, TypeDefinition, TypeExtension, TypeSystemDefinition, Value, ValueDefinition } from './Spec'; | ||
import { Helpers, Instances, ScalarTypes, Type, TypeDefinition, TypeExtension, TypeSystemDefinition, TypeSystemExtension, Value, ValueDefinition } from './Spec'; | ||
export declare enum BuiltInDirectives { | ||
@@ -8,3 +8,3 @@ skip = "skip", | ||
} | ||
export type AllTypes = ScalarTypes | Value | ValueDefinition | TypeDefinition | TypeDefinitionDisplayStrings | TypeSystemDefinition | TypeSystemDefinitionDisplayStrings | TypeExtension | Instances | Helpers | Type; | ||
export type AllTypes = ScalarTypes | Value | ValueDefinition | TypeDefinition | TypeDefinitionDisplayStrings | TypeSystemDefinition | TypeSystemDefinitionDisplayStrings | TypeExtension | TypeSystemExtension | Instances | Helpers | Type; | ||
export declare const kindAsAllTypes: (v: string) => AllTypes; | ||
@@ -11,0 +11,0 @@ export declare const kindAsValue: (v: string) => Value; |
@@ -1,2 +0,2 @@ | ||
import { DefinitionNode, GraphQLSchema } from 'graphql'; | ||
import { GraphQLSchema, SchemaDefinitionNode, SchemaExtensionNode, TypeDefinitionNode, TypeSystemDefinitionNode } from 'graphql'; | ||
import { ParserField, ParserTree } from "../Models"; | ||
@@ -6,3 +6,3 @@ export declare class Parser { | ||
static importSchema: (schema: string) => GraphQLSchema; | ||
static documentDefinitionToSerializedNodeTree: (d: DefinitionNode) => ParserField | undefined; | ||
static documentDefinitionToSerializedNodeTree: (d: TypeSystemDefinitionNode | TypeDefinitionNode | SchemaDefinitionNode | SchemaExtensionNode) => ParserField | undefined; | ||
static parse: (schema: string, excludeRoots?: string[], libraries?: string) => ParserTree; | ||
@@ -9,0 +9,0 @@ static parseAddExtensions: (schema: string, excludeRoots?: string[]) => ParserTree; |
@@ -39,25 +39,81 @@ "use strict"; | ||
Parser.documentDefinitionToSerializedNodeTree = (d) => { | ||
var _a; | ||
var _a, _b; | ||
if ((0, graphql_1.isTypeSystemDefinitionNode)(d) || (0, graphql_1.isTypeSystemExtensionNode)(d)) { | ||
const args = typeResolver_1.TypeResolver.resolveFieldsFromDefinition(d); | ||
if ('name' in d) { | ||
const interfaces = 'interfaces' in d && d.interfaces ? d.interfaces.map((i) => i.name.value) : []; | ||
const directives = 'directives' in d && d.directives ? typeResolver_1.TypeResolver.iterateDirectives(d.directives) : []; | ||
return Object.assign(Object.assign({ name: d.name.value, type: d.kind === 'DirectiveDefinition' | ||
? { | ||
fieldType: { name: Models_1.TypeDefinitionDisplayMap[d.kind], type: Models_1.Options.name }, | ||
directiveOptions: d.locations.map((l) => l.value), | ||
} | ||
: { | ||
fieldType: { name: Models_1.TypeDefinitionDisplayMap[d.kind], type: Models_1.Options.name }, | ||
}, data: { | ||
type: (0, Models_1.kindAsAllTypes)(d.kind), | ||
} }, ('description' in d && ((_a = d.description) === null || _a === void 0 ? void 0 : _a.value) ? { description: d.description.value } : {})), { interfaces, | ||
directives, | ||
args, id: (0, shared_1.generateNodeId)(d.name.value, (0, Models_1.kindAsAllTypes)(d.kind), args) }); | ||
const interfaces = 'interfaces' in d && d.interfaces ? d.interfaces.map((i) => i.name.value) : []; | ||
const directives = 'directives' in d && d.directives ? typeResolver_1.TypeResolver.iterateDirectives(d.directives) : []; | ||
if (d.kind === 'SchemaDefinition') { | ||
return { | ||
name: 'schema', | ||
args: d.operationTypes.map((ot) => (0, shared_1.createParserField)({ | ||
name: ot.operation, | ||
data: { | ||
type: Spec_1.TypeSystemDefinition.FieldDefinition, | ||
}, | ||
type: { | ||
fieldType: { | ||
name: ot.type.name.value, | ||
type: Models_1.Options.name, | ||
}, | ||
}, | ||
})), | ||
data: { | ||
type: Spec_1.TypeSystemDefinition.SchemaDefinition, | ||
}, | ||
directives: d.directives ? typeResolver_1.TypeResolver.iterateDirectives(d.directives) : [], | ||
id: (0, shared_1.generateNodeId)('schema', (0, Models_1.kindAsAllTypes)(d.kind), []), | ||
interfaces: [], | ||
type: { | ||
fieldType: { | ||
type: Models_1.Options.name, | ||
name: 'schema', | ||
}, | ||
}, | ||
}; | ||
} | ||
if (d.kind === 'SchemaExtension') { | ||
return { | ||
name: 'schema', | ||
data: { | ||
type: Spec_1.TypeSystemExtension.SchemaExtension, | ||
}, | ||
directives: d.directives ? typeResolver_1.TypeResolver.iterateDirectives(d.directives) : [], | ||
interfaces: [], | ||
type: { | ||
fieldType: { | ||
type: Models_1.Options.name, | ||
name: 'schema', | ||
}, | ||
}, | ||
args: ((_a = d.operationTypes) === null || _a === void 0 ? void 0 : _a.map((ot) => (0, shared_1.createParserField)({ | ||
name: ot.operation, | ||
data: { | ||
type: Spec_1.TypeSystemDefinition.FieldDefinition, | ||
}, | ||
type: { | ||
fieldType: { | ||
name: ot.type.name.value, | ||
type: Models_1.Options.name, | ||
}, | ||
}, | ||
}))) || [], | ||
id: (0, shared_1.generateNodeId)('schema', (0, Models_1.kindAsAllTypes)(d.kind), []), | ||
}; | ||
} | ||
return Object.assign(Object.assign({ name: d.name.value, type: d.kind === 'DirectiveDefinition' | ||
? { | ||
fieldType: { name: Models_1.TypeDefinitionDisplayMap[d.kind], type: Models_1.Options.name }, | ||
directiveOptions: d.locations.map((l) => l.value), | ||
} | ||
: { | ||
fieldType: { name: Models_1.TypeDefinitionDisplayMap[d.kind], type: Models_1.Options.name }, | ||
}, data: { | ||
type: (0, Models_1.kindAsAllTypes)(d.kind), | ||
} }, ('description' in d && ((_b = d.description) === null || _b === void 0 ? void 0 : _b.value) ? { description: d.description.value } : {})), { interfaces, | ||
directives, | ||
args, id: (0, shared_1.generateNodeId)(d.name.value, (0, Models_1.kindAsAllTypes)(d.kind), args) }); | ||
} | ||
}; | ||
Parser.parse = (schema, excludeRoots = [], libraries = '') => { | ||
var _a; | ||
var _a, _b, _c; | ||
let parsedSchema; | ||
@@ -80,20 +136,8 @@ const compiledSchema = [libraries, schema].join('\n'); | ||
} | ||
const operations = {}; | ||
const schemaDefinition = parsedSchema.definitions.find((d) => d.kind === 'SchemaDefinition'); | ||
if (schemaDefinition && 'operationTypes' in schemaDefinition) { | ||
(_a = schemaDefinition.operationTypes) === null || _a === void 0 ? void 0 : _a.forEach((ot) => { | ||
if (ot.operation === 'query') { | ||
operations.Query = ot.type.name.value; | ||
} | ||
if (ot.operation === 'mutation') { | ||
operations.Mutation = ot.type.name.value; | ||
} | ||
if (ot.operation === 'subscription') { | ||
operations.Subscription = ot.type.name.value; | ||
} | ||
}); | ||
} | ||
const nodes = parsedSchema.definitions | ||
.filter((t) => 'name' in t && t.name && !excludeRoots.includes(t.name.value)) | ||
.map(Parser.documentDefinitionToSerializedNodeTree) | ||
.filter((t) => t.kind === 'SchemaExtension' || t.kind === 'SchemaDefinition' | ||
? true | ||
: 'name' in t && t.name && !excludeRoots.includes(t.name.value)) | ||
.filter((t) => t.kind !== 'FragmentDefinition') | ||
.map((t) => Parser.documentDefinitionToSerializedNodeTree(t)) | ||
.filter((d) => !!d); | ||
@@ -118,13 +162,2 @@ const comments = Parser.findComments(schema).map((description) => (0, shared_1.createParserField)({ | ||
nodeTree.nodes.forEach((n) => { | ||
if (n.data.type === Spec_1.TypeDefinition.ObjectTypeDefinition) { | ||
if (operations.Query ? operations.Query === n.name : n.name === 'Query') { | ||
n.type.operations = [Spec_1.OperationType.query]; | ||
} | ||
if (operations.Mutation ? operations.Mutation === n.name : n.name === 'Mutation') { | ||
n.type.operations = [Spec_1.OperationType.mutation]; | ||
} | ||
if (operations.Subscription ? operations.Subscription === n.name : n.name === 'Subscription') { | ||
n.type.operations = [Spec_1.OperationType.subscription]; | ||
} | ||
} | ||
if (n.data.type === Spec_1.TypeDefinition.ObjectTypeDefinition || | ||
@@ -150,2 +183,17 @@ n.data.type === Spec_1.TypeDefinition.InterfaceTypeDefinition) { | ||
}); | ||
const schemaNode = nodeTree.nodes.find((n) => n.data.type === Spec_1.TypeSystemDefinition.SchemaDefinition); | ||
if (!schemaNode) { | ||
const query = (_a = nodeTree.nodes.find((n) => n.name === 'Query')) === null || _a === void 0 ? void 0 : _a.name; | ||
const mutation = (_b = nodeTree.nodes.find((n) => n.name === 'Mutation')) === null || _b === void 0 ? void 0 : _b.name; | ||
const subscription = (_c = nodeTree.nodes.find((n) => n.name === 'Subscription')) === null || _c === void 0 ? void 0 : _c.name; | ||
if (query || mutation || subscription) { | ||
nodeTree.nodes.push((0, shared_1.createSchemaDefinition)({ | ||
operations: { | ||
query, | ||
mutation, | ||
subscription, | ||
}, | ||
})); | ||
} | ||
} | ||
return nodeTree; | ||
@@ -152,0 +200,0 @@ }; |
@@ -1,2 +0,2 @@ | ||
import { ArgumentNode, DirectiveNode, FieldDefinitionNode, InputValueDefinitionNode, ObjectFieldNode, TypeDefinitionNode, TypeNode, TypeSystemDefinitionNode, TypeSystemExtensionNode, ValueNode } from 'graphql'; | ||
import { ArgumentNode, DirectiveNode, FieldDefinitionNode, InputValueDefinitionNode, ObjectFieldNode, SchemaDefinitionNode, TypeDefinitionNode, TypeNode, TypeSystemDefinitionNode, TypeSystemExtensionNode, ValueNode } from 'graphql'; | ||
import { ParserField } from "../Models"; | ||
@@ -13,3 +13,3 @@ export declare class TypeResolver { | ||
static resolveFields(n: TypeDefinitionNode): ParserField[] | undefined; | ||
static resolveFieldsFromDefinition(n: TypeSystemDefinitionNode | TypeSystemExtensionNode): ParserField[]; | ||
static resolveFieldsFromDefinition(n: TypeSystemDefinitionNode | TypeSystemExtensionNode | SchemaDefinitionNode): ParserField[]; | ||
} |
@@ -1,2 +0,2 @@ | ||
import { AllTypes, Directive, FieldType, ParserField, TypeDefinition, TypeExtension } from "../Models"; | ||
import { AllTypes, Directive, FieldType, OperationType, ParserField, TypeDefinition, TypeExtension } from "../Models"; | ||
export declare const getTypeName: (f: FieldType) => string; | ||
@@ -38,2 +38,12 @@ export declare const compileType: (f: FieldType) => string; | ||
export declare const compareParserFields: (f1: ParserField) => (f2: ParserField) => boolean; | ||
type SchemaCreationProps = { | ||
operations?: { | ||
[OperationType.query]?: string; | ||
[OperationType.mutation]?: string; | ||
[OperationType.subscription]?: string; | ||
}; | ||
directives?: ParserField[]; | ||
}; | ||
export declare const createSchemaDefinition: (options: SchemaCreationProps) => ParserField; | ||
export declare const createSchemaExtension: (options: SchemaCreationProps) => ParserField; | ||
export {}; |
@@ -14,3 +14,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.compareParserFields = exports.createPlainArgument = exports.createPlainDirectiveImplementation = exports.createUnionMember = exports.createPlainEnumValue = exports.createPlainInputValue = exports.createTypeNameField = exports.createPlainField = exports.createRootDirectiveField = exports.createRootExtensionField = exports.createRootField = exports.createParserField = exports.generateNodeId = exports.decompileType = exports.compileType = exports.getTypeName = void 0; | ||
exports.createSchemaExtension = exports.createSchemaDefinition = exports.compareParserFields = exports.createPlainArgument = exports.createPlainDirectiveImplementation = exports.createUnionMember = exports.createPlainEnumValue = exports.createPlainInputValue = exports.createTypeNameField = exports.createPlainField = exports.createRootDirectiveField = exports.createRootExtensionField = exports.createRootField = exports.createParserField = exports.generateNodeId = exports.decompileType = exports.compileType = exports.getTypeName = void 0; | ||
const Models_1 = require("../Models"); | ||
@@ -206,1 +206,61 @@ const shared_1 = require("../TreeOperations/shared"); | ||
exports.compareParserFields = compareParserFields; | ||
const createSchemaDefinition = (options) => { | ||
return (0, exports.createParserField)({ | ||
name: 'schema', | ||
data: { | ||
type: Models_1.TypeSystemDefinition.SchemaDefinition, | ||
}, | ||
type: { | ||
fieldType: { | ||
type: Models_1.Options.name, | ||
name: 'schema', | ||
}, | ||
}, | ||
directives: (options === null || options === void 0 ? void 0 : options.directives) || [], | ||
args: (options === null || options === void 0 ? void 0 : options.operations) | ||
? Object.entries(options === null || options === void 0 ? void 0 : options.operations).map(([k, v]) => (0, exports.createParserField)({ | ||
name: k, | ||
data: { | ||
type: Models_1.TypeSystemDefinition.FieldDefinition, | ||
}, | ||
type: { | ||
fieldType: { | ||
type: Models_1.Options.name, | ||
name: v, | ||
}, | ||
}, | ||
})) | ||
: [], | ||
}); | ||
}; | ||
exports.createSchemaDefinition = createSchemaDefinition; | ||
const createSchemaExtension = (options) => { | ||
return (0, exports.createParserField)({ | ||
name: 'schema', | ||
data: { | ||
type: Models_1.TypeSystemExtension.SchemaExtension, | ||
}, | ||
directives: (options === null || options === void 0 ? void 0 : options.directives) || [], | ||
type: { | ||
fieldType: { | ||
type: Models_1.Options.name, | ||
name: 'schema', | ||
}, | ||
}, | ||
args: (options === null || options === void 0 ? void 0 : options.operations) | ||
? Object.entries(options === null || options === void 0 ? void 0 : options.operations).map(([k, v]) => (0, exports.createParserField)({ | ||
name: k, | ||
data: { | ||
type: Models_1.TypeSystemDefinition.FieldDefinition, | ||
}, | ||
type: { | ||
fieldType: { | ||
type: Models_1.Options.name, | ||
name: v, | ||
}, | ||
}, | ||
})) | ||
: [], | ||
}); | ||
}; | ||
exports.createSchemaExtension = createSchemaExtension; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.TreeToGraphQL = void 0; | ||
const Models_1 = require("../Models"); | ||
const TemplateUtils_1 = require("./templates/TemplateUtils"); | ||
@@ -10,30 +9,5 @@ class TreeToGraphQL { | ||
const alldefs = parserTree.nodes.map((a) => TemplateUtils_1.TemplateUtils.resolverForConnection(a)); | ||
const schemaOperations = { | ||
[Models_1.OperationType.query]: null, | ||
[Models_1.OperationType.mutation]: null, | ||
[Models_1.OperationType.subscription]: null, | ||
}; | ||
parserTree.nodes.forEach((n) => { | ||
const { operations } = n.type; | ||
if (operations && operations.length > 0) { | ||
if (operations.find((o) => o === Models_1.OperationType.query)) { | ||
schemaOperations[Models_1.OperationType.query] = n.name; | ||
} | ||
if (operations.find((o) => o === Models_1.OperationType.mutation)) { | ||
schemaOperations[Models_1.OperationType.mutation] = n.name; | ||
} | ||
if (operations.find((o) => o === Models_1.OperationType.subscription)) { | ||
schemaOperations[Models_1.OperationType.subscription] = n.name; | ||
} | ||
} | ||
}); | ||
const resolvedOperations = Object.keys(schemaOperations) | ||
.filter((k) => schemaOperations[k]) | ||
.map((k) => `\t${k}: ${schemaOperations[k]}`) | ||
.join(',\n'); | ||
return joinDefinitions(...alldefs) | ||
.concat('\n') | ||
.concat(schemaOperations[Models_1.OperationType.query] ? `schema{\n${resolvedOperations}\n}` : ''); | ||
return joinDefinitions(...alldefs).concat('\n'); | ||
} | ||
} | ||
exports.TreeToGraphQL = TreeToGraphQL; |
@@ -15,2 +15,3 @@ "use strict"; | ||
const UnionMemberTemplate_1 = require("./UnionMemberTemplate"); | ||
const SchemaDefinitionTemplate_1 = require("./SchemaDefinitionTemplate"); | ||
const dedent = new RegExp('\n([\t ]*)', 'gm'); | ||
@@ -53,2 +54,6 @@ class TemplateUtils { | ||
switch (type) { | ||
case Models_1.TypeSystemDefinition.SchemaDefinition: | ||
return SchemaDefinitionTemplate_1.SchemaDefinitionTemplate.resolve(f); | ||
case Models_1.TypeSystemExtension.SchemaExtension: | ||
return SchemaDefinitionTemplate_1.SchemaExtensionTemplate.resolve(f); | ||
case Models_1.TypeSystemDefinition.FieldDefinition: | ||
@@ -55,0 +60,0 @@ return FieldTemplate_1.FieldTemplate.resolve(f, prefix); |
{ | ||
"name": "graphql-js-tree", | ||
"version": "0.4.0", | ||
"version": "1.0.0", | ||
"private": false, | ||
@@ -5,0 +5,0 @@ "license": "MIT", |
No v1
QualityPackage is not semver >=1. This means it is not stable and does not support ^ ranges.
Found 1 instance in 1 package
125771
76
2595
0