@homebound/graphql-typescript-simple-resolvers
Advanced tools
Comparing version 1.33.0 to 1.36.0
@@ -19,2 +19,3 @@ "use strict"; | ||
const chunks = []; | ||
const interfaceTypes = Object.values(schema.getTypeMap()).filter(graphql_1.isInterfaceType); | ||
const typesThatNeedResolvers = Object.values(schema.getTypeMap()) | ||
@@ -41,6 +42,7 @@ .filter(types_1.isObjectType) | ||
generateTopLevelResolversType(chunks, typesThatMayHaveResolvers, typesThatNeedResolvers, scalars); | ||
// Make generic resolvers for interfaces | ||
generateEachInterfaceResolverType(chunks, config, interfaceToImpls, interfaceTypes); | ||
// Make each resolver for any output type, whether its required or optional | ||
generateEachResolverType(chunks, config, interfaceToImpls, allTypesWithResolvers); | ||
// For the output types with optional resolvers, make DTOs for them. Mapped types don't need DTOs. | ||
const interfaceTypes = Object.values(schema.getTypeMap()).filter(graphql_1.isInterfaceType); | ||
generateDtosForNonMappedTypes(chunks, config, interfaceToImpls, [...typesThatMayHaveResolvers, ...interfaceTypes]); | ||
@@ -51,2 +53,4 @@ // Input types | ||
generateEnums(chunks, config, schema); | ||
// Union Types | ||
generateUnionTypes(chunks, config, schema); | ||
const content = await ts_poet_1.code `${chunks}`.toStringWithImports(); | ||
@@ -73,2 +77,13 @@ return { content }; | ||
} | ||
function generateEachInterfaceResolverType(chunks, config, interfaceToImpls, allTypesWithResolvers) { | ||
const argDefs = []; | ||
allTypesWithResolvers.forEach(type => { | ||
chunks.push(ts_poet_1.code ` | ||
export interface ${type.name}Resolvers<T> { | ||
${generateFieldSignature(type, config, interfaceToImpls, argDefs)} | ||
} | ||
`); | ||
}); | ||
argDefs.forEach(a => chunks.push(a)); | ||
} | ||
function generateEachResolverType(chunks, config, interfaceToImpls, allTypesWithResolvers) { | ||
@@ -78,25 +93,6 @@ const ctx = types_1.toImp(config.contextType); | ||
allTypesWithResolvers.forEach(type => { | ||
const root = types_1.mapObjectType(config, type); | ||
chunks.push(ts_poet_1.code ` | ||
export interface ${type.name}Resolvers { | ||
${Object.values(type.getFields()).map(f => { | ||
const argsName = `${type.name}${upper_case_first_1.upperCaseFirst(f.name)}Args`; | ||
const args = f.args.length > 0 ? argsName : "{}"; | ||
if (f.args.length > 0) { | ||
argDefs.push(ts_poet_1.code ` | ||
export interface ${argsName} { | ||
${f.args.map(a => { | ||
const maybeOptional = graphql_1.isNullableType(a.type) ? "?" : ""; | ||
return ts_poet_1.code `${a.name}${maybeOptional}: ${types_1.mapType(config, interfaceToImpls, a.type)}; `; | ||
})} | ||
}`); | ||
} | ||
const root = types_1.mapObjectType(config, type); | ||
const result = types_1.mapType(config, interfaceToImpls, f.type); | ||
if (types_1.isSubscriptionType(type)) { | ||
return ts_poet_1.code `${f.name}: SubscriptionResolver<${root}, ${args}, ${result}>;`; | ||
} | ||
else { | ||
return ts_poet_1.code `${f.name}: Resolver<${root}, ${args}, ${result}>;`; | ||
} | ||
})} | ||
export interface ${type.name}Resolvers ${extendInterfaces(type, root)} { | ||
${generateFieldSignature(type, config, interfaceToImpls, argDefs)} | ||
} | ||
@@ -120,2 +116,36 @@ `); | ||
} | ||
function generateFieldSignature(type, config, interfaceToImpls, argDefs) { | ||
// For GraphQLObjectType, don't include fields which are coming from an implemented interface | ||
const excludeFields = type instanceof graphql_1.GraphQLObjectType ? type.getInterfaces().flatMap(i => Object.keys(i.getFields())) : []; | ||
return Object.values(type.getFields()) | ||
.filter(f => !excludeFields.includes(f.name)) | ||
.map(f => { | ||
const argsName = `${type.name}${upper_case_first_1.upperCaseFirst(f.name)}Args`; | ||
const args = f.args.length > 0 ? argsName : "{}"; | ||
if (f.args.length > 0) { | ||
argDefs.push(ts_poet_1.code ` | ||
export interface ${argsName} { | ||
${f.args.map(a => { | ||
const maybeOptional = graphql_1.isNullableType(a.type) ? "?" : ""; | ||
return ts_poet_1.code `${a.name}${maybeOptional}: ${types_1.mapType(config, interfaceToImpls, a.type)}; `; | ||
})} | ||
}`); | ||
} | ||
const root = type instanceof graphql_1.GraphQLObjectType ? types_1.mapObjectType(config, type) : "T"; | ||
const result = types_1.mapType(config, interfaceToImpls, f.type); | ||
if (types_1.isSubscriptionType(type)) { | ||
return ts_poet_1.code `${f.name}: SubscriptionResolver<${root}, ${args}, ${result}>;`; | ||
} | ||
else { | ||
return ts_poet_1.code `${f.name}: Resolver<${root}, ${args}, ${result}>;`; | ||
} | ||
}); | ||
} | ||
function extendInterfaces(type, root) { | ||
const interfaces = type | ||
.getInterfaces() | ||
.map(i => ts_poet_1.code `${i.name}Resolvers<${root}>`) | ||
.join(", "); | ||
return interfaces ? `extends ${interfaces}` : ""; | ||
} | ||
function generateDtosForNonMappedTypes(chunks, config, interfaceToImpls, types) { | ||
@@ -168,2 +198,12 @@ types.forEach(type => { | ||
} | ||
function generateUnionTypes(chunks, config, schema) { | ||
Object.values(schema.getTypeMap()) | ||
.filter(graphql_1.isUnionType) | ||
.filter(types_1.isNotMetadataType) | ||
.forEach(type => { | ||
chunks.push(ts_poet_1.code ` | ||
export type ${type.name} = ${types_1.joinCodes(type.getTypes().map(t => types_1.mapObjectType(config, t)), " | ")} | ||
`); | ||
}); | ||
} | ||
function needsResolver(config, t) { | ||
@@ -170,0 +210,0 @@ return types_1.isNotMetadataType(t) && (types_1.isMappedType(t, config) || types_1.isQueryOrMutationType(t)); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.isMappedType = exports.isSubscriptionType = exports.isQueryOrMutationType = exports.isNotMetadataType = exports.isScalarType = exports.isEnumType = exports.isInputObjectType = exports.isNonNullType = exports.isObjectType = exports.toImp = exports.mapInterfaceType = exports.mapObjectType = exports.mapType = void 0; | ||
exports.joinCodes = exports.isMappedType = exports.isSubscriptionType = exports.isQueryOrMutationType = exports.isNotMetadataType = exports.isScalarType = exports.isEnumType = exports.isInputObjectType = exports.isNonNullType = exports.isObjectType = exports.toImp = exports.mapInterfaceType = exports.mapObjectType = exports.mapType = void 0; | ||
const graphql_1 = require("graphql"); | ||
@@ -43,3 +43,3 @@ const ts_poet_1 = require("ts-poet"); | ||
else if (type instanceof graphql_1.GraphQLUnionType) { | ||
return joinCodes(type.getTypes().map(t => mapObjectType(config, t)), " | "); | ||
return type.name; | ||
} | ||
@@ -157,1 +157,2 @@ else { | ||
} | ||
exports.joinCodes = joinCodes; |
@@ -17,4 +17,14 @@ import { Context, AuthorId, Popularity } from "./entities"; | ||
export interface AuthorResolvers { | ||
name: Resolver<AuthorId, {}, string>; | ||
export interface HasNameResolvers<T> { | ||
name: Resolver<T, {}, string>; | ||
} | ||
export interface FieldWithArgsResolvers<T> { | ||
field1: Resolver<T, FieldWithArgsField1Args, boolean | null | undefined>; | ||
} | ||
export interface FieldWithArgsField1Args { | ||
input?: boolean | null | undefined; | ||
} | ||
export interface AuthorResolvers extends HasNameResolvers<AuthorId>, FieldWithArgsResolvers<AuthorId> { | ||
summary: Resolver<AuthorId, {}, AuthorSummary>; | ||
@@ -31,3 +41,4 @@ popularity: Resolver<AuthorId, {}, Popularity>; | ||
authorSummaries: Resolver<{}, {}, AuthorSummary[]>; | ||
search: Resolver<{}, QuerySearchArgs, Array<AuthorId | Book>>; | ||
search: Resolver<{}, QuerySearchArgs, SearchResult[]>; | ||
testUnionOfUnions: Resolver<{}, {}, UnionOfUnions | null | undefined>; | ||
} | ||
@@ -44,6 +55,5 @@ | ||
export interface BookResolvers { | ||
name: Resolver<Book, {}, string>; | ||
unionProp: Resolver<Book, {}, null | undefined | String | Boolean>; | ||
reqUnionProp: Resolver<Book, {}, String | Boolean>; | ||
export interface BookResolvers extends HasNameResolvers<Book>, FieldWithArgsResolvers<Book> { | ||
unionProp: Resolver<Book, {}, UnionProp | null | undefined>; | ||
reqUnionProp: Resolver<Book, {}, UnionProp>; | ||
} | ||
@@ -60,3 +70,3 @@ | ||
authorSaved: SubscriptionResolver<Subscription, {}, AuthorId>; | ||
searchSub: SubscriptionResolver<Subscription, SubscriptionSearchSubArgs, Array<AuthorId | Book>>; | ||
searchSub: SubscriptionResolver<Subscription, SubscriptionSearchSubArgs, SearchResult[]>; | ||
} | ||
@@ -99,4 +109,5 @@ | ||
name: string; | ||
unionProp: null | undefined | String | Boolean; | ||
reqUnionProp: String | Boolean; | ||
unionProp: UnionProp | null | undefined; | ||
reqUnionProp: UnionProp; | ||
field1: boolean | null | undefined; | ||
} | ||
@@ -113,3 +124,3 @@ | ||
authorSaved: AuthorId; | ||
searchSub: Array<AuthorId | Book>; | ||
searchSub: SearchResult[]; | ||
} | ||
@@ -125,2 +136,6 @@ | ||
export interface FieldWithArgs { | ||
field1: boolean | null | undefined; | ||
} | ||
export interface AuthorInput { | ||
@@ -136,1 +151,7 @@ name?: string | null | undefined; | ||
} | ||
export type UnionProp = String | Boolean; | ||
export type SearchResult = AuthorId | Book; | ||
export type UnionOfUnions = UnionProp | SearchResult; |
{ | ||
"name": "@homebound/graphql-typescript-simple-resolvers", | ||
"version": "1.33.0", | ||
"version": "1.36.0", | ||
"main": "./build/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./build/", |
Sorry, the diff of this file is not supported yet
30519
581