Args removed from derived resolvers after first call #1091
-
Hi all, I have been seeing a very strange issue with my derived Resolver classes. On my first call to the endpoint, I am able to get the data I would expect and see that the correct GQL schema is generated: # -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------
input DefinitionFilterInputType {
id: String
nameLike: String
}
input DefinitionOrder {
sortAscending: Boolean = true
sortByField: String!
}
type Query {
views(filter: DefinitionFilterInputType, order: DefinitionOrder): [ViewResults!]!
}
type ViewDefinition {
displayName: String!
name: String!
}
type ViewResults {
viewDefinition: ViewDefinition!
} However, any subsequent calls will rewrite the schema, stripping my args and the input types, and causing "Unknown type" errors to be thrown. # -----------------------------------------------
# !!! THIS FILE WAS GENERATED BY TYPE-GRAPHQL !!!
# !!! DO NOT MODIFY THIS FILE BY YOURSELF !!!
# -----------------------------------------------
type Query {
views: [ViewResults!]!
}
type ViewDefinition {
displayName: String!
name: String!
}
type ViewResults {
viewDefinition: ViewDefinition!
} Here is my BaseResolver class following the resolver inheritance docs: // ResolverBase.ts
import { Args, ClassType, Ctx, Query, Resolver, Root } from "type-graphql";
import { AppDataArgs, DefinitionFilterInputType, DefinitionOrder } from "./args";
import { DefinitionService, QueryService } from "./services";
export function AppDataResolver<TResultType, TAppDataResultType>(
suffix: string,
appDataResultType: ClassType<TAppDataResultType>
) {
@Resolver(() => appDataResultType, {isAbstract: true})
abstract class AppDataBaseResolver {
constructor(protected definitionService: DefinitionService, protected queryService: QueryService) { }
abstract getAppData(filter?: DefinitionFilterInputType, order?: DefinitionOrder): Promise<TResultType[]>;
@Query(() => [appDataResultType], {name: suffix})
async get(
@Ctx() token: string,
@Args() {filter, order}: AppDataArgs
): Promise<TAppDataResultType[]> {
const allData = await this.getAppData(filter, order);
return allData.map(def => new appDataResultType(def));
}
}
return AppDataBaseResolver;
} I also have a ResultBase abstract object and combine them to create my resolver and expected result type in resolvers.ts: // ResultBase.ts
import { ClassType, Field, ObjectType } from "type-graphql";
export default function AppDataResults<TDefinition>(suffix: string, TDefinitionClass: ClassType<TDefinition>){
@ObjectType({isAbstract: true})
abstract class AppDataResultsClass{
@Field(() => TDefinitionClass, {name: `${suffix}Definition`})
definition: TDefinition;
constructor(definition: TDefinition) {
this.definition = definition;
}
}
return AppDataResultsClass;
}
//resolver.ts
import { Field, ObjectType, Resolver } from "type-graphql";
import { Service } from "typedi";
import { DefinitionFilterInputType, DefinitionOrder } from "./args";
import { AppDataResolver } from "./ResolverBase";
import AppDataResults from "./ResultBase";
@ObjectType()
class ViewDefinition {
@Field()
name: string;
@Field()
displayName: string;
}
@ObjectType()
class ViewResults extends AppDataResults("view", ViewDefinition) { }
const ViewBaseResolver = AppDataResolver<ViewDefinition, ViewResults>("views", ViewResults);
@Service()
@Resolver( () => ViewResults)
export class ViewResultsResolver extends ViewBaseResolver {
getAppData(filter?: DefinitionFilterInputType, order?: DefinitionOrder): Promise<ViewDefinition[]> {
return Promise.resolve([{
name: "sample",
displayName: "Sample"
}])
}
} I spun up this sample repo which should be able to reproduce what I'm seeing: https://github.com/jnis23/type-gql-resolver-sample Is there something I am missing here? Any help would be greatly appreciated! Here is my query and vars: query views($filter: DefinitionFilterInputType, $order: DefinitionOrder) {
views(filter: $filter, order: $order) {
viewDefinition{
name
displayName
}
}
} Vars: {
"filter": {
"id": "1f3wa2"
},
"order": {
"sortByField": "name"
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Something is wrong with your setup. You should build the schema only once. Then all following requests are executed against the same schema. |
Beta Was this translation helpful? Give feedback.
Something is wrong with your setup. You should build the schema only once. Then all following requests are executed against the same schema.