Skip to content

[Question] Why can queryWithVars variables only be strings, not ints? #33

@good-idea

Description

@good-idea

https://github.com/dgraph-io/dgraph-js/blob/64dfa8c630c72bd7e0f95747aa14ae9fc1a71596/tests/txn.spec.ts#L49-L60

I have a query function that looks like this:

const getUsers = async ({ first = 50, after = '0x0' }: PaginationArgs): Promise<PageType | null | Error> => {
	const query = `query getUsers($first: number, $after: string) {
		getUsers(func: eq(type, "user"), first: $first, after: $after)  {
			${userFields}
		}
	}`
	const vars = {
		$first: first,
		$after: after
	}
	const result = await db.newTxn().queryWithVars(query, vars))
	return result
}

One issue is that this variable is simply discarded, then I get the error:
Error: 2 UNKNOWN: : strconv.ParseInt: parsing "": invalid syntax - this would be much more helpful if it said something like "Only strings are allowed as variables"

I can then get this working if I do this:

	const query = `query getUsers($first: string, $after: string) {
	// change 'number' to 'string'    ----^
	//...

	const vars = {
		$first: first.toString(),
		$after: after
	}

But then, it's just going to be parsed back to an Int internally. Why not allow for ints in the first place?

Metadata

Metadata

Assignees

No one assigned

    Labels

    kind/questionSomething requiring a response.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions