MichalLytek / typegraphql-prisma

Prisma generator to emit TypeGraphQL types and CRUD resolvers from your Prisma schema
https://prisma.typegraphql.com
MIT License
891 stars 113 forks source link

Not Clear How To User CustomScalars with a FieldResolver #333

Closed lemiesz closed 2 years ago

lemiesz commented 2 years ago

Describe the issue The documentations is not clear how to use a custom scalar on a dynamically resolver field.

In my code I have the following code.

// Just random crap here for now until a figure out a way to implement it properly. 
export const RuleAstScalar = new GraphQLScalarType({
    name: 'RuleAstScalar',
    description: 'The full AST representing a single rule up to the leaf node',
    serialize(): string {
        return 'something';
    },
    parseValue(): RuleSyntaxTree {
        return {
            children: [],
            ruleId: '1',
            ruleOperation: RuleOperationInternal['AND'],
        };
    },
    parseLiteral(ast): RuleNode {
        return new RuleNode({
            children: [],
            ruleId: '1',
            ruleOperation: RuleOperationInternal['AND'],
        }); // value from the client query
    },
});

@ObjectType()
export class Rule {
    constructor({ parentLearningPathId, ruleId }: Rule) {
        return {
            parentLearningPathId,
            ruleId,
        };
    }

    @Field(() => ID)
    ruleId: string;

    @Field(() => ID)
    parentLearningPathId: string;

    // Field resolver
    @Field((type) => RuleAstScalar, { nullable: true })
    readonly ruleAst?: RuleNode;
} 

@Resolver((of) => Rule)
export default class RuleResolver implements ResolverInterface<Rule> {
    @FieldResolver()
    public async ruleAst(@Root() root: Rule, @Ctx() ctx: GqlSchemaContext): Promise<RuleSyntaxTree> {
        const { ruleDataSource } = ctx.dataSources;
        return await ruleDataSource.getResolvedRuleTree(root.ruleId);
    }
}

However seems that the RuleAst field resolver is not being called the same way that it would be with NonCustomScalar types.

lemiesz commented 2 years ago

Nvm I think i misunderstood something

lemiesz commented 2 years ago

feel free to delete this issue. The problem was with my constructor

    constructor({ parentLearningPathId, ruleId }: Rule) {
        return {
            parentLearningPathId,
            ruleId,
        };
    }

Im not sure why was even doing this. was pattern matching something else in the project mindlessly.