Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

grats

Package Overview
Dependencies
Maintainers
1
Versions
240
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

grats - npm Package Compare versions

Comparing version 0.0.0-main-9fae3b01 to 0.0.0-main-a74be599

10

dist/package.json
{
"name": "grats",
"version": "0.0.3",
"version": "0.0.5",
"main": "dist/src/index.js",

@@ -12,3 +12,3 @@ "bin": "dist/src/cli.js",

"scripts": {
"test": "ts-node --esm src/tests/test.ts",
"test": "ts-node src/tests/test.ts",
"integration-tests": "node src/tests/integration.mjs",

@@ -38,3 +38,7 @@ "build": "tsc --build",

},
"packageManager": "pnpm@8.1.1"
"packageManager": "pnpm@8.1.1",
"engines": {
"node": ">=16 <=21",
"pnpm": "^8"
}
}

@@ -12,3 +12,2 @@ /**

export declare function fieldTagOnWrongNode(): string;
export declare function implementsTagOnWrongNode(): string;
export declare function killsParentOnExceptionOnWrongNode(): string;

@@ -36,3 +35,3 @@ export declare function wrongCasingForGratsTag(actual: string, expected: string): string;

export declare function inputFieldUntyped(): string;
export declare function typeTagOnUnamedClass(): string;
export declare function typeTagOnUnnamedClass(): string;
export declare function typeTagOnAliasOfNonObject(): string;

@@ -68,3 +67,3 @@ export declare function typeNameNotDeclaration(): string;

export declare function pluralTypeMissingParameter(): string;
export declare function expectedIdentifer(): string;
export declare function expectedIdentifier(): string;
export declare function killsParentOnExceptionWithWrongConfig(): string;

@@ -75,2 +74,5 @@ export declare function killsParentOnExceptionOnNullable(): string;

export declare function implementsTagMissingValue(): string;
export declare function implementsTagOnClass(): string;
export declare function implementsTagOnInterface(): string;
export declare function implementsTagOnTypeAlias(): string;
export declare function duplicateTag(tagName: string): string;

@@ -83,1 +85,7 @@ export declare function duplicateInterfaceTag(): string;

export declare function unresolvedTypeReference(): string;
export declare function expectedTypeAnnotationOnContext(): string;
export declare function expectedTypeAnnotationOfReferenceOnContext(): string;
export declare function expectedTypeAnnotationOnContextToBeResolvable(): string;
export declare function expectedTypeAnnotationOnContextToHaveDeclaration(): string;
export declare function unexpectedParamSpreadForContextParam(): string;
export declare function multipleContextTypes(): string;
"use strict";
exports.__esModule = true;
exports.defaultArgElementIsNotAssignment = exports.defaultValueIsNotLiteral = exports.ambiguousNumberType = exports.expectedOneNonNullishType = exports.propertyFieldMissingType = exports.cannotResolveSymbolForDescription = exports.promiseMissingTypeArg = exports.methodMissingType = exports.gqlEntityMissingName = exports.enumVariantMissingInitializer = exports.enumVariantNotStringLiteral = exports.enumTagOnInvalidNode = exports.argNotTyped = exports.argNameNotLiteral = exports.argIsNotProperty = exports.argumentParamIsNotObject = exports.argumentParamIsMissingType = exports.typeNameDoesNotMatchExpected = exports.typeNameTypeNotStringLiteral = exports.typeNameMissingTypeAnnotation = exports.typeNameInitializerWrong = exports.typeNameInitializeNotString = exports.typeNameMissingInitializer = exports.typeNameNotDeclaration = exports.typeTagOnAliasOfNonObject = exports.typeTagOnUnamedClass = exports.inputFieldUntyped = exports.inputTypeFieldNotProperty = exports.inputTypeNotLiteral = exports.functionFieldNotNamedExport = exports.functionFieldDefaultExport = exports.functionFieldNotNamed = exports.functionFieldParentTypeNotValid = exports.functionFieldParentTypeMissing = exports.functionFieldNotTopLevel = exports.invalidReturnTypeForFunctionField = exports.invalidParentArgForFunctionField = exports.expectedUnionTypeReference = exports.expectedUnionTypeNode = exports.invalidUnionTagUsage = exports.invalidInputTagUsage = exports.invalidEnumTagUsage = exports.invalidInterfaceTagUsage = exports.invalidScalarTagUsage = exports.invalidTypeTagUsage = exports.invalidGratsTag = exports.wrongCasingForGratsTag = exports.killsParentOnExceptionOnWrongNode = exports.implementsTagOnWrongNode = exports.fieldTagOnWrongNode = void 0;
exports.unresolvedTypeReference = exports.invalidTypePassedToFieldFunction = exports.parameterPropertyMissingType = exports.parameterPropertyNotPublic = exports.parameterWithoutModifiers = exports.duplicateInterfaceTag = exports.duplicateTag = exports.implementsTagMissingValue = exports.mergedInterfaces = exports.nonNullTypeCannotBeOptional = exports.killsParentOnExceptionOnNullable = exports.killsParentOnExceptionWithWrongConfig = exports.expectedIdentifer = exports.pluralTypeMissingParameter = exports.unknownGraphQLType = exports.unsupportedTypeLiteral = exports.defaultArgPropertyMissingInitializer = exports.defaultArgPropertyMissingName = void 0;
exports.defaultArgPropertyMissingName = exports.defaultArgElementIsNotAssignment = exports.defaultValueIsNotLiteral = exports.ambiguousNumberType = exports.expectedOneNonNullishType = exports.propertyFieldMissingType = exports.cannotResolveSymbolForDescription = exports.promiseMissingTypeArg = exports.methodMissingType = exports.gqlEntityMissingName = exports.enumVariantMissingInitializer = exports.enumVariantNotStringLiteral = exports.enumTagOnInvalidNode = exports.argNotTyped = exports.argNameNotLiteral = exports.argIsNotProperty = exports.argumentParamIsNotObject = exports.argumentParamIsMissingType = exports.typeNameDoesNotMatchExpected = exports.typeNameTypeNotStringLiteral = exports.typeNameMissingTypeAnnotation = exports.typeNameInitializerWrong = exports.typeNameInitializeNotString = exports.typeNameMissingInitializer = exports.typeNameNotDeclaration = exports.typeTagOnAliasOfNonObject = exports.typeTagOnUnnamedClass = exports.inputFieldUntyped = exports.inputTypeFieldNotProperty = exports.inputTypeNotLiteral = exports.functionFieldNotNamedExport = exports.functionFieldDefaultExport = exports.functionFieldNotNamed = exports.functionFieldParentTypeNotValid = exports.functionFieldParentTypeMissing = exports.functionFieldNotTopLevel = exports.invalidReturnTypeForFunctionField = exports.invalidParentArgForFunctionField = exports.expectedUnionTypeReference = exports.expectedUnionTypeNode = exports.invalidUnionTagUsage = exports.invalidInputTagUsage = exports.invalidEnumTagUsage = exports.invalidInterfaceTagUsage = exports.invalidScalarTagUsage = exports.invalidTypeTagUsage = exports.invalidGratsTag = exports.wrongCasingForGratsTag = exports.killsParentOnExceptionOnWrongNode = exports.fieldTagOnWrongNode = void 0;
exports.multipleContextTypes = exports.unexpectedParamSpreadForContextParam = exports.expectedTypeAnnotationOnContextToHaveDeclaration = exports.expectedTypeAnnotationOnContextToBeResolvable = exports.expectedTypeAnnotationOfReferenceOnContext = exports.expectedTypeAnnotationOnContext = exports.unresolvedTypeReference = exports.invalidTypePassedToFieldFunction = exports.parameterPropertyMissingType = exports.parameterPropertyNotPublic = exports.parameterWithoutModifiers = exports.duplicateInterfaceTag = exports.duplicateTag = exports.implementsTagOnTypeAlias = exports.implementsTagOnInterface = exports.implementsTagOnClass = exports.implementsTagMissingValue = exports.mergedInterfaces = exports.nonNullTypeCannotBeOptional = exports.killsParentOnExceptionOnNullable = exports.killsParentOnExceptionWithWrongConfig = exports.expectedIdentifier = exports.pluralTypeMissingParameter = exports.unknownGraphQLType = exports.unsupportedTypeLiteral = exports.defaultArgPropertyMissingInitializer = void 0;
var Extractor_1 = require("./Extractor");

@@ -9,3 +9,4 @@ // TODO: Move these to short URLS that are easier to keep from breaking.

mergedInterfaces: "https://grats.capt.dev/docs/dockblock-tags/interfaces/#merged-interfaces",
parameterProperties: "https://grats.capt.dev/docs/dockblock-tags/fields#class-based-fields"
parameterProperties: "https://grats.capt.dev/docs/dockblock-tags/fields#class-based-fields",
typeImplementsInterface: "TODO"
};

@@ -26,6 +27,2 @@ /**

exports.fieldTagOnWrongNode = fieldTagOnWrongNode;
function implementsTagOnWrongNode() {
return "`@".concat(Extractor_1.IMPLEMENTS_TAG, "` can only be used on Grats type or interface declarations. Did you mean to include the `@").concat(Extractor_1.TYPE_TAG, "` or `@").concat(Extractor_1.INTERFACE_TAG, "` tag in this docblock?");
}
exports.implementsTagOnWrongNode = implementsTagOnWrongNode;
function killsParentOnExceptionOnWrongNode() {

@@ -77,3 +74,3 @@ return "Unexpected `@".concat(Extractor_1.KILLS_PARENT_ON_EXCEPTION_TAG, "`. `@").concat(Extractor_1.KILLS_PARENT_ON_EXCEPTION_TAG, "` can only be used in field annotation docblocks. Perhaps you are missing a `@").concat(Extractor_1.FIELD_TAG, "` tag?");

function invalidParentArgForFunctionField() {
return "Expected `@".concat(Extractor_1.FIELD_TAG, "` function to have a first argument representing the type to extend.");
return "Expected `@".concat(Extractor_1.FIELD_TAG, "` function to have a first argument representing the type to extend. If you don't need access to the parent object in the function, you can name the variable `_` to indicate that it is unused. e.g. `function myField(_: ParentType) {}`");
}

@@ -121,6 +118,6 @@ exports.invalidParentArgForFunctionField = invalidParentArgForFunctionField;

exports.inputFieldUntyped = inputFieldUntyped;
function typeTagOnUnamedClass() {
function typeTagOnUnnamedClass() {
return "Unexpected `@".concat(Extractor_1.TYPE_TAG, "` annotation on unnamed class declaration.");
}
exports.typeTagOnUnamedClass = typeTagOnUnamedClass;
exports.typeTagOnUnnamedClass = typeTagOnUnnamedClass;
function typeTagOnAliasOfNonObject() {

@@ -159,7 +156,7 @@ return "Expected `@".concat(Extractor_1.TYPE_TAG, "` type to be a type literal. For example: `type Foo = { bar: string }`");

function argumentParamIsMissingType() {
return "Expected GraphQL field arguments to have a TypeScript type. If there are no arguments, you can use `args: never`.";
return "Expected GraphQL field arguments to have a TypeScript type. If there are no arguments, you can use `args: unknown`.";
}
exports.argumentParamIsMissingType = argumentParamIsMissingType;
function argumentParamIsNotObject() {
return "Expected GraphQL field arguments to be typed using a literal object: `{someField: string}`.";
return "Expected GraphQL field arguments to be typed using a literal object: `{someField: string}`. If there are no arguments, you can use `args: unknown`.";
}

@@ -247,6 +244,6 @@ exports.argumentParamIsNotObject = argumentParamIsNotObject;

exports.pluralTypeMissingParameter = pluralTypeMissingParameter;
function expectedIdentifer() {
function expectedIdentifier() {
return "Expected an identifier.";
}
exports.expectedIdentifer = expectedIdentifer;
exports.expectedIdentifier = expectedIdentifier;
function killsParentOnExceptionWithWrongConfig() {

@@ -275,5 +272,17 @@ return "Unexpected `@".concat(Extractor_1.KILLS_PARENT_ON_EXCEPTION_TAG, "` tag. `@").concat(Extractor_1.KILLS_PARENT_ON_EXCEPTION_TAG, "` is only supported when the Grats config `nullableByDefault` is enabled.");

function implementsTagMissingValue() {
return "Expected `@".concat(Extractor_1.IMPLEMENTS_TAG, "` to be followed by one or more interface names.");
return "Expected `@".concat(Extractor_1.IMPLEMENTS_TAG_DEPRECATED, "` to be followed by one or more interface names.");
}
exports.implementsTagMissingValue = implementsTagMissingValue;
function implementsTagOnClass() {
return "`@".concat(Extractor_1.IMPLEMENTS_TAG_DEPRECATED, "` has been deprecated. Instead use `class MyType implements MyInterface`.");
}
exports.implementsTagOnClass = implementsTagOnClass;
function implementsTagOnInterface() {
return "`@".concat(Extractor_1.IMPLEMENTS_TAG_DEPRECATED, "` has been deprecated. Instead use `interface MyType extends MyInterface`.");
}
exports.implementsTagOnInterface = implementsTagOnInterface;
function implementsTagOnTypeAlias() {
return "`@".concat(Extractor_1.IMPLEMENTS_TAG_DEPRECATED, "` has been deprecated. Types which implement GraphQL interfaces should be defined using TypeScript class or interface declarations. Learn more: ").concat(DOC_URLS.typeImplementsInterface, ".");
}
exports.implementsTagOnTypeAlias = implementsTagOnTypeAlias;
function duplicateTag(tagName) {

@@ -284,3 +293,3 @@ return "Unexpected duplicate `@".concat(tagName, "` tag. Grats does not accept multiple instances of the same tag.");

function duplicateInterfaceTag() {
return "Unexpected duplicate `@".concat(Extractor_1.IMPLEMENTS_TAG, "` tag. To declare that a type or interface implements multiple interfaces list them as comma separated values: `@").concat(Extractor_1.IMPLEMENTS_TAG, " interfaceA, interfaceB`.");
return "Unexpected duplicate `@".concat(Extractor_1.IMPLEMENTS_TAG_DEPRECATED, "` tag. To declare that a type or interface implements multiple interfaces list them as comma separated values: `@").concat(Extractor_1.IMPLEMENTS_TAG_DEPRECATED, " interfaceA, interfaceB`.");
}

@@ -290,3 +299,3 @@ exports.duplicateInterfaceTag = duplicateInterfaceTag;

return [
"Expected `@".concat(Extractor_1.FIELD_TAG, "` constructor paramater to be a parameter property. This requires a modifier such as `public` or `readonly` before the parameter name.\n\n"),
"Expected `@".concat(Extractor_1.FIELD_TAG, "` constructor parameter to be a parameter property. This requires a modifier such as `public` or `readonly` before the parameter name.\n\n"),
"Learn more: ".concat(DOC_URLS.parameterProperties),

@@ -315,1 +324,27 @@ ].join("");

exports.unresolvedTypeReference = unresolvedTypeReference;
function expectedTypeAnnotationOnContext() {
return "Expected context parameter to have a type annotation. Grats validates that your context parameter is type-safe by checking all context values reference the same type declaration.";
}
exports.expectedTypeAnnotationOnContext = expectedTypeAnnotationOnContext;
function expectedTypeAnnotationOfReferenceOnContext() {
return "Expected context parameter's type to be a type reference Grats validates that your context parameter is type-safe by checking all context values reference the same type declaration.";
}
exports.expectedTypeAnnotationOfReferenceOnContext = expectedTypeAnnotationOfReferenceOnContext;
function expectedTypeAnnotationOnContextToBeResolvable() {
// TODO: Provide guidance?
// TODO: I don't think we have a test case that triggers this error.
return "Unable to resolve context parameter type. Grats validates that your context parameter is type-safe by checking all context values reference the same type declaration.";
}
exports.expectedTypeAnnotationOnContextToBeResolvable = expectedTypeAnnotationOnContextToBeResolvable;
function expectedTypeAnnotationOnContextToHaveDeclaration() {
return "Unable to locate the declaration of the context parameter's type. Grats validates that your context parameter is type-safe by checking all context values reference the same type declaration. Did you forget to import or define this type?";
}
exports.expectedTypeAnnotationOnContextToHaveDeclaration = expectedTypeAnnotationOnContextToHaveDeclaration;
function unexpectedParamSpreadForContextParam() {
return "Unexpected spread parameter in context parameter position. Grats expects the context parameter to be a single, explicitly typed, argument.";
}
exports.unexpectedParamSpreadForContextParam = unexpectedParamSpreadForContextParam;
function multipleContextTypes() {
return "Context argument's type does not match. Grats expects all resolvers that read the context argument to use the same type for that argument. Did you use the incorrect type in one of your resolvers?";
}
exports.multipleContextTypes = multipleContextTypes;

@@ -17,3 +17,3 @@ import { FieldDefinitionNode, InputValueDefinitionNode, NamedTypeNode, NameNode, TypeNode, StringValueNode, ConstValueNode, ConstDirectiveNode, EnumValueDefinitionNode, ConstObjectFieldNode, ConstObjectValueNode, ConstListValueNode } from "graphql";

export declare const INPUT_TAG = "gqlInput";
export declare const IMPLEMENTS_TAG = "gqlImplements";
export declare const IMPLEMENTS_TAG_DEPRECATED = "gqlImplements";
export declare const KILLS_PARENT_ON_EXCEPTION_TAG = "killsParentOnException";

@@ -74,4 +74,6 @@ export declare const ALL_TAGS: string[];

collectInterfaces(node: ts.ClassDeclaration | ts.InterfaceDeclaration | ts.TypeAliasDeclaration): Array<NamedTypeNode> | null;
collectTagInterfaces(node: ts.ClassDeclaration | ts.InterfaceDeclaration | ts.TypeAliasDeclaration): Array<NamedTypeNode> | null;
collectHeritageInterfaces(node: ts.ClassDeclaration): Array<NamedTypeNode> | null;
reportTagInterfaces(node: ts.TypeAliasDeclaration | ts.ClassDeclaration | ts.InterfaceDeclaration): null | undefined;
collectHeritageInterfaces(node: ts.ClassDeclaration | ts.InterfaceDeclaration): Array<NamedTypeNode> | null;
symbolHasGqlTag(node: ts.Node): boolean;
hasGqlTag(node: ts.Node): boolean;
interfaceInterfaceDeclaration(node: ts.InterfaceDeclaration, tag: ts.JSDocTag): null | undefined;

@@ -84,3 +86,3 @@ collectFields(node: ts.ClassDeclaration | ts.InterfaceDeclaration | ts.TypeLiteralNode): Array<FieldDefinitionNode>;

collectArrayLiteral(node: ts.ArrayLiteralExpression): ConstListValueNode | null;
cellectObjectLiteral(node: ts.ObjectLiteralExpression): ConstObjectValueNode | null;
collectObjectLiteral(node: ts.ObjectLiteralExpression): ConstObjectValueNode | null;
collectObjectField(node: ts.ObjectLiteralElementLike): ConstObjectFieldNode | null;

@@ -93,2 +95,3 @@ collectArg(node: ts.TypeElement, defaults?: Map<string, ts.Expression> | null): InputValueDefinitionNode | null;

entityName(node: ts.ClassDeclaration | ts.MethodDeclaration | ts.MethodSignature | ts.PropertyDeclaration | ts.InterfaceDeclaration | ts.PropertySignature | ts.EnumDeclaration | ts.TypeAliasDeclaration | ts.FunctionDeclaration | ts.ParameterDeclaration, tag: ts.JSDocTag): NameNode | null;
validateContextParameter(node: ts.ParameterDeclaration): null | undefined;
methodDeclaration(node: ts.MethodDeclaration | ts.MethodSignature): FieldDefinitionNode | null;

@@ -95,0 +98,0 @@ collectMethodType(node: ts.TypeNode): TypeNode | null;

@@ -30,3 +30,3 @@ "use strict";

exports.__esModule = true;
exports.Extractor = exports.ALL_TAGS = exports.KILLS_PARENT_ON_EXCEPTION_TAG = exports.IMPLEMENTS_TAG = exports.INPUT_TAG = exports.UNION_TAG = exports.ENUM_TAG = exports.INTERFACE_TAG = exports.SCALAR_TAG = exports.FIELD_TAG = exports.TYPE_TAG = exports.ISSUE_URL = exports.LIBRARY_NAME = exports.LIBRARY_IMPORT_NAME = void 0;
exports.Extractor = exports.ALL_TAGS = exports.KILLS_PARENT_ON_EXCEPTION_TAG = exports.IMPLEMENTS_TAG_DEPRECATED = exports.INPUT_TAG = exports.UNION_TAG = exports.ENUM_TAG = exports.INTERFACE_TAG = exports.SCALAR_TAG = exports.FIELD_TAG = exports.TYPE_TAG = exports.ISSUE_URL = exports.LIBRARY_NAME = exports.LIBRARY_IMPORT_NAME = void 0;
var graphql_1 = require("graphql");

@@ -38,3 +38,2 @@ var DiagnosticError_1 = require("./utils/DiagnosticError");

var JSDoc_1 = require("./utils/JSDoc");
var helpers_1 = require("./utils/helpers");
var GraphQLConstructor_1 = require("./GraphQLConstructor");

@@ -52,3 +51,3 @@ var serverDirectives_1 = require("./serverDirectives");

exports.INPUT_TAG = "gqlInput";
exports.IMPLEMENTS_TAG = "gqlImplements";
exports.IMPLEMENTS_TAG_DEPRECATED = "gqlImplements";
exports.KILLS_PARENT_ON_EXCEPTION_TAG = "killsParentOnException";

@@ -64,3 +63,2 @@ // All the tags that start with gql

exports.INPUT_TAG,
exports.IMPLEMENTS_TAG,
];

@@ -128,11 +126,2 @@ var DEPRECATED_TAG = "deprecated";

break;
case exports.IMPLEMENTS_TAG: {
var hasTypeOrInterfaceTag = ts.getJSDocTags(node).some(function (t) {
return (t.tagName.text === exports.TYPE_TAG || t.tagName.text === exports.INTERFACE_TAG);
});
if (!hasTypeOrInterfaceTag) {
_this.report(tag.tagName, E.implementsTagOnWrongNode());
}
break;
}
case exports.KILLS_PARENT_ON_EXCEPTION_TAG: {

@@ -329,2 +318,6 @@ var hasFieldTag = ts.getJSDocTags(node).some(function (t) {

}
var context = node.parameters[2];
if (context != null) {
this.validateContextParameter(context);
}
var description = this.collectDescription(funcName);

@@ -443,3 +436,3 @@ if (!ts.isSourceFile(node.parent)) {

if (node.name == null) {
return this.report(node, E.typeTagOnUnamedClass());
return this.report(node, E.typeTagOnUnnamedClass());
}

@@ -547,23 +540,20 @@ var name = this.entityName(node, tag);

Extractor.prototype.collectInterfaces = function (node) {
var heritageInterfaces = ts.isClassDeclaration(node)
this.reportTagInterfaces(node);
return ts.isClassDeclaration(node) || ts.isInterfaceDeclaration(node)
? this.collectHeritageInterfaces(node)
: null;
return (0, helpers_1.concatMaybeArrays)(heritageInterfaces, this.collectTagInterfaces(node));
};
Extractor.prototype.collectTagInterfaces = function (node) {
var _this = this;
var tag = this.findTag(node, exports.IMPLEMENTS_TAG);
Extractor.prototype.reportTagInterfaces = function (node) {
var tag = this.findTag(node, exports.IMPLEMENTS_TAG_DEPRECATED);
if (tag == null)
return null;
var commentName = ts.getTextOfJSDocComment(tag.comment);
if (commentName == null) {
return this.report(tag, E.implementsTagMissingValue());
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
this.report(tag, E.implementsTagOnClass());
}
return commentName.split(",").map(function (name) {
// FIXME: Use more targeted location information.
// Will require rewriting everything that expects a node for location
// purposes to transform the node into a location eagerly. Then we can have
// a richer set of tools to construct custom locations.
return _this.gql.namedType(tag, name.trim());
});
if (node.kind === ts.SyntaxKind.InterfaceDeclaration) {
this.report(tag, E.implementsTagOnInterface());
}
if (node.kind === ts.SyntaxKind.TypeAliasDeclaration) {
this.report(tag, E.implementsTagOnTypeAlias());
}
};

@@ -574,12 +564,18 @@ Extractor.prototype.collectHeritageInterfaces = function (node) {

return null;
var maybeInterfaces = node.heritageClauses.flatMap(function (clause) {
if (clause.token !== ts.SyntaxKind.ImplementsKeyword)
return [];
return clause.types.map(function (type) {
if (!ts.isIdentifier(type.expression)) {
// TODO: Are there valid cases we want to cover here?
return null;
}
var namedType = _this.gql.namedType(type.expression, TypeContext_1.UNRESOLVED_REFERENCE_NAME);
_this.ctx.markUnresolvedType(type.expression, namedType.name);
var maybeInterfaces = node.heritageClauses
.filter(function (clause) {
if (node.kind === ts.SyntaxKind.ClassDeclaration) {
return clause.token === ts.SyntaxKind.ImplementsKeyword;
}
// Interfaces can only have extends clauses, and those are allowed.
return true;
})
.flatMap(function (clause) {
return clause.types
.map(function (type) { return type.expression; })
.filter(function (expression) { return ts.isIdentifier(expression); })
.filter(function (expression) { return _this.symbolHasGqlTag(expression); })
.map(function (expression) {
var namedType = _this.gql.namedType(expression, TypeContext_1.UNRESOLVED_REFERENCE_NAME);
_this.ctx.markUnresolvedType(expression, namedType.name);
return namedType;

@@ -594,2 +590,16 @@ });

};
Extractor.prototype.symbolHasGqlTag = function (node) {
var symbol = this.ctx.checker.getSymbolAtLocation(node);
if (symbol == null)
return false;
var declaration = this.ctx.findSymbolDeclaration(symbol);
if (declaration == null)
return false;
return this.hasGqlTag(declaration);
};
Extractor.prototype.hasGqlTag = function (node) {
return ts.getJSDocTags(node).some(function (tag) {
return exports.ALL_TAGS.includes(tag.tagName.text);
});
};
Extractor.prototype.interfaceInterfaceDeclaration = function (node, tag) {

@@ -722,3 +732,3 @@ var _this = this;

}
if (argsType.kind === ts.SyntaxKind.NeverKeyword) {
if (argsType.kind === ts.SyntaxKind.UnknownKeyword) {
return [];

@@ -792,3 +802,3 @@ }

else if (ts.isObjectLiteralExpression(node)) {
return this.cellectObjectLiteral(node);
return this.collectObjectLiteral(node);
}

@@ -828,3 +838,3 @@ else if (ts.isArrayLiteralExpression(node)) {

};
Extractor.prototype.cellectObjectLiteral = function (node) {
Extractor.prototype.collectObjectLiteral = function (node) {
var e_8, _a;

@@ -1034,2 +1044,41 @@ var fields = [];

};
// Ensure the type of the ctx param resolves to the declaration
// annotated with `@gqlContext`.
Extractor.prototype.validateContextParameter = function (node) {
if (node.type == null) {
return this.report(node, E.expectedTypeAnnotationOnContext());
}
if (node.type.kind === ts.SyntaxKind.UnknownKeyword) {
// If the user just needs to define the argument to get to a later parameter,
// they can use `ctx: unknown` to safely avoid triggering a Grats error.
return;
}
if (!ts.isTypeReferenceNode(node.type)) {
return this.report(node.type, E.expectedTypeAnnotationOfReferenceOnContext());
}
// Check for ...
if (node.dotDotDotToken != null) {
return this.report(node.dotDotDotToken, E.unexpectedParamSpreadForContextParam());
}
var symbol = this.ctx.checker.getSymbolAtLocation(node.type.typeName);
if (symbol == null) {
return this.report(node.type.typeName, E.expectedTypeAnnotationOnContextToBeResolvable());
}
var declaration = this.ctx.findSymbolDeclaration(symbol);
if (declaration == null) {
return this.report(node.type.typeName, E.expectedTypeAnnotationOnContextToHaveDeclaration());
}
if (this.ctx.gqlContext == null) {
// This is the first typed context value we've seen...
this.ctx.gqlContext = {
declaration: declaration,
firstReference: node.type.typeName
};
}
else if (this.ctx.gqlContext.declaration !== declaration) {
return this.report(node.type.typeName, E.multipleContextTypes(), [
this.related(this.ctx.gqlContext.firstReference, "A different type reference was used here"),
]);
}
};
Extractor.prototype.methodDeclaration = function (node) {

@@ -1054,2 +1103,6 @@ var tag = this.findTag(node, exports.FIELD_TAG);

}
var context = node.parameters[1];
if (context != null) {
this.validateContextParameter(context);
}
var description = this.collectDescription(node.name);

@@ -1253,3 +1306,3 @@ var id = this.expectIdentifier(node.name);

}
return this.report(node, E.expectedIdentifer());
return this.report(node, E.expectedIdentifier());
};

@@ -1268,3 +1321,3 @@ Extractor.prototype.findTag = function (node, tagName) {

});
var message = tagName === exports.IMPLEMENTS_TAG
var message = tagName === exports.IMPLEMENTS_TAG_DEPRECATED
? E.duplicateInterfaceTag()

@@ -1271,0 +1324,0 @@ : E.duplicateTag(tagName);

@@ -39,5 +39,5 @@ "use strict";

var implementor = _g.value;
var resolved = typeContext.resolveNamedDefinition(implementor.name);
var resolved = typeContext.resolveNamedType(implementor.name);
if (resolved.kind === "ERROR") {
errors.push(resolved.err);
// We trust that these errors will be reported elsewhere.
continue;

@@ -64,5 +64,5 @@ }

var implementor = _j.value;
var resolved = typeContext.resolveNamedDefinition(implementor.name);
var resolved = typeContext.resolveNamedType(implementor.name);
if (resolved.kind === "ERROR") {
errors.push(resolved.err);
// We trust that these errors will be reported elsewhere.
continue;

@@ -69,0 +69,0 @@ }

@@ -44,2 +44,3 @@ "use strict";

var serverDirectives_1 = require("./serverDirectives");
var helpers_1 = require("./utils/helpers");
var serverDirectives_2 = require("./serverDirectives");

@@ -91,2 +92,3 @@ __createBinding(exports, serverDirectives_2, "applyServerDirectives");

var definitions = Array.from(serverDirectives_1.DIRECTIVES_AST.definitions);
var errors = [];
try {

@@ -103,3 +105,4 @@ for (var _c = __values(program.getSourceFiles()), _d = _c.next(); !_d.done; _d = _c.next()) {

if (typeErrors.length > 0) {
return (0, DiagnosticError_1.err)(typeErrors);
(0, helpers_1.extend)(errors, typeErrors);
continue;
}

@@ -114,3 +117,4 @@ }

// the first one.
return (0, DiagnosticError_1.err)([syntaxErrors[0]]);
errors.push(syntaxErrors[0]);
continue;
}

@@ -120,4 +124,6 @@ }

var extractedResult = extractor.extract();
if (extractedResult.kind === "ERROR")
return extractedResult;
if (extractedResult.kind === "ERROR") {
(0, helpers_1.extend)(errors, extractedResult.err);
continue;
}
try {

@@ -145,2 +151,5 @@ for (var _e = (e_2 = void 0, __values(extractedResult.value)), _f = _e.next(); !_f.done; _f = _e.next()) {

}
if (errors.length > 0) {
return (0, DiagnosticError_1.err)(errors);
}
// If you define a field on an interface using the functional style, we need to add

@@ -147,0 +156,0 @@ // that field to each concrete type as well. This must be done after all types are created,

@@ -18,2 +18,10 @@ import { DefinitionNode, DocumentNode, FieldDefinitionNode, Location, NameNode } from "graphql";

/**
* Information about the GraphQL context type. We track the first value we see,
* and then validate that any other values we see are the same.
*/
type GqlContext = {
declaration: ts.Node;
firstReference: ts.Node;
};
/**
* Used to track TypeScript references.

@@ -36,2 +44,3 @@ *

_unresolvedTypes: Map<NameNode, ts.Symbol>;
gqlContext: GqlContext | null;
hasTypename: Set<string>;

@@ -42,6 +51,7 @@ constructor(options: ts.ParsedCommandLine, checker: ts.TypeChecker, host: ts.CompilerHost);

markUnresolvedType(node: ts.Node, name: NameNode): void;
findSymbolDeclaration(startSymbol: ts.Symbol): ts.Declaration | null;
resolveSymbol(startSymbol: ts.Symbol): ts.Symbol;
resolveTypes(doc: DocumentNode): DiagnosticsResult<DocumentNode>;
handleAbstractDefinitions(docs: GratsDefinitionNode[]): DiagnosticsResult<DefinitionNode[]>;
addAbstractFieldDefinition(doc: AbstractFieldDefinitionNode, interfaceGraph: InterfaceMap): DiagnosticsResult<DefinitionNode[]>;
resolveNamedDefinition(unresolved: NameNode): DiagnosticResult<NameNode>;
resolveNamedType(unresolved: NameNode): DiagnosticResult<NameNode>;

@@ -48,0 +58,0 @@ err(loc: Location, message: string, relatedInformation?: ts.DiagnosticRelatedInformation[]): ts.Diagnostic;

@@ -52,2 +52,5 @@ "use strict";

this._unresolvedTypes = new Map();
// The resolver context declaration, if it has been encountered.
// Gets mutated by Extractor.
this.gqlContext = null;
this.hasTypename = new Set();

@@ -79,7 +82,23 @@ this._options = options;

}
if (symbol.flags & ts.SymbolFlags.Alias) {
// Follow any aliases to get the real type declaration.
this._unresolvedTypes.set(name, this.resolveSymbol(symbol));
};
TypeContext.prototype.findSymbolDeclaration = function (startSymbol) {
var _a;
var symbol = this.resolveSymbol(startSymbol);
var declaration = (_a = symbol.declarations) === null || _a === void 0 ? void 0 : _a[0];
return declaration !== null && declaration !== void 0 ? declaration : null;
};
// Follow symbol aliases until we find the original symbol. Accounts for
// cyclical aliases.
TypeContext.prototype.resolveSymbol = function (startSymbol) {
var symbol = startSymbol;
var visitedSymbols = new Set();
while (ts.SymbolFlags.Alias & symbol.flags) {
if (visitedSymbols.has(symbol)) {
throw new Error("Cyclical alias detected. Breaking resolution.");
}
visitedSymbols.add(symbol);
symbol = this.checker.getAliasedSymbol(symbol);
}
this._unresolvedTypes.set(name, symbol);
return symbol;
};

@@ -242,20 +261,2 @@ TypeContext.prototype.resolveTypes = function (doc) {

};
TypeContext.prototype.resolveNamedDefinition = function (unresolved) {
var symbol = this._unresolvedTypes.get(unresolved);
if (symbol == null) {
if (unresolved.value === exports.UNRESOLVED_REFERENCE_NAME) {
// This is a logic error on our side.
throw new Error("Unexpected unresolved reference name.");
}
return (0, DiagnosticError_1.ok)(unresolved);
}
var nameDefinition = this._symbolToName.get(symbol);
if (nameDefinition == null) {
if (unresolved.loc == null) {
throw new Error("Expected namedType to have a location.");
}
return (0, DiagnosticError_1.err)(this.err(unresolved.loc, E.unresolvedTypeReference()));
}
return (0, DiagnosticError_1.ok)(__assign(__assign({}, unresolved), { value: nameDefinition.name.value }));
};
TypeContext.prototype.resolveNamedType = function (unresolved) {

@@ -262,0 +263,0 @@ var symbol = this._unresolvedTypes.get(unresolved);

@@ -1,2 +0,1 @@

export declare function concatMaybeArrays<T>(a: T[] | null, b: T[] | null): T[] | null;
export declare class DefaultMap<K, V> {

@@ -8,2 +7,2 @@ private readonly getDefault;

}
export declare function extend<T>(a: T[], b: T[]): void;
export declare function extend<T>(a: T[], b: readonly T[]): void;

@@ -14,13 +14,3 @@ "use strict";

exports.__esModule = true;
exports.extend = exports.DefaultMap = exports.concatMaybeArrays = void 0;
// Returns null if both are null, otherwise returns the concatenated values of
// the non-null arrays.
function concatMaybeArrays(a, b) {
if (a == null)
return b;
if (b == null)
return a;
return a.concat(b);
}
exports.concatMaybeArrays = concatMaybeArrays;
exports.extend = exports.DefaultMap = void 0;
var DefaultMap = /** @class */ (function () {

@@ -27,0 +17,0 @@ function DefaultMap(getDefault) {

{
"name": "grats",
"version": "0.0.0-main-9fae3b01",
"version": "0.0.0-main-a74be599",
"main": "dist/src/index.js",

@@ -32,4 +32,8 @@ "bin": "dist/src/cli.js",

"packageManager": "pnpm@8.1.1",
"engines": {
"node": ">=16 <=21",
"pnpm": "^8"
},
"scripts": {
"test": "ts-node --esm src/tests/test.ts",
"test": "ts-node src/tests/test.ts",
"integration-tests": "node src/tests/integration.mjs",

@@ -36,0 +40,0 @@ "build": "tsc --build",

@@ -11,3 +11,3 @@ # -=[ ALPHA SOFTWARE ]=-

When you write your GraphQL server in TypeScript, your fields and resovlers
When you write your GraphQL server in TypeScript, your fields and resolvers
are _already_ annotated with type information. _Grats leverages your existing

@@ -18,3 +18,3 @@ type annotations to automatically extract an executable GraphQL schema from your

By making your TypeScript implementation the source of truth, you never have to
worry about valiating that your implementiaton matches your schema. Your
worry about valuating that your implementation matches your schema. Your
implementation _is_ your schema!

@@ -30,9 +30,9 @@

* [@mofeiZ](https://github.com/mofeiZ) and [@alunyov](https://github/alunyov) for their Relay hack-week project exploring a similar idea.
* [@josephsavona](https://github.com/josephsavona) for input on the design of [Relay Resolvers](https://relay.dev/docs/guides/relay-resolvers/) which inspired this project.
* [@bradzacher](https://github.com/bradzacher) for tips on how to handle TypeScript ASTs.
* Everyone who worked on Meta's Hack GraphQL server, the developer experince of which inspired this project.
* A number of other projects which seem to have explored similar ideas in the past:
* [ts2gql](https://github.com/convoyinc/ts2gql)
* [ts2graphql](https://github.com/cevek/ts2graphql)
* [typegraphql-reflection-poc](https://github.com/MichalLytek/typegraphql-reflection-poc)
- [@mofeiZ](https://github.com/mofeiZ) and [@alunyov](https://github/alunyov) for their Relay hack-week project exploring a similar idea.
- [@josephsavona](https://github.com/josephsavona) for input on the design of [Relay Resolvers](https://relay.dev/docs/guides/relay-resolvers/) which inspired this project.
- [@bradzacher](https://github.com/bradzacher) for tips on how to handle TypeScript ASTs.
- Everyone who worked on Meta's Hack GraphQL server, the developer experince of which inspired this project.
- A number of other projects which seem to have explored similar ideas in the past:
- [ts2gql](https://github.com/convoyinc/ts2gql)
- [ts2graphql](https://github.com/cevek/ts2graphql)
- [typegraphql-reflection-poc](https://github.com/MichalLytek/typegraphql-reflection-poc)
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc