简体   繁体   English

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

[英]Flutter / GraphQL - Mutation with custom type as parameter

I'm new to flutter and graphQL and currently I'm integrating mutations into my app.我是 flutter 和 graphQL 的新手,目前我正在将突变集成到我的应用程序中。 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

But on the client I don't know how to specify the DiaryInsideCommunity inside the mutation.但是在客户端上,我不知道如何在突变中指定 DiaryInsideCommunity。 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', ' ');

And passing my runMutation as follows:并通过我的 runMutation 如下:

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

Any idea?任何的想法? Can't find anything on google.在谷歌上找不到任何东西。

Love to see the community that is created around the graphql_flutter library.很高兴看到围绕 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");

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.在 dart 中没有更简单的方法来做类型。

Cheers from the creator of this library,来自这个库的创建者的欢呼,

Eus欧盟

Assuming you are using graphql_flutter , You may specify it in variables an example is say you have this definitions for a mutation假设您使用的是graphql_flutter ,您可以在变量中指定它,一个例子是说您有一个突变的定义

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

your input type definition您的输入类型定义

     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
     }

In flutter have在扑有

 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
}
}
}


"""),
));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM