-
-
Notifications
You must be signed in to change notification settings - Fork 677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Custom Scalar as argument #65
Comments
Ah! I needed to map the scalar in |
Yes, you can provide the custom scalar to the class SampleResolver {
@Query()
sampleQuery(@Arg(type => CustomScalar) data: any): boolean {
return true;
}
} |
@19majkel94 are you sure the former is true? In the |
For getting query/mutation/resolver arg, it calls if (type instanceof GraphQLScalarType) {
return type;
} |
That's strange. I've just experienced this same issue again, check this out: Custom Scalar: import { GraphQLScalarType, Kind } from 'graphql';
import { PaginationDirection } from '../input/PaginationArgs';
export interface CursorID {
id: number | string;
direction: PaginationDirection;
}
export interface CursorPaged {
skip: number;
}
export type Cursor = CursorID | CursorPaged;
export const CursorScalar = new GraphQLScalarType({
name: 'Cursor',
description: 'Pagination cursor',
parseValue(value: string): Cursor {
return JSON.parse(Buffer.from(value, 'base64').toString('ascii'));
},
serialize(value: Cursor) {
return Buffer.from(JSON.stringify(value)).toString('base64');
},
parseLiteral(ast): Cursor | null {
if (ast.kind === Kind.STRING) {
return JSON.parse(Buffer.from(ast.value, 'base64').toString('ascii'));
}
return null;
},
}); Argument in my resolver: @Arg('cursor', type => CursorScalar, { nullable: true })
cursor: Cursor Error:
Am I doing something wrong @19majkel94 ? |
There's a discrepancy between const schema = await TypeGraphQL.buildSchema({
resolvers: [`${__dirname}/resolvers/**/*.ts`],
scalarsMap: [{ type: Cursor, scalar: CursorScalar }],
}); This results in a type warning:
Which is strange, since my |
Try Maybe I should hide |
Upgrading my |
Can you compile the app using
It should look something like this: __decorate([
src_1.Query(returns => [recipe_type_1.Recipe]),
__metadata("design:type", Function),
__metadata("design:paramtypes", []),
__metadata("design:returntype", Promise)
], ExampleResolver.prototype, "recipes", null);
__decorate([
src_1.Authorized() // only logged users can add new recipe
,
src_1.Mutation(),
__param(0, src_1.Arg("title")),
__param(1, src_1.Arg("description", { nullable: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String, String]),
__metadata("design:returntype", recipe_type_1.Recipe)
], ExampleResolver.prototype, "addRecipe", null);
__decorate([
src_1.Authorized("ADMIN") // only admin can remove the published recipe
,
src_1.Mutation(),
__param(0, src_1.Arg("title")),
__metadata("design:type", Function),
__metadata("design:paramtypes", [String]),
__metadata("design:returntype", Boolean)
], ExampleResolver.prototype, "deleteRecipe", null);
ExampleResolver = __decorate([
src_1.Resolver()
], ExampleResolver);
exports.ExampleResolver = ExampleResolver; I wounder to what the |
__decorate([
type_graphql_1.FieldResolver(),
__param(0, type_graphql_1.Root()),
__param(1, type_graphql_1.Arg('cursor', type => Cursor_1.CursorScalar, { nullable: true })),
__param(2, type_graphql_1.Arg('limit', { nullable: true })),
__param(3, type_graphql_1.Arg('direction', type => PaginationArgs_1.PaginationDirection, { nullable: true })),
__metadata("design:type", Function),
__metadata("design:paramtypes", [University_1.University, Object, Number, String]),
__metadata("design:returntype", Promise)
], UniversityResolver.prototype, "technologies", null); I'm fairly new to Typescript so I'm not sure exactly what I'm looking at. Would it be the I've added |
Object might be ignored but it works:
Unfortunately I don't know from the stacktrace on which argument it failed. Can you create a minimal reproducible example for that case? I would then be able to debug it and fix 😉 |
Here you go: https://github.com/ashleyw/type-graphql-issue-65-example :) |
Ok, now I see that. Your scalar parse returns custom object, which goes through the if that checks the type of data only, not the target type: if (simpleTypes.includes(data.constructor)) {
return data;
} Fixed in 4176ed2 🎉 |
Is it possible to use a custom scalar as an argument in a resolver? It appears like
convertToType
expects aClassType
, butGraphQLScalarType
returns an object?Thanks!
The text was updated successfully, but these errors were encountered: