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

时间:2018-12-13 17:26:44

标签: dart flutter graphql

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

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(),
            }
          });

有什么主意吗?在Google上找不到任何内容。

2 个答案:

答案 0 :(得分:1)

喜欢看到围绕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中进行输入。

此库创建者的欢呼声

我们

答案 1 :(得分:1)

假设您正在使用graphql_flutter, 您可以在变量中指定它 一个例子是说您对突变有这个定义

 type Mutation {
 createMeterReading(
  createMeterReadingInput: CreateMeterReadingInput
 ): MeterReading }

您的输入类型定义

     input CreateMeterReadingInput {
           reading: Float
           companyId: Int
           companyStaffId: Int
           customerId: Int
           readingDataType: String
           latitude: Float
           longitude: Float
           masterMeterId: Int
           analogMeterId: Int
           type: String
           editedStatus: String
           base64Image: String
           readingTime: String
     }

扑朔迷离中

 final response = await client.value.mutate(MutationOptions(
  variables: {
    'createMeterReadingInput': {
      'reading': double.parse(_meterReadingController.text),
      'companyStaffId': _decodedToken["companyStaffId"],
      'analogMeterId': waterMeter["analogMeterId"],
      'type': "actual",
      'base64Image': getBase64(),
      'readingTime': timeNow,
      'latitude': widget?.currentPosition?.latitude,
      'longitude': widget?.currentPosition?.longitude
    }
  },
  documentNode: gql(r"""
mutation createMeterReading($createMeterReadingInput:CreateMeterReadingInput  ) {
  createMeterReading(createMeterReadingInput: $createMeterReadingInput) {
 meterReadingId
reading
companyId
companyStaffId
imageUrl
editedStatus
companyStaff{
  firstName
}
}
}


"""),
));