Skip to content

Recursive Query: How to parse data? #652

Description

@komakur

Say I am having a data like:

fragment DataRecursive on DataModel {
    id
    parentId
    children {
        id
        parentId
        children {
            id
            parentId
// add more nesting if needed
        }
    }
}
query FetchData {
    data {
        dataGroup1 {
           ...DataRecursive
        }
        dataGroup2 {
            ...DataRecursive
        }
    }
}

Since the dart class is going to look something like:

class Data {
  const Data({
    required this.dataGroup1,
    required this.dataGroup2,
  });

  factory Data.fromGraphQL(GFetchData data) {
    return Data(
      dataGroup1:
          data.data.dataGroup1
              ?.map((c) => DataNode.fromJson(c.toJson()))
              .toList() ??
          [],
      dataGroup2:
          data.data.dataGroup2
              ?.map((r) => DataNode.fromJson(r.toJson()))
              .toList() ??
          [],
    );
  }

  final List<DataNode> dataGroup1;
  final List<DataNode> dataGroup2;
}

The question here is how to omit using fromJson(r.toJson()) to properly parse the data?

The issue is that there are different classes generated for each dataGroup

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions