Comparing version 0.9.11 to 0.10.0-alpha.1
@@ -52,3 +52,3 @@ import { | ||
import { TypegenFormatFn } from "./typegenFormatPrettier"; | ||
import { GetGen } from "./typegenTypeHelpers"; | ||
import { GetGen, AuthorizeResolver } from "./typegenTypeHelpers"; | ||
export declare type Maybe<T> = T | null; | ||
@@ -294,2 +294,6 @@ export interface BuilderConfig { | ||
} | ||
export declare function wrapAuthorize( | ||
resolver: GraphQLFieldResolver<any, any>, | ||
authorize: AuthorizeResolver<string, any> | ||
): GraphQLFieldResolver<any, any>; | ||
export interface BuildTypes< | ||
@@ -296,0 +300,0 @@ TypeMapDefs extends Record<string, GraphQLNamedType> |
@@ -542,2 +542,5 @@ "use strict"; | ||
} | ||
if (fieldOptions.authorize) { | ||
resolver = wrapAuthorize(resolver || graphql_1.defaultFieldResolver, fieldOptions.authorize); | ||
} | ||
return resolver; | ||
@@ -557,2 +560,29 @@ }; | ||
} | ||
function wrapAuthorize(resolver, authorize) { | ||
var _this = this; | ||
var nexusAuthWrapped = function (root, args, ctx, info) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var authResult, fieldName, parentTypeName; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, authorize(root, args, ctx, info)]; | ||
case 1: | ||
authResult = _a.sent(); | ||
if (authResult === true) { | ||
return [2 /*return*/, resolver(root, args, ctx, info)]; | ||
} | ||
if (authResult === false) { | ||
throw new Error("Not authorized"); | ||
} | ||
if (authResult instanceof Error) { | ||
throw authResult; | ||
} | ||
fieldName = info.fieldName, parentTypeName = info.parentType.name; | ||
throw new Error("Nexus authorize for " + parentTypeName + "." + fieldName + " Expected a boolean or Error, saw " + authResult); | ||
} | ||
}); | ||
}); }; | ||
nexusAuthWrapped.nexusWrappedResolver = resolver; | ||
return nexusAuthWrapped; | ||
} | ||
exports.wrapAuthorize = wrapAuthorize; | ||
/** | ||
@@ -559,0 +589,0 @@ * Builds the types, normalizing the "types" passed into the schema for a |
@@ -5,3 +5,7 @@ import { | ||
GraphQLInputObjectType, | ||
GraphQLFieldResolver, | ||
} from "graphql"; | ||
export declare type WrappedResolver = GraphQLFieldResolver<any, any> & { | ||
nexusWrappedResolver?: GraphQLFieldResolver<any, any>; | ||
}; | ||
export declare type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
@@ -8,0 +12,0 @@ export declare type BaseScalars = "String" | "Int" | "Float" | "ID" | "Boolean"; |
@@ -7,2 +7,3 @@ import { | ||
NeedsResolver, | ||
AuthorizeResolver, | ||
} from "../typegenTypeHelpers"; | ||
@@ -54,2 +55,11 @@ import { NexusArgDef } from "./args"; | ||
resolve?: FieldResolver<TypeName, FieldName>; | ||
/** | ||
* Authorization for an individual field. Returning "true" | ||
* or "Promise<true>" means the field can be accessed. | ||
* Returning "false" or "Promise<false>" will respond | ||
* with a "Not Authorized" error for the field. Returning | ||
* or throwing an error will also prevent the resolver from | ||
* executing. | ||
*/ | ||
authorize?: AuthorizeResolver<TypeName, FieldName>; | ||
} | ||
@@ -56,0 +66,0 @@ export interface NexusOutputFieldConfig< |
@@ -72,3 +72,2 @@ import { | ||
* Specifies a default field resolver for all members of this type. | ||
* Warning: this may break type-safety. | ||
*/ | ||
@@ -75,0 +74,0 @@ defaultResolver?: FieldResolver<TypeName, any>; |
@@ -14,2 +14,3 @@ import { | ||
import { GroupedTypes } from "./utils"; | ||
import { WrappedResolver } from "./definitions/_types"; | ||
declare type TypeFieldMapping = Record< | ||
@@ -62,3 +63,5 @@ string, | ||
hasResolver( | ||
field: GraphQLField<any, any>, | ||
field: GraphQLField<any, any> & { | ||
resolve?: WrappedResolver; | ||
}, | ||
_type: GraphQLObjectType | GraphQLInterfaceType | ||
@@ -65,0 +68,0 @@ ): boolean; |
@@ -298,3 +298,8 @@ "use strict"; | ||
) { | ||
return Boolean(field.resolve); | ||
if (field.resolve) { | ||
if (field.resolve.nexusWrappedResolver !== graphql_1.defaultFieldResolver) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
}; | ||
@@ -301,0 +306,0 @@ Typegen.prototype.printRootTypeMap = function () { |
@@ -15,6 +15,2 @@ import { GraphQLSchema, GraphQLNamedType } from "graphql"; | ||
/** | ||
* Deprecated, use 'source' | ||
*/ | ||
module?: string; | ||
/** | ||
* The module for where to look for the types. | ||
@@ -62,3 +58,3 @@ * This uses the node resolution algorthm via require.resolve, | ||
/** | ||
* Array of files to match for a type | ||
* Array of TypegenConfigSourceModule's to look in and match the type names against. | ||
* | ||
@@ -65,0 +61,0 @@ * ``` |
@@ -78,2 +78,11 @@ import { GraphQLResolveInfo } from "graphql"; | ||
) => MaybePromiseDeep<ResultValue<TypeName, FieldName>>; | ||
export declare type AuthorizeResolver< | ||
TypeName extends string, | ||
FieldName extends string | ||
> = ( | ||
root: RootValue<TypeName>, | ||
args: ArgsValue<TypeName, FieldName>, | ||
context: GetGen<"context">, | ||
info: GraphQLResolveInfo | ||
) => MaybePromise<boolean | Error>; | ||
export declare type AbstractResolveReturn< | ||
@@ -80,0 +89,0 @@ TypeName extends string |
{ | ||
"name": "nexus", | ||
"version": "0.9.11", | ||
"version": "0.10.0-alpha.1", | ||
"main": "dist", | ||
@@ -5,0 +5,0 @@ "types": "dist", |
@@ -35,2 +35,3 @@ import { | ||
isScalarType, | ||
defaultFieldResolver, | ||
} from "graphql"; | ||
@@ -85,2 +86,3 @@ import { NexusArgConfig, NexusArgDef } from "./definitions/args"; | ||
NonNullConfig, | ||
WrappedResolver, | ||
} from "./definitions/_types"; | ||
@@ -90,3 +92,7 @@ import { TypegenAutoConfigOptions } from "./typegenAutoConfig"; | ||
import { TypegenMetadata } from "./typegenMetadata"; | ||
import { AbstractTypeResolver, GetGen } from "./typegenTypeHelpers"; | ||
import { | ||
AbstractTypeResolver, | ||
GetGen, | ||
AuthorizeResolver, | ||
} from "./typegenTypeHelpers"; | ||
import { firstDefined, objValues, suggestionList, isObject } from "./utils"; | ||
@@ -512,5 +518,5 @@ | ||
protected withScalarMethods<T extends NexusGenCustomDefinitionMethods<string>>( | ||
definitionBlock: T | ||
): T { | ||
protected withScalarMethods< | ||
T extends NexusGenCustomDefinitionMethods<string> | ||
>(definitionBlock: T): T { | ||
this.customScalarMethods.forEach(([methodName, typeName]) => { | ||
@@ -895,2 +901,8 @@ // @ts-ignore - Yeah, yeah... we know | ||
} | ||
if (fieldOptions.authorize) { | ||
resolver = wrapAuthorize( | ||
resolver || defaultFieldResolver, | ||
fieldOptions.authorize | ||
); | ||
} | ||
return resolver; | ||
@@ -917,2 +929,29 @@ } | ||
export function wrapAuthorize( | ||
resolver: GraphQLFieldResolver<any, any>, | ||
authorize: AuthorizeResolver<string, any> | ||
): GraphQLFieldResolver<any, any> { | ||
const nexusAuthWrapped: WrappedResolver = async (root, args, ctx, info) => { | ||
const authResult = await authorize(root, args, ctx, info); | ||
if (authResult === true) { | ||
return resolver(root, args, ctx, info); | ||
} | ||
if (authResult === false) { | ||
throw new Error("Not authorized"); | ||
} | ||
if (authResult instanceof Error) { | ||
throw authResult; | ||
} | ||
const { | ||
fieldName, | ||
parentType: { name: parentTypeName }, | ||
} = info; | ||
throw new Error( | ||
`Nexus authorize for ${parentTypeName}.${fieldName} Expected a boolean or Error, saw ${authResult}` | ||
); | ||
}; | ||
nexusAuthWrapped.nexusWrappedResolver = resolver; | ||
return nexusAuthWrapped; | ||
} | ||
export interface BuildTypes< | ||
@@ -919,0 +958,0 @@ TypeMapDefs extends Record<string, GraphQLNamedType> |
@@ -5,4 +5,9 @@ import { | ||
GraphQLInputObjectType, | ||
GraphQLFieldResolver, | ||
} from "graphql"; | ||
export type WrappedResolver = GraphQLFieldResolver<any, any> & { | ||
nexusWrappedResolver?: GraphQLFieldResolver<any, any>; | ||
}; | ||
export type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>; | ||
@@ -9,0 +14,0 @@ |
@@ -7,2 +7,3 @@ import { | ||
NeedsResolver, | ||
AuthorizeResolver, | ||
} from "../typegenTypeHelpers"; | ||
@@ -56,2 +57,11 @@ import { NexusArgDef } from "./args"; | ||
resolve?: FieldResolver<TypeName, FieldName>; | ||
/** | ||
* Authorization for an individual field. Returning "true" | ||
* or "Promise<true>" means the field can be accessed. | ||
* Returning "false" or "Promise<false>" will respond | ||
* with a "Not Authorized" error for the field. Returning | ||
* or throwing an error will also prevent the resolver from | ||
* executing. | ||
*/ | ||
authorize?: AuthorizeResolver<TypeName, FieldName>; | ||
} | ||
@@ -58,0 +68,0 @@ |
@@ -81,3 +81,2 @@ import { | ||
* Specifies a default field resolver for all members of this type. | ||
* Warning: this may break type-safety. | ||
*/ | ||
@@ -84,0 +83,0 @@ defaultResolver?: FieldResolver<TypeName, any>; |
@@ -24,5 +24,7 @@ import { | ||
GraphQLEnumType, | ||
defaultFieldResolver, | ||
} from "graphql"; | ||
import { TypegenInfo } from "./builder"; | ||
import { eachObj, GroupedTypes, groupTypes, mapObj } from "./utils"; | ||
import { WrappedResolver } from "./definitions/_types"; | ||
@@ -348,6 +350,11 @@ const SpecifiedScalars = { | ||
hasResolver( | ||
field: GraphQLField<any, any>, | ||
field: GraphQLField<any, any> & { resolve?: WrappedResolver }, | ||
_type: GraphQLObjectType | GraphQLInterfaceType // Used in tests | ||
) { | ||
return Boolean(field.resolve); | ||
if (field.resolve) { | ||
if (field.resolve.nexusWrappedResolver !== defaultFieldResolver) { | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
@@ -354,0 +361,0 @@ |
@@ -25,6 +25,2 @@ import { | ||
/** | ||
* Deprecated, use 'source' | ||
*/ | ||
module?: string; | ||
/** | ||
* The module for where to look for the types. | ||
@@ -73,3 +69,3 @@ * This uses the node resolution algorthm via require.resolve, | ||
/** | ||
* Array of files to match for a type | ||
* Array of TypegenConfigSourceModule's to look in and match the type names against. | ||
* | ||
@@ -76,0 +72,0 @@ * ``` |
@@ -84,2 +84,12 @@ import { GraphQLResolveInfo } from "graphql"; | ||
export type AuthorizeResolver< | ||
TypeName extends string, | ||
FieldName extends string | ||
> = ( | ||
root: RootValue<TypeName>, | ||
args: ArgsValue<TypeName, FieldName>, | ||
context: GetGen<"context">, | ||
info: GraphQLResolveInfo | ||
) => MaybePromise<boolean | Error>; | ||
export type AbstractResolveReturn< | ||
@@ -86,0 +96,0 @@ TypeName extends string |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
Deprecated
MaintenanceThe maintainer of the package marked it as deprecated. This could indicate that a single version should not be used, or that the package is no longer maintained and any new vulnerabilities will not be fixed.
Found 1 instance in 1 package
542275
8297
0