Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Example of using typedItems with subentities #76

Open
alexdess opened this issue Dec 29, 2023 · 0 comments
Open

Example of using typedItems with subentities #76

alexdess opened this issue Dec 29, 2023 · 0 comments

Comments

@alexdess
Copy link

Is it possible to use typedItems with a one-to-many relationship?
For example, a project entity with the relationship "has a" expert

@freezed
class ProjectDto with _$ProjectDto implements IIdentifable {
  const ProjectDto._(); // Added constructor fro my own methods
  const factory ProjectDto({
    required int id,
    required String name,
    required ExpertDto expert,
  }) = _ProjectDto;

  factory ProjectDto.fromJson(Map<String, dynamic> json) =>
      _$ProjectDtoFromJson(json);
}

Currently the code below doesn't work because expert is an "int" and not my expert fields.

class ProjectRepository extends BaseRepository {

  static const String collection = "project";

  Future<List<ProjectDto>> getProjectsAsync() async {
    final result =
        await executeQueryConverterAsync(collection, ProjectDtoConverter());
    return result;
  }

}

class ProjectDtoConverter extends ItemsConverter<ProjectDto> {
  @override
  ProjectDto fromJson(Map<String, Object?> data) {
    return ProjectDto.fromJson(data);
  }

  @override
  Map<String, Object?> toJson(ProjectDto data) {
    return data.toJson();
  }
}
  @override
  Future<List<E>> executeQueryConverterAsync<E>(
      String collection, ItemsConverter<E> converter) async {
    try {
      var result0 = await DirectusCoreSingleton.instance
          .typedItems(collection, converter: converter)
          .readMany();

      return result0.data;
    } catch (e) {
      debugPrint(e.toString());
      return [];
    }
  }

Is there a way to achieve my goals or is it not supported? If not, I'll have to make two requests and link them by hand, or attack GraphQL directly.

Thank you in advance for your advice and for this library.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant