netlify-onegraph-internal
Advanced tools
Comparing version 0.0.36 to 0.0.37
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var fs_1 = require("fs"); | ||
var graphql_1 = require("graphql"); | ||
var graphqlHelpers_1 = require("./graphqlHelpers"); | ||
var test = function () { | ||
var _a; | ||
var sourceGraphQLFilename = "./tests/assets/netlifyGraphOperationsLibrary.graphql"; | ||
var schemaGraphQLFilename = "./tests/assets/netlifyGraphSchema.graphql"; | ||
var sourceGraphQLFile = (0, fs_1.readFileSync)(sourceGraphQLFilename, "utf8"); | ||
var schemaFile = (0, fs_1.readFileSync)(schemaGraphQLFilename, "utf8"); | ||
var schema = (0, graphql_1.buildASTSchema)((0, graphql_1.parse)(schemaFile)); | ||
var opsDoc = (0, graphql_1.parse)(sourceGraphQLFile); | ||
var targetOperationName = "MyQuery"; | ||
var operation = opsDoc.definitions.find(function (operation) { | ||
var _a; | ||
if (operation.kind === graphql_1.Kind.OPERATION_DEFINITION && | ||
((_a = operation.name) === null || _a === void 0 ? void 0 : _a.value) === targetOperationName) { | ||
return true; | ||
} | ||
}); | ||
if (!operation) { | ||
throw new Error("Could not find operation: " + targetOperationName); | ||
} | ||
var operationVariableNames = ((_a = operation.variableDefinitions) === null || _a === void 0 ? void 0 : _a.map(function (variableDefinition) { return variableDefinition.variable.name.value; })) || []; | ||
var variableSignature = (0, graphqlHelpers_1.typeScriptSignatureForOperationVariables)(operationVariableNames, schema, operation); | ||
console.log("variableSignature:\n" + variableSignature); | ||
var result = (0, graphqlHelpers_1.normalizeOperationsDoc)(sourceGraphQLFile); | ||
@@ -10,0 +29,0 @@ if (!result) { |
@@ -45,3 +45,3 @@ import { GraphQLSchema, FragmentDefinitionNode, OperationDefinitionNode, GraphQLType } from "graphql"; | ||
export declare function listCount(gqlType: any): number; | ||
export declare function typeScriptDefinitionObjectForOperation(schema: GraphQLSchema, operationDefinition: OperationDefinitionNode | FragmentDefinitionNode, fragmentDefinitions: Record<string, FragmentDefinitionNode>, shouldLog?: boolean): OutObject; | ||
export declare function typeScriptDefinitionObjectForOperation(schema: GraphQLSchema, operationDefinition: OperationDefinitionNode | FragmentDefinitionNode, fragmentDefinitions: Record<string, FragmentDefinitionNode>): OutObject; | ||
export declare function typeScriptSignatureForOperation(schema: GraphQLSchema, operationDefinition: OperationDefinitionNode, fragmentDefinitions: Record<string, FragmentDefinitionNode>): string; | ||
@@ -48,0 +48,0 @@ export declare function typeScriptDefinitionObjectForFragment(schema: GraphQLSchema, fragmentDefinition: FragmentDefinitionNode, fragmentDefinitions: Record<string, FragmentDefinitionNode>, shouldLog?: boolean): OutObject | OutScalar; |
@@ -136,2 +136,48 @@ "use strict"; | ||
}); | ||
var guessVariableDescription = function (variableNames) { | ||
var variableRecords = {}; | ||
for (var _i = 0, variableNames_1 = variableNames; _i < variableNames_1.length; _i++) { | ||
var variableName = variableNames_1[_i]; | ||
variableRecords[variableName] = { | ||
usageCount: 0, | ||
descriptions: new Set(), | ||
}; | ||
} | ||
var typeInfo = new graphql_1.TypeInfo(schema); | ||
var argHandler = function (node) { | ||
if (node.value && node.value.kind === graphql_1.Kind.VARIABLE) { | ||
var argument = typeInfo.getArgument(); | ||
var existingRecord = variableRecords[node.value.name.value]; | ||
var existingDescription = existingRecord === null || existingRecord === void 0 ? void 0 : existingRecord.descriptions; | ||
if (existingDescription && (argument === null || argument === void 0 ? void 0 : argument.description)) { | ||
existingDescription.add(argument.description); | ||
} | ||
variableRecords[node.value.name.value] = __assign(__assign({}, existingRecord), { usageCount: existingRecord.usageCount + 1 }); | ||
} | ||
return node; | ||
}; | ||
var objectFieldHandler = function (node) { | ||
if (node.value && node.value.kind === graphql_1.Kind.VARIABLE) { | ||
var parentType = typeInfo.getParentInputType(); | ||
var namedParentType = (0, graphql_1.getNamedType)(parentType); | ||
var field = void 0; | ||
if ((0, graphql_1.isInputObjectType)(namedParentType)) { | ||
field = namedParentType === null || namedParentType === void 0 ? void 0 : namedParentType.getFields()[node.name.value]; | ||
var existingRecord = variableRecords[node.value.name.value]; | ||
var existingDescription = existingRecord === null || existingRecord === void 0 ? void 0 : existingRecord.descriptions; | ||
if (existingDescription && (field === null || field === void 0 ? void 0 : field.description)) { | ||
existingDescription.add(field.description); | ||
} | ||
variableRecords[node.value.name.value] = __assign(__assign({}, existingRecord), { usageCount: existingRecord.usageCount + 1 }); | ||
} | ||
return node; | ||
} | ||
}; | ||
(0, graphql_1.visit)(operationDefinition, (0, graphql_1.visitWithTypeInfo)(typeInfo, { | ||
Argument: argHandler, | ||
ObjectField: objectFieldHandler, | ||
})); | ||
return variableRecords; | ||
}; | ||
var variableUsageInfo = guessVariableDescription(variableNames); | ||
var typesObject = variables | ||
@@ -146,4 +192,5 @@ .map(function (_a) { | ||
} | ||
var isRequired = (0, graphql_1.isNonNullType)(gqlType); | ||
var tsType = typeScriptForGraphQLType(schema, gqlType); | ||
return [varName, tsType]; | ||
return [varName, tsType, isRequired]; | ||
}) | ||
@@ -153,7 +200,14 @@ .filter(Boolean); | ||
.map(function (_a) { | ||
var name = _a[0], tsType = _a[1]; | ||
return "\"" + name + "\": " + tsType; | ||
var name = _a[0], tsType = _a[1], isRequired = _a[2]; | ||
var usageCount = variableUsageInfo[name].usageCount; | ||
var descriptions = variableUsageInfo[name].descriptions; | ||
var description = ""; | ||
if (usageCount > 0 && (descriptions === null || descriptions === void 0 ? void 0 : descriptions.size) === 1) { | ||
description = " /**\n * " + Array.from(descriptions)[0] + "\n */\n "; | ||
} | ||
var optionalMark = isRequired ? "" : "?"; | ||
return description + "\"" + name + "\"" + optionalMark + ": " + tsType; | ||
}) | ||
.join("; "); | ||
var types = "{" + typeFields + "}"; | ||
.join("; \n"); | ||
var types = "{\n " + typeFields + "\n}"; | ||
return types === "" ? "null" : types; | ||
@@ -181,32 +235,31 @@ } | ||
var unknownScalar = { kind: "scalar", type: "unknown" }; | ||
function typeScriptDefinitionObjectForOperation(schema, operationDefinition, fragmentDefinitions, shouldLog) { | ||
if (shouldLog === void 0) { shouldLog = true; } | ||
var dummyOut = { | ||
kind: "object", | ||
namedFragments: [], | ||
inlineFragments: [], | ||
selections: { | ||
data: { | ||
kind: "selection_field", | ||
name: "data", | ||
description: "Any data retrieved by the function will be returned here [Placeholder]", | ||
var dummyOut = { | ||
kind: "object", | ||
namedFragments: [], | ||
inlineFragments: [], | ||
selections: { | ||
data: { | ||
kind: "selection_field", | ||
name: "data", | ||
description: "Any data retrieved by the function will be returned here [Placeholder]", | ||
type: { | ||
kind: "scalar", | ||
type: "Record<string, unknown>", | ||
}, | ||
}, | ||
errors: { | ||
kind: "selection_field", | ||
name: "errors", | ||
description: "Any errors in the function will be returned here [Placeholder]", | ||
type: { | ||
kind: "array", | ||
type: { | ||
kind: "scalar", | ||
type: "Record<string, unknown>", | ||
type: "GraphQLError", | ||
}, | ||
}, | ||
errors: { | ||
kind: "selection_field", | ||
name: "errors", | ||
description: "Any errors in the function will be returned here [Placeholder]", | ||
type: { | ||
kind: "array", | ||
type: { | ||
kind: "scalar", | ||
type: "GraphQLError", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}; | ||
}, | ||
}; | ||
function typeScriptDefinitionObjectForOperation(schema, operationDefinition, fragmentDefinitions) { | ||
var objectHelper = function (type, selectionSet) { | ||
@@ -213,0 +266,0 @@ var inlineFragments = []; |
{ | ||
"name": "netlify-onegraph-internal", | ||
"version": "0.0.36", | ||
"version": "0.0.37", | ||
"description": "Internal tools for use by Netlify", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
353404
4605