dgraph-io / dgraph-io/dgraph

[ENHANCEMENT]: Allow array of strings in "GraphQL" variable in DQL

Open
#9,118 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

kind/enhancement Stale
Dominant language
Go
Stars
21.8k
Forks
1.6k
Avg merge
2d 5h
Merged PRs (30d)
9

Description

Use case and current behavior

Syntax Examples (using default values):

  • query title($name: string = "Bauman") { ... }
  • query title($age: int = "95") { ... }
  • query title($uids: string = "0x1") { ... }
  • query title($uids: string = "[0x1, 0x2, 0x3]") { ... }. The value of the variable is a quoted array.

Note If you want to input a list of uids as a GraphQL variable value, you can have the variable as string type and have the value surrounded by square brackets like ["13", "14"].

This list syntax only works with uids. I don't even know why the example ["13", "14"] is in the docs because those are not hexidecimal uids, and it just adds confusion thinking this feature is already implemented.

I can do:

const query = `
    query q($uids: string){
        items(func: uid($uids)) {
            uid
            srcId
        }
    }
`;

const req = {
  query,
  variables: {
    "$uids": "[0x9a, 0x9b]",
  }
};

But I can't do:

const query = `
    query q($srcIds: string){
        items(func: eq(srcId, $srcIds)) {
            uid
            srcId
        }
    }
`;

const req = {
  query,
  variables: {
    "$srcIds": "[\"25\", \"26\"]",
  }
};

Alternatively, when I want to query for a list of items not a uid, I have to dynamically build a more complex query and variable list.

// imagine this is a dynamic list from an input
const srcIds = [25, 26];
const varList = srcIds.map((_, i) => `$srcId_${i}`);

const query = `
    query q(${varList.map(var => `${var}: string`).join(", ")}){
        items(func: eq(
            srcId,
            ${varList.join(", ")}
          )
        ) {
            uid
            srcId
        }
    }
`;

const req = {
  query,
  variables: Object.fromEntries(varList.map((var, i) => [var, `${srcIds[i]}`]))
};
Enhancement

It would be nicer to do this:

// imagine this is a dynamic list from an input
const srcIds = [25, 26];

const query = `
    query q($srcIds: string){
        items(func: eq(
            srcId,
            $srcIds
          )
        ) {
            uid
            srcId
        }
    }
`;

const req = {
  query,
  variables: {
    "$srcIds": `[${srcIds.map((id) => `"${id}"`)}]`
  }
};
Solution proposal

No response

Links to Discuss, RFC or previous Issues and PRs

https://discuss.dgraph.io/t/how-to-find-nodes-given-an-array-of-values/3316/7?u=amaster507

Links to examples and research

https://dgraph.io/docs/query-language/graphql-variables/

Additional Information

It would be best to support the syntax $srcIds: [string] but for now a quicker fix would just allow an array of strings/ints like the $uids: string accepts.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the GraphQL variables documentation and the linked Dgraph discussion to understand the existing quoted-UID list behavior and the requested string-list syntax. Trace the GraphQL variable handling for DQL functions such as eq, then define and verify behavior for array-valued string and integer variables, including the proposed [string] form.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, graphql
Domain
api, databases
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.