GraphQLError: Expected type Int, found Int - apollo-server

I have encountered a new type of nonsensical errors (Expected type Int, found Int) when running queries with input parameters (not just Ints). They seem to have appeared since early June (when I upgraded to graphql#14.3.1?)
Our setting is a bit different since we are stitching a remote schema to a local one (overlapping types when they exist or adding them when they are not).
Anyone has seen this type of errors and knows how to correct it?
I have traced the problem to the parseLiteral method of the scalar.js file. In the present case, ast.kind equals EnumValue while its value is IntValue
var GraphQLInt = new _definition.GraphQLScalarType({
name: 'Int',
description: '...',
serialize: serializeInt,
parseValue: coerceInt,
parseLiteral: function parseLiteral(ast) {
if (ast.kind === _kinds.Kind.INT) {
var num = parseInt(ast.value, 10);
if (num <= MAX_INT && num >= MIN_INT) {
return num;
}
}
return undefined;
}
});
It seems as if the parser arbitrarily converts a value of 10 to an EnumValue (of type Int), rather than IntValue.
Here is a typical query
combinedStatus(status: "bad", start:10, end:20) {
status
timestamp
}
I get the following error: GraphQLError: Expected type Int, found Int. at isValidScalar ./node_modules/graphql/validation/rules/ValuesOfCorrectType.js:180:27
the same query with variables will return the expected result and no error:
combinedStatus(status: "bad", start:$start, end:$end) {
status
timestamp
}
The schema is:
type CdsHealthCheckLocal implements CdsHealthCheckInterface {
status: String!
combinedStatus(status: String, start: Int, end: Int): CdsHealthCheck
}
and the corresponding resolver is:
async (parent, { status, start, end }, context) => {
return {
status: `not sure if I am feeling ${status} (${start}, ${end})`
}}

Related

Why am I getting an error when assigning a correct type?

Recently I started experimenting with Go, but I hit on hard rock.
I have this type:
type LocationType string
const (
River LocationType = "River"
Mountain LocationType = "Mountain"
)
func (t LocationType) ToString() string {
return string(t)
}
I also have this one:
type LocationCreateInput struct {
Name string `json:"name,omitempty"`
Type *models.LocationType `json:"type,omitempty"`
}
Now I'm trying to create a new LocationCreateInput variable:
input := &gqlModels.LocationCreateInput {
Name: "Test name",
Type: models.River
}
and I am getting the below error:
Cannot use 'models.Site' (type LocationType) as the type *models.LocationType
Can somebody point me to the right way of assigning the Type value? In the end, it is just a string.
What am I missing here? Could you give me a push?
You are trying to assign a value to a pointer type. So it's not "just a string", it's a "a pointer to just a string".
Either you change the type of the struct field from *models.LocationType to models.LocationType, or you need to take the address when assigning:
val := models.River
input := &gqlModels.LocationCreateInput {
Name: "Test name",
Type: &val,
}

How to get a struct from view! call in near_sdk_sim?

I have a following method:
pub fn get_product(&self, product_id: U128) -> Product {
let product_option = self.product_map.get(&product_id);
match product_option {
Some(product) => product,
None => {
panic!("No products for the id");
}
}
}
Product struct:
#[derive(Default, BorshDeserialize, BorshSerialize)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Product {
pub user_id: u128,
pub product_details_hash: String,
}
When I call in near_sdk_sim I get error
let product:Product = view!(contract.get_product(1.into())).unwrap_json();
Error is:
called `Result::unwrap()` on an `Err` value: Error("EOF while parsing a value", line: 1, column: 0)
How can I get the struct from the view! call?
For future reference questions of this nature are better suited as an issuenear-sdk-rs .
My guess for this issue is that the call paniced and thus there was no value to deserialize. Try checking if it is okay.
let res = view!(contract.get_product(1.into()));
assert!(res.is_ok());

How to set a bool value to drive<Bool>

var readIndicatorNeedsDisplay: Driver<Bool> = .empty()
public func bindcellEvents(readNotificationID: String) {
if let unreadNotificationIDs = UserDefaults.main?.unreadNotificationIDs, unreadNotificationIDs.contains(readNotificationID) {
readIndicatorNeedsDisplay = true
} else {
UserDefaults.main?.unreadNotificationIDs.append(readNotificationID)
readIndicatorNeedsDisplay = false
// Cannot assign value of type 'Bool' to type 'Driver<Bool>' (aka 'SharedSequence<DriverSharingStrategy, Bool>')
}
}
when i assign bool to driver Giving error: Cannot assign value of type 'Bool' to type 'Driver' (aka 'SharedSequence')
You're not supposed to assign values directly to a Driver, and attempting to do so shows your fundamental misunderstanding of RxSwift. You should probably step back, and learn the basics, then come back to this problem.
However, if you want to feed values to a stream, you could use a PublishRelay:
var readIndicatorNeedsDisplay = PublishRelay<Bool>()
public func bindcellEvents(readNotificationID: String) {
if let unreadNotificationIDs = UserDefaults.main?.unreadNotificationIDs, unreadNotificationIDs.contains(readNotificationID) {
readIndicatorNeedsDisplay.accept(true)
} else {
UserDefaults.main?.unreadNotificationIDs.append(readNotificationID)
readIndicatorNeedsDisplay.accept(false)
}
}

How to read a value of an enum which associates with a custom type in Rust?

I have an implementation in Rust as follows. In the main function, I am reading a value in SalaryRange enum and this will display High("So High").
// This can be a complex type, just using string for the question
type SRange = String;
type SalEnu = SalaryRange<SRange>;
struct User<SRange> {
username: String,
email: String,
sign_in_count: u64,
active: bool,
income: Income<SRange>,
}
struct Income<SRange> {
salary_range: SalaryRange<SRange>
}
#[derive(Debug)]
enum SalaryRange<SRange> {
Low(SRange),
Mid(SRange),
High(SRange),
}
fn main() {
let user1 = User {
email: String::from("test#email.com"),
username: String::from("test_name"),
active: true,
sign_in_count: 1,
income: Income {
salary_range: (
SalaryRange::High("So High")
)
},
};
let mut srange: SalaryRange<&str> = user1.income.salary_range;
println!("{:?}", srange);
}
Link for this example can be found here.
Just wanted to know if there is a possibility to read and print the value in that enum as println!("{:?}", srange::High);, just to print out the string value?
I only want to print the value So High.
If I use srange::High This will throw an error saying
println!("{:?}", srange::High);
| ^^^^^^ use of undeclared type or module `srange`
error: aborting due to previous error
You can implement a method on your enum to extract the value:
#[derive(Debug)]
enum SalaryRange<S> {
Low(S),
Mid(S),
High(S),
}
impl<S> SalaryRange<S> {
fn value(&self) -> &S {
match self {
SalaryRange::Low(value) => value,
SalaryRange::Mid(value) => value,
SalaryRange::High(value) => value,
}
}
}
println!("{:?}", srange.value());
You can pattern match srange with the if let syntax.
if let SalaryRange::High(s) = srange {
println!("{}", s);
}
will print "so high".
I know it's been a while since the question has been opened, but I would like to complete Peter's answer.
There is a more idiomatic way to achieve what you want. Just implement the std::fmt::Display trait to your enum as following:
pub enum SalaryRange {
LOW(String),
MID(String),
HIGH(String),
}
impl std::fmt::Display for SalaryRange {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let content = match self {
SalaryRange::LOW(content) => content,
SalaryRange::MID(content) => content,
SalaryRange::HIGH(content) => content,
};
write!(f, "{}", content)
}
}
The std::fmt::Display trait allows you to display the content held by your enum value like this:
let salary_range = SalaryRange::HIGH("So high".to_string());
println!("{}", salary_range);
// outputs: "So high"
This should work with any type.
Playground to test it: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=eaea33a955dc9dcd81a4b96ec22d82bd

Enum as Parameter in TypeScript

Isn't it possible to set the type of a parameter to an Enum? Like this:
private getRandomElementOfEnum(e : enum):string{
var length:number = Object.keys(e).length;
return e[Math.floor((Math.random() * length)+1)];
}
Following error is thrown:
Argument expression expected.(1135)
With any obviously everything is alright:
private getRandomElementOfEnum(e : any):string{
var length:number = Object.keys(e).length;
return e[Math.floor((Math.random() * length)+1)];
}
Is there a possibility or a little workaround to define an enum as a parameter?
It's not possible to ensure the parameter is an enum, because enumerations in TS don't inherit from a common ancestor or interface.
TypeScript brings static analysis. Your code uses dynamic programming with Object.keys and e[dynamicKey]. For dynamic codes, the type any is convenient.
Your code is buggy: length() doesn't exists, e[Math.floor((Math.random() * length)+1)] returns a string or an integer, and the enumeration values can be manually set…
Here is a suggestion:
function getRandomElementOfEnum<E>(e: any): E {
var keys = Object.keys(e),
index = Math.floor(Math.random() * keys.length),
k = keys[index];
if (typeof e[k] === 'number')
return <any>e[k];
return <any>parseInt(k, 10);
}
function display(a: Color) {
console.log(a);
}
enum Color { Blue, Green };
display(getRandomElementOfEnum<Color>(Color));
Ideally, the parameter type any should be replaced by typeof E but the compiler (TS 1.5) can't understand this syntax.
You can do better than any:
enum E1 {
A, B, C
}
enum E2 {
X, Y, Z
}
function getRandomElementOfEnum(e: { [s: number]: string }): number {
/* insert working implementation here */
return undefined;
}
// OK
var x: E1 = getRandomElementOfEnum(E1);
// Error
var y: E2 = getRandomElementOfEnum(window);
// Error
var z: string = getRandomElementOfEnum(E2);
I agree with #Tarh. Enums in TypeScript are just Javascript objects without a common interface or prototype (and if they are const enum, then they are not even objects), so you cannot restrict types to "any enum".
The closest I could get is something like the following:
enum E1 {
A, B, C
}
enum E2 {
X, Y, Z
}
// make up your own interface to match TypeScript enums
// as closely as possible (not perfect, though)
interface Enum {
[id: number]: string
}
function getRandomElementOfEnum(e: Enum): string {
let length = Object.keys(e).length / 2;
return e[Math.floor((Math.random() * length))];
}
This works for all enums (without custom initializers), but it would also accept other arrays as input (and then fail because the method body relies on the very specific key structure that TypeScript enums have).
So unless you have a real need for such a "generic" function, make typesafe functions for the individual enum types (or a union type like E1|E2|E3) that you actually need.
And if you do have this need (and this might very well be an X-Y-problem that can be solved in a better, completely different way given more context), use any, because you have left typesafe territory anyway.
Summing up the previous answers with some new syntax - a generic typesafe function, which works with numeric enums as well as string enums:
function getRandomElementOfEnum<T extends {[key: number]: string | number}>(e: T): T[keyof T] {
const keys = Object.keys(e);
const randomKeyIndex = Math.floor(Math.random() * keys.length);
const randomKey = keys[randomKeyIndex];
// Numeric enums members also get a reverse mapping from enum values to enum names.
// So, if a key is a number, actually it's a value of a numeric enum.
// see https://www.typescriptlang.org/docs/handbook/enums.html#reverse-mappings
const randomKeyNumber = Number(randomKey);
return isNaN(randomKeyNumber)
? e[randomKey as keyof T]
: randomKeyNumber as unknown as T[keyof T];
}
Another possible option not mentioned above is using the actual values. This is however possible only when you know all the options. This, in my opinion is definitely better than any.
doSomething(a: string, b: 'this'|'can'|'work'): void {
//do something
}
Tested on TypeScript 3.9.7
Solution
type EnumTypeString<TEnum extends string> =
{ [key in string]: TEnum | string; }
type EnumTypeNumber<TEnum extends number> =
{ [key in string]: TEnum | number; }
| { [key in number]: string; }
type EnumType<TEnum extends string | number> =
(TEnum extends string ? EnumTypeString<TEnum> : never)
| (TEnum extends number ? EnumTypeNumber<TEnum> : never)
type EnumOf<TEnumType> = TEnumType extends EnumType<infer U>
? U
: never
Usage
EnumType:
function forEachEnum<TEnum extends string | number>(
enumType: EnumType<TEnum>,
callback: (value: TEnum, key: string) => boolean|void,
) {
for (let key in enumType) {
if (Object.prototype.hasOwnProperty.call(enumType, key) && isNaN(Number(key))) {
const value = enumType[key] as any
if (callback(value, key)) {
return
}
}
}
}
EnumOf:
function forEachEnum2<TEnumType>(
enumType: TEnumType,
callback: (value: EnumOf<TEnumType>, key: string) => boolean|void,
) {
for (let key in enumType) {
if (Object.prototype.hasOwnProperty.call(enumType, key) && isNaN(Number(key))) {
const value = enumType[key] as any
if (callback(value, key)) {
return
}
}
}
}
Tests
enum EnumAsString {
Value1 = 'value 1',
Value2 = 'value 2',
}
enum EnumAsNumber {
Value1 = 1,
Value2 = 2,
}
// Error
let sn: EnumType<string> = EnumAsNumber
// Correct
let ns: EnumType<number> = EnumAsString // I have not found a solution for the error here
let nn: EnumType<number> = EnumAsNumber
let Nn: EnumType<EnumAsNumber> = EnumAsNumber
let ss: EnumType<string> = EnumAsString
let Ss: EnumType<EnumAsString> = EnumAsString
forEachEnum(EnumAsString, value => {
let e: EnumAsString = value
let s: string = value
let n: number = value // Error
})
forEachEnum(EnumAsNumber, value => {
let e: EnumAsNumber = value
let s: string = value // Error
let n: number = value
})
forEachEnum2(EnumAsString, value => {
let e: EnumAsString = value
let s: string = value
let n: number = value // Error
})
forEachEnum2(EnumAsNumber, value => {
let e: EnumAsNumber = value
let s: string = value // Error
let n: number = value
})
May be this trick will fit:
enum AbstractEnum { // put somewhere in hidden scope
}
private getRandomElementOfEnum(e: typeof AbstractEnum) {
...
}
#selinathat's solution is great only if you have few types. but what if we have more ? for example :
doSomething(a: string, b: 'this'|'can'|'work'|'test1'|'test2'|'test3'): void {
//do something
}
its pretty ugly hah !?
i prefer to use keyof :
interface Items {
'this',
'can',
'work',
'test1',
'test2',
'test3',
}
doSomething(a: string, b: keyof Items): void {
//do something
}
Here is an example that allows passing an enum with a typechecked value of that enum using a generic. It's really a response to a slightly different question here that was marked as a duplicate: Typescript how to pass enum as Parameter
enum Color {
blue,
};
enum Car {
cadillac,
};
enum Shape {
square,
}
type SupportedEnums = typeof Color | typeof Car;
type InvertTypeOf<T> = T extends typeof Color ? Color :
T extends typeof Car ? Car : never;
function getText<T extends SupportedEnums>(enumValue: InvertTypeOf<T>, typeEnum: T) string | undefined {
if (typeEnum[enumValue]) {
return `${enumValue}(${typeEnum[enumValue]})`;
}
}
console.log(getText(Car.cadillac, Car)); // 0(cadillac)
console.log(getText(0, Color)); // 0(red)
console.log(getText(4, Color)); // undefined
// #ts-expect-error Color is not Car
console.log(getText(Color.blue, Car));
// #ts-expect-error Car is not a Color
console.log(getText(Car.toyota, Color));
// #ts-expect-error Shape is not in SupportedEnums
console.log(getText(5, Shape));
// #ts-expect-error Shape is not in SupportedEnums
console.log(getText(Shape.square, Shape));
I had the same kind of problem, and i did this
private getOptionsFromEnum(OptionEnum: Record<string, string>): Array<SelectOption> {
return Object.keys(OptionEnum).map((value) => {
return {
name: OptionEnum[value],
value,
} as SelectOption;
});
}
I made a helper type to accept any enum as a paramaeter, then you can handle whatever you need next with Object or by calling an index of the Enum.
type Enum = Record<string | number, string | number>
Now use it to accept any enum as parameter:
function enumValues<T extends Enum>(enum: T, filter?: "string"): string[];
function enumValues<T extends Enum>(enum: T, filter?: "number"): number[];
function enumValues<T extends Enum>(enum: T, filter?: undefined): (string|number)[];
function enumValues<T extends Enum>(enum: T, filter?: "string" | "number") {
return Object.values(enum).filter(x => !filter || typeof x === filter);
}
enum color {
red,
green,
blue
}
console.log(enumValues(color,"string"));
// output ['red','green','blue']
console.log(enumValues(color,"number"));
// output [0,1,2]

Resources