jjzjj

dart - Flutter/GraphQL - 以自定义类型作为参数的突变

coder 2023-07-22 原文

我是 flutter 和 graphQL 的新手,目前我正在将 mutations 集成到我的应用程序中。所以,我让服务器端使用模式中定义的一些自定义类型,但我不知道如何在 flutter 端指定它们。让我们看一些代码:

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

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

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

但是在客户端我不知道如何在突变中指定 DiaryInsideCommunity。 我试过这样的事情:

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

并按如下方式传递我的 runMutation:

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

有什么想法吗?在谷歌上找不到任何东西。

最佳答案

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

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 中实现了您想要的类型,并创建了一个模型变异函数来向您展示如何调用它。

在 dart 中做类型没有更简单的方法。

来自这个库的创建者的欢呼,

我们

关于dart - Flutter/GraphQL - 以自定义类型作为参数的突变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53767176/

有关dart - Flutter/GraphQL - 以自定义类型作为参数的突变的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  3. ruby-on-rails - 如何在 ruby​​ 中使用两个参数异步运行 exe? - 2

    exe应该在我打开页面时运行。异步进程需要运行。有什么方法可以在ruby​​中使用两个参数异步运行exe吗?我已经尝试过ruby​​命令-system()、exec()但它正在等待过程完成。我需要用参数启动exe,无需等待进程完成是否有任何ruby​​gems会支持我的问题? 最佳答案 您可以使用Process.spawn和Process.wait2:pid=Process.spawn'your.exe','--option'#Later...pid,status=Process.wait2pid您的程序将作为解释器的子进程执行。除

  4. ruby - RSpec - 使用测试替身作为 block 参数 - 2

    我有一些Ruby代码,如下所示:Something.createdo|x|x.foo=barend我想编写一个测试,它使用double代替block参数x,这样我就可以调用:x_double.should_receive(:foo).with("whatever").这可能吗? 最佳答案 specify'something'dox=doublex.should_receive(:foo=).with("whatever")Something.should_receive(:create).and_yield(x)#callthere

  5. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  6. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  7. ruby - 如何在 Ruby 中拆分参数字符串 Bash 样式? - 2

    我正在为一个项目制作一个简单的shell,我希望像在Bash中一样解析参数字符串。foobar"helloworld"fooz应该变成:["foo","bar","helloworld","fooz"]等等。到目前为止,我一直在使用CSV::parse_line,将列分隔符设置为""和.compact输出。问题是我现在必须选择是要支持单引号还是双引号。CSV不支持超过一个分隔符。Python有一个名为shlex的模块:>>>shlex.split("Test'helloworld'foo")['Test','helloworld','foo']>>>shlex.split('Test"

  8. ruby - 检查方法参数的类型 - 2

    我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)

  9. ruby-on-rails - 在默认方法参数中使用 .reverse_merge 或 .merge - 2

    两者都可以defsetup(options={})options.reverse_merge:size=>25,:velocity=>10end和defsetup(options={}){:size=>25,:velocity=>10}.merge(options)end在方法的参数中分配默认值。问题是:哪个更好?您更愿意使用哪一个?在性能、代码可读性或其他方面有什么不同吗?编辑:我无意中添加了bang(!)...并不是要询问nobang方法与bang方法之间的区别 最佳答案 我倾向于使用reverse_merge方法:option

  10. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

随机推荐