Giter Club home page Giter Club logo

Comments (4)

IluhinDotPro avatar IluhinDotPro commented on August 26, 2024 8

build.yaml

targets:
  $default:
    builders:
      json_serializable:
        options:
          include_if_null: false
      graphql_codegen:
        options:
          includeIfNullOnInput: false
          clients:
            - graphql
          scalars:
            DateTime:
              type: DateTime
              fromJsonFunctionName: dateTimeFromJson
              toJsonFunctionName: dateTimeToJson
              import: package:yourapp/scalars.dart
            JSON:
              type: Map<String, dynamic>
            Upload:
              type: MultipartFile
              fromJsonFunctionName: fileFromJson
              toJsonFunctionName: fileToJson
              import: package:yourapp/scalars.dart

lib/scalars.dart

import "package:http/http.dart";
export "package:http/http.dart";

String dateTimeToJson(DateTime data) {
  return data.toIso8601String();
}

DateTime dateTimeFromJson(dynamic data) {
  return DateTime.parse(data);
}

MultipartFile fileFromJson(dynamic data) {
  return data as MultipartFile;
}

dynamic fileToJson(MultipartFile data) {
  return data;
}

in code..

Future<bool> uploadImage(String fileNameId) async {
  final ImagePicker _picker = ImagePicker();
  final XFile? image = await _picker.pickImage(source: ImageSource.gallery);
  if (image == null) return false;
  print(image.path);

  var result = await _client.mutateUploadUserFile(
    OptionsMutationUploadUserFile(
      variables: VariablesMutationUploadUserFile(
        file: MultipartFile.fromBytes(
          "",
          await image.readAsBytes(),
          filename: fileNameId,
          contentType: MediaType.parse(lookupMimeType(image.path)!),
        ),
      ),
    ),
  );
  if (result.hasException) {
    print(result.exception.toString());
    await Sentry.captureException(result.exception);
    return false;
  }
  print(result.parsedData!.toJson());
  return true;
}

from graphql_codegen.

budde377 avatar budde377 commented on August 26, 2024

This is the way

from graphql_codegen.

fionues avatar fionues commented on August 26, 2024

thank you very much for the fast and precise answer! In the end i figured out that the scalar Upload was not defined in the schema and thats why it could not work :)

from graphql_codegen.

IluhinDotPro avatar IluhinDotPro commented on August 26, 2024

"Upload" is scalar,and it must be declared in scheme
f.e.:

"""The `Upload` scalar type represents a file upload."""
scalar Upload

type Mutation {
  uploadUserFile(file: Upload!): Resource!
  uploadUserMultipleFiles(files: [Upload!]!): [Resource!]!
}
mutation uploadUserFile($file: Upload!){
    uploadUserFile(file: $file){
        filename
        id
        mimetype
        type
        url
    }
}

from graphql_codegen.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.