@homebound/graphql-typescript-simple-resolvers
Advanced tools
Comparing version 1.31.0 to 1.32.0
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.plugin = void 0; | ||
const graphql_1 = require("graphql"); | ||
@@ -8,5 +9,9 @@ const change_case_1 = require("change-case"); | ||
const types_1 = require("./types"); | ||
const builtInScalars = ["Int", "Boolean", "String", "ID", "Float"]; | ||
const GraphQLScalarTypeSymbol = ts_poet_1.imp("GraphQLScalarType@graphql"); | ||
const GraphQLResolveInfo = ts_poet_1.imp("GraphQLResolveInfo@graphql"); | ||
const builtInScalarsImps = ["Int", "Boolean", "String", "ID", "Float"]; | ||
const GraphQLScalarTypeSymbolImp = ts_poet_1.imp("GraphQLScalarType@graphql"); | ||
const GraphQLResolveInfoImp = ts_poet_1.imp("GraphQLResolveInfo@graphql"); | ||
const GraphQLSchemaImp = ts_poet_1.imp("GraphQLSchema@graphql"); | ||
const DocumentNodeImp = ts_poet_1.imp("DocumentNode@graphql"); | ||
const GraphQLFieldResolverImp = ts_poet_1.imp("GraphQLFieldResolver@graphql"); | ||
const ExecutionResultImp = ts_poet_1.imp("ExecutionResult@graphql"); | ||
/** | ||
@@ -62,5 +67,5 @@ * Generates Resolver/server-side type definitions for an Apollo-based GraphQL implementation. | ||
${scalars | ||
.filter(s => !builtInScalars.includes(s.name)) | ||
.filter(s => !builtInScalarsImps.includes(s.name)) | ||
.map(s => { | ||
return ts_poet_1.code `${s.name}: ${GraphQLScalarTypeSymbol};`; | ||
return ts_poet_1.code `${s.name}: ${GraphQLScalarTypeSymbolImp};`; | ||
})} | ||
@@ -91,3 +96,8 @@ } | ||
const result = types_1.mapType(config, interfaceToImpls, f.type); | ||
return ts_poet_1.code `${f.name}: Resolver<${root}, ${args}, ${result}>;`; | ||
if (types_1.isSubscriptionType(type)) { | ||
return ts_poet_1.code `${f.name}: SubscriptionResolver<${root}, ${args}>;`; | ||
} | ||
else { | ||
return ts_poet_1.code `${f.name}: Resolver<${root}, ${args}, ${result}>;`; | ||
} | ||
})} | ||
@@ -98,4 +108,22 @@ } | ||
chunks.push(ts_poet_1.code ` | ||
export type Resolver<R, A, T> = (root: R, args: A, ctx: ${ctx}, info: ${GraphQLResolveInfo}) => T | Promise<T>; | ||
export type Resolver<R, A, T> = (root: R, args: A, ctx: ${ctx}, info: ${GraphQLResolveInfoImp}) => T | Promise<T>; | ||
`); | ||
// SubscriptionResolver.subscribe based on `SubscriptionArgs` and `subscribe` function | ||
// defined in the "graphql" package. We've added some typing for the rootValue, contextValue | ||
// and variableValues. Note: AsyncIterableIterator requires "esnext.asynciterable" to be defined | ||
// in the "lib" property in tsconfig.json. | ||
chunks.push(ts_poet_1.code ` | ||
export type SubscriptionResolver<R, A> = { | ||
subscribe: ( | ||
schema: ${GraphQLSchemaImp}, | ||
document: ${DocumentNodeImp}, | ||
rootValue?: R, | ||
contextValue?: ${ctx}, | ||
variableValues?: A, | ||
operationName?: string, | ||
fieldResolver?: ${GraphQLFieldResolverImp}<any, any>, | ||
subscribeFieldResolver?: ${GraphQLFieldResolverImp}<any, any>, | ||
) => Promise<AsyncIterableIterator<${ExecutionResultImp}> | ${ExecutionResultImp}>; | ||
}; | ||
`); | ||
argDefs.forEach(a => chunks.push(a)); | ||
@@ -102,0 +130,0 @@ } |
"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; | ||
const graphql_1 = require("graphql"); | ||
@@ -136,2 +137,6 @@ const ts_poet_1 = require("ts-poet"); | ||
exports.isQueryOrMutationType = isQueryOrMutationType; | ||
function isSubscriptionType(type) { | ||
return type.name === "Subscription"; | ||
} | ||
exports.isSubscriptionType = isSubscriptionType; | ||
function isMappedType(type, config) { | ||
@@ -138,0 +143,0 @@ return !!config.mappers[type.name]; |
@@ -0,4 +1,5 @@ | ||
export type Context = { | ||
userId?: string; | ||
}; | ||
export type Context = any; | ||
export type AuthorId = string; | ||
@@ -5,0 +6,0 @@ |
import { Context, AuthorId, Popularity } from "./entities"; | ||
import { GraphQLResolveInfo, GraphQLScalarType } from "graphql"; | ||
import { | ||
GraphQLResolveInfo, | ||
GraphQLSchema, | ||
DocumentNode, | ||
GraphQLFieldResolver, | ||
ExecutionResult, | ||
GraphQLScalarType, | ||
} from "graphql"; | ||
export interface Resolvers { | ||
Author: AuthorResolvers; | ||
Query: QueryResolvers; | ||
Author: AuthorResolvers; | ||
Mutation: MutationResolvers; | ||
AuthorSummary?: AuthorSummaryResolvers; | ||
Book?: BookResolvers; | ||
Container?: ContainerResolvers; | ||
Subscription?: SubscriptionResolvers; | ||
SaveAuthorResult?: SaveAuthorResultResolvers; | ||
Container?: ContainerResolvers; | ||
Date: GraphQLScalarType; | ||
@@ -16,8 +24,2 @@ DateTime: GraphQLScalarType; | ||
export interface QueryResolvers { | ||
authors: Resolver<{}, QueryAuthorsArgs, AuthorId[]>; | ||
authorSummaries: Resolver<{}, {}, AuthorSummary[]>; | ||
search: Resolver<{}, QuerySearchArgs, Array<AuthorId | Book>>; | ||
} | ||
export interface AuthorResolvers { | ||
@@ -33,2 +35,8 @@ name: Resolver<AuthorId, {}, string>; | ||
export interface QueryResolvers { | ||
authors: Resolver<{}, QueryAuthorsArgs, AuthorId[]>; | ||
authorSummaries: Resolver<{}, {}, AuthorSummary[]>; | ||
search: Resolver<{}, QuerySearchArgs, Array<AuthorId | Book>>; | ||
} | ||
export interface MutationResolvers { | ||
@@ -49,6 +57,2 @@ saveAuthor: Resolver<{}, MutationSaveAuthorArgs, SaveAuthorResult>; | ||
export interface SaveAuthorResultResolvers { | ||
author: Resolver<SaveAuthorResult, {}, AuthorId>; | ||
} | ||
export interface ContainerResolvers { | ||
@@ -61,4 +65,26 @@ thingOptional: Resolver<Container, {}, null | undefined | AuthorId | HasName>; | ||
export interface SubscriptionResolvers { | ||
authorSaved: SubscriptionResolver<Subscription, {}>; | ||
searchSub: SubscriptionResolver<Subscription, SubscriptionSearchSubArgs>; | ||
} | ||
export interface SaveAuthorResultResolvers { | ||
author: Resolver<SaveAuthorResult, {}, AuthorId>; | ||
} | ||
export type Resolver<R, A, T> = (root: R, args: A, ctx: Context, info: GraphQLResolveInfo) => T | Promise<T>; | ||
export type SubscriptionResolver<R, A> = { | ||
subscribe: ( | ||
schema: GraphQLSchema, | ||
document: DocumentNode, | ||
rootValue?: R, | ||
contextValue?: Context, | ||
variableValues?: A, | ||
operationName?: string, | ||
fieldResolver?: GraphQLFieldResolver<any, any>, | ||
subscribeFieldResolver?: GraphQLFieldResolver<any, any>, | ||
) => Promise<AsyncIterableIterator<ExecutionResult> | ExecutionResult>; | ||
}; | ||
export interface QueryAuthorsArgs { | ||
@@ -73,2 +99,5 @@ id?: string | null | undefined; | ||
} | ||
export interface SubscriptionSearchSubArgs { | ||
query: string; | ||
} | ||
export interface AuthorSummary { | ||
@@ -85,6 +114,2 @@ numberOfBooks: number; | ||
export interface SaveAuthorResult { | ||
author: AuthorId; | ||
} | ||
export interface Container { | ||
@@ -97,2 +122,11 @@ thingOptional: null | undefined | AuthorId | HasName; | ||
export interface Subscription { | ||
authorSaved: AuthorId; | ||
searchSub: Array<AuthorId | Book>; | ||
} | ||
export interface SaveAuthorResult { | ||
author: AuthorId; | ||
} | ||
export interface HasName { | ||
@@ -99,0 +133,0 @@ name: string; |
@@ -1,2 +0,6 @@ | ||
import { AuthorResolvers, ContainerResolvers } from "./graphql-types"; | ||
import { | ||
AuthorResolvers, | ||
ContainerResolvers, | ||
SubscriptionResolvers, | ||
} from "./graphql-types"; | ||
@@ -9,3 +13,6 @@ const canReturnUndefined: Pick<AuthorResolvers, "birthday"> = { | ||
const canReturnMappedTypesForInterfaces: Pick<ContainerResolvers, "thingOptional"> = { | ||
const canReturnMappedTypesForInterfaces: Pick< | ||
ContainerResolvers, | ||
"thingOptional" | ||
> = { | ||
thingOptional() { | ||
@@ -21,1 +28,18 @@ return "a:1"; | ||
}; | ||
const subInterfaces: Pick<SubscriptionResolvers, "authorSaved"> = { | ||
authorSaved: { | ||
subscribe: ( | ||
schema, | ||
document, | ||
root, | ||
ctx, | ||
vars, | ||
operationName, | ||
fieldResolver, | ||
subscribeFieldResolver, | ||
) => { | ||
return Promise.resolve({}); | ||
}, | ||
}, | ||
}; |
{ | ||
"name": "@homebound/graphql-typescript-simple-resolvers", | ||
"version": "1.31.0", | ||
"version": "1.32.0", | ||
"main": "./build/index.js", | ||
@@ -28,3 +28,3 @@ "types": "./build/", | ||
"ts-jest": "^24.0.2", | ||
"typescript": "^3.6.3" | ||
"typescript": "^3.9.7" | ||
}, | ||
@@ -31,0 +31,0 @@ "husky": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
27915
15
541