I’ve defined a simple custom Date scalar, which is used in one of the subgraphs in a federated graph. I have a query using that custom scalar as an input argument. And I’m using lambda as ApolloServer, by using the apollo-server-lambda package.
The problem I’m facing is:
The argument it’s successfully transformed in the parseLiteral function(The log print out the correct string “1990-01-01”). But when I try to get it from the resolver by calling args.date
, it print out a {} instead of the string “1990-01-01”.
Any idea why?
My schema definition:
scalar Date
type Query {
hello(date: Date): String
}
My query:
query{
hello(date:“1990-01-01”)
}
My custom scalar type:
import { format, parseISO } from ‘date-fns’;
import { GraphQLScalarType, Kind } from ‘graphql’;
export const dateScalar = new GraphQLScalarType({
name: ‘Date’,
description: ‘Date custom scalar type’,
serialize(value): string {
try {
return convertToString(value);
} catch (e) {
throw new Error(Failed to convert Date ${value} to string
);
}
},
parseValue(value): string {
console.log(parseValue Date: ${value} ${typeof value}
);
return value as string;
},
parseLiteral(ast) {
console.log(parseLiteral Date: ${ast.kind}
);
if (ast.kind === Kind.STRING) {
console.log(parseLiteral Date: ${ast.value} ${typeof ast.value}
);
return ast.value;
}
return null;
},
});
const dateFormat = ‘yyyy-MM-dd’;
const convertToString = (value: unknown): string => {
switch (typeof value) {
case ‘object’:
return format(value as Date, dateFormat);
case ‘string’:
return format(parseISO(value), dateFormat);
default:
return value as string;
}
};
My resolver:
import { QueryResolvers } from ‘…/…/generated/resolvers-types’;
export const hello: QueryResolvers[‘hello’] = (parent, args, context, info) => {
console.log(Resolver args: ${JSON.stringify(args)}
);
return Hello user(${context.personId}) from PersonClient! ${args.date}
;
};