How to send multiline string from graphQL from flutter?

396

I presume you are using flutter_graphql. It is bad practice to generate mutation strings using interpolation. You should use graphql variables for sending data with mutations (And, there is no problem in sending a multi-line string).

Sample:

String createComments(String postId, var text) {
  const createCommentMutation = """
      mutation createComment(\$postId: String, \$comment:String) { 
        createComment(postId: \$postId, 
          data:{
            text: \$comment,
          }
        ){
          _id
        }
      }
  """;

  dynamic _resp = await _graphClient
          .mutate(MutationOptions(
              document: gql(createCommentMutation),
              variables: {
                'postId': postId, //Add your variables here
                'comment':text
              },
          ));

}

The type of \$postId & \$comment should be same as that of in your graphql schema. I have declared them as String on the very first line.

You can find the documentation of the same here

Share:
396
Yasharth Dubey
Author by

Yasharth Dubey

Experienced Software Engineer and a Competetive Coder with a demonstrated history of working in the e-learning industry. Skilled in Python (Programming Language), C++, Communication, Dart, and Discrete Mathematics. Energetic engineering professional with a Bachelor of Technology - BTech focused in Computer Science from Indian Institute Of Information Technology, Dharwad

Updated on December 28, 2022

Comments

  • Yasharth Dubey
    Yasharth Dubey over 1 year

    I am trying put multiline support in one of the comment section of app and it is not accepting it.

    the input which i put is

    Hi
    Hello
    Hello
    

    and it is showing this error

    this is the error i am getting while putting the above input in the textfield

    And this is the code i am writing for the inputfield

                 ListTile(
                    leading: CircleAvatar(
                      backgroundImage: AssetImage(UIData.pkImage),
                    ),
                    title:  Container(
                      constraints: BoxConstraints(
                        maxHeight: double.infinity,
                        minHeight: 20,
                      ),
                    child: TextField(
                      keyboardType: TextInputType.multiline,
                      minLines: 1,//Normal textInputField will be displayed
                      maxLines: 10,// when user presses enter it will adapt to it
                      decoration: InputDecoration(
                          suffix: IconButton(
                            color: Colors.grey,
                            icon: Icon(Icons.send),
                            onPressed: () {
                              createComment();
                            },
                          ),
                          hintText: 'Leave a Comment....',
                          border: OutlineInputBorder(
                              borderRadius: BorderRadius.circular(20.0),
                              borderSide: BorderSide(color: Colors.teal))),
                      controller: commentController,
                    ),
                    ),
                  ),
    

    The problem is with updating the graphQL query and initializing it with String block

    String createComments(String postId, var text) {
        return """
    mutation{
      createComment(postId: "$postId", 
      data:{
        text: ""$text"",
      }
      ){
        _id
      }
    }
    """
    ;
      }
                  
    
  • Yasharth Dubey
    Yasharth Dubey about 3 years
    it is showing that _resp is not used anywhere and second string cant be assigned to document
  • Yasharth Dubey
    Yasharth Dubey about 3 years
    i think document should be document node
  • Yasharth Dubey
    Yasharth Dubey about 3 years
    and it is saying that future can't be assinged to map
  • HasilT
    HasilT about 3 years
    @YasharthDubey You might be using an older version of flutter_graphql. They have depricated documentNode in favor of document