Flutter / GraphQL-以自定义类型作为参数的变异 [英] Flutter / GraphQL - Mutation with custom type as parameter

查看:203
本文介绍了Flutter / GraphQL-以自定义类型作为参数的变异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是flutter和graphQL的新手,目前正在将变异集成到我的应用程序中。因此,我在服务器端使用了在架构中定义的一些自定义类型,但是我不知道如何在浮动端进行指定。让我们看一些代码:

I'm new to flutter and graphQL and currently I'm integrating mutations into my app. So, I have the server side using some custom types defined in the schema, but I don't know how to specify them on the flutter side. Let's see some code:

input DiaryGroupPermission {
  groupId: Int!
  permission: Int!
}

input DiaryInsideCommunity {
  communityId: Int!
  permissions: [DiaryGroupPermission]!
}

createDiary(community: DiaryInsideCommunity, description: String, title: String!): Diary

但是在客户端上,我不知道如何在突变内指定DiaryInsideCommunity。
我已经尝试过这样的事情:

But on the client I don't know how to specify the DiaryInsideCommunity inside the mutation. I've tried something like this:

String createDiary = """
  mutation CreateDiary(\$title: String!, \$description: String!, \$community: DiaryInsideCommunity) {
    createDiary(
    title: \$title,
    description: \$description,
    community: \$community
  ) {
    id
  }
)}""".replaceAll('\n', ' ');

并按照以下方式传递我的runMutation:

And passing my runMutation as follows:

runMutation({
            "title": _generalPage.title(),
            "description": _generalPage.description(),
            "community": {
              "communityId": 1,
              "permissions": _permissionPage.selectedGroups().map((group) {
                return {
                  "groupId": group.id,
                  "permission": 1,
                };
              }).toList(),
            }
          });

有什么想法吗?

推荐答案

喜欢看到围绕graphql_flutter库创建的社区。

Love to see the community that is created around the graphql_flutter library.

class DiaryGroupPermission {
  int groupId;
  int permission;

  DiaryGroupPermission.fromJson(Map json)
      : groupId = json['groupId'],
        permission = json['permission'];
}

class DiaryInsideCommunity {
  int communityId;
  List<DiaryGroupPermission> permissions;

  DiaryInsideCommunity.fromJson(Map json)
      : communityId = json['communityId'],
        permissions = json['permissions']
            .map<DiaryGroupPermission>((Map permisionJson) =>
                DiaryGroupPermission.fromJson(permisionJson))
            .toList();
}

class Diary {
  String body;

  Diary(dynamic value) : body = value.toString();
}

typedef Diary createDiaryFunction(
    DiaryInsideCommunity community, String description, String title);

DiaryInsideCommunity community = DiaryInsideCommunity.fromJson({
  'communityId': 1,
  'permissions': [
    {'groupId': 1, 'permission': 1}
  ]
});

Diary mutation(DiaryInsideCommunity community,
        {String description, @required String title}) =>
    Diary(community.permissions[0].groupId);

Diary mutationResult = mutation(community, description: "a", title: "b");

我在dart中实现了您想要的类型,并创建了一个样机突变函数来向您展示如何

I implemented the types that you wanted to in dart and created a mockup mutation function to show you how to call it.

没有简单的方法可以在dart中进行输入。

There is no easier way to do types in dart.

此创建者的欢呼声库,

我们

这篇关于Flutter / GraphQL-以自定义类型作为参数的变异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆