atulmy/gql-query-builder

Trying to understand nested queries with aliases

Opened this issue · 0 comments

I'm trying to put together a query where the alias is "deep", but am getting runtime errors. TS checks out because it's type is object.

import { query as buildQuery } from "gql-query-builder";

const works = buildQuery(
  {
    operation: "operation",
    variables: {},
    fields: [
      "id",
      {
        operation: "operartion",
        variables: {},
        fields: [
          {
            operation: {
              alias: "alias",
              name: "name",
            },
            // if this is removed (below example) 
            // it throws a runtime error of innerFields.forEach is not a function
            variables: {},
            fields: ["field"],
          },
        ],
      },
      {
       	nested: ["id"],
      },
    ],
  },
);

console.log(works)

try {
  const broken = buildQuery(
  {
    operation: "operation",
    variables: {},
    fields: [
      "id",
      {
        operation: "operartion",
        variables: {},
        fields: [
          {
            operation: {
              alias: "alias",
              name: "name",
            },
            fields: ["field"],
          },
        ],
      },
      {
       	nested: ["id"],
      },
    ],
  },
);
} catch(e){
  console.error("it didn't work", e)
}

Is it documented anywhere that this field needs to be present, even if you don't ave any variables?