Below is the full sample, the commented out option is the one that works. In both cases I am searching the same username “vespertilian” which is the only demo user I have loaded.
I did notice in the docs that there should be a minimum requirement setup for regex searching.
// function query(usernamePartialString: string): string {
// return `query {
// users(func: regexp(username, /${usernamePartialString}/))
// {
// uid
// name
// emailAddress
// username
// timezone
// bio
// }
// }`;
// }
const query = `query all($a: string) {
users(func: regexp(username, /$a/))
{
uid
name
emailAddress
username
timezone
bio
}
};`
export async function usersUsernameResolver(
_: any,
{username} : any,
context: any,
info: any,
dgraphClient: DgraphClient = newDgraphClient()
) {
const vars = { $a: username};
let result = null;
try {
// const res = await dgraphClient.newTxn().query(query(username));
const res = await dgraphClient.newTxn().queryWithVars(query, vars);
const jsonStr = new Buffer(res.getJson_asU8()).toString();
result = JSON.parse(jsonStr).users;
console.log(result)
} catch(e) {
console.log(e)
}
return result;
}