Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generates a lot of (unnecessary) parse code for non-trivial queries (slowing down bsb) #27

Closed
jfrolich opened this issue Oct 16, 2019 · 10 comments

Comments

@jfrolich
Copy link
Collaborator

The parse function has a lot of checks on the data, and it looks like it's expanding into hundreds or even thousands of lines of code for non-trivial queries making bsb very slow. In principle the parse function could (almost) be compiled to identity because we always should get back the data in the same way (even if our resolvers change).

I think this is related to mhallin/graphql_ppx#85, but not sure.

Example it generates this code for the following query.

module HomeScreenQuery = [%graphql
  {|
  query HomeScreen($after: String) {
    dailyContent {
      item {
        id
        title
        description
        duration
      }
    }
    goals {
      id
      scheduledAt
      item {
        id
        title
        description
        duration
      }
    }
    feed(after: $after) {
      edges {
        cursor
        node {
          ... on GoalFeedItem {
            id
            completedAt
            rating
            participants {
              id
              name
            }
            item {
              id
              title
              description
              duration
            }
          }
          ... on ActivityFeedItem {
            id
            duration
            completedAt
            rating
            participants {
              id
              name
            }
            activity {
              id
              title
              description
              duration
            }
          }
        }
      }
    }
  }
|}
];

Code:

var ppx_printed_query = "query HomeScreen($after: String)  {\ndailyContent  {\nitem  {\nid  \ntitle  \ndescription  \nduration  \n}\n\n}\n\ngoals  {\nid  \nscheduledAt  \nitem  {\nid  \ntitle  \ndescription  \nduration  \n}\n\n}\n\nfeed(after: $after)  {\nedges  {\ncursor  \nnode  {\n__typename\n...on GoalFeedItem   {\nid  \ncompletedAt  \nrating  \nparticipants  {\nid  \nname  \n}\n\nitem  {\nid  \ntitle  \ndescription  \nduration  \n}\n\n}\n\n...on ActivityFeedItem   {\nid  \nduration  \ncompletedAt  \nrating  \nparticipants  {\nid  \nname  \n}\n\nactivity  {\nid  \ntitle  \ndescription  \nduration  \n}\n\n}\n\n}\n\n}\n\n}\n\n}\n";

function parse(value) {
  var match = Js_json.decodeObject(value);
  if (match !== undefined) {
    var value$1 = Caml_option.valFromOption(match);
    var match$1 = Js_dict.get(value$1, "dailyContent");
    var tmp;
    if (match$1 !== undefined) {
      var value$2 = Caml_option.valFromOption(match$1);
      var match$2 = Js_json.decodeNull(value$2);
      if (match$2 !== undefined) {
        tmp = undefined;
      } else {
        var match$3 = Js_json.decodeObject(value$2);
        var tmp$1;
        if (match$3 !== undefined) {
          var match$4 = Js_dict.get(Caml_option.valFromOption(match$3), "item");
          var tmp$2;
          if (match$4 !== undefined) {
            var value$3 = Caml_option.valFromOption(match$4);
            var match$5 = Js_json.decodeNull(value$3);
            if (match$5 !== undefined) {
              tmp$2 = undefined;
            } else {
              var match$6 = Js_json.decodeObject(value$3);
              var tmp$3;
              if (match$6 !== undefined) {
                var value$4 = Caml_option.valFromOption(match$6);
                var match$7 = Js_dict.get(value$4, "id");
                var tmp$4;
                if (match$7 !== undefined) {
                  var value$5 = Caml_option.valFromOption(match$7);
                  var match$8 = Js_json.decodeString(value$5);
                  tmp$4 = match$8 !== undefined ? match$8 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$5));
                } else {
                  tmp$4 = Js_exn.raiseError("graphql_ppx: Field id on type Item is missing");
                }
                var match$9 = Js_dict.get(value$4, "title");
                var tmp$5;
                if (match$9 !== undefined) {
                  var value$6 = Caml_option.valFromOption(match$9);
                  var match$10 = Js_json.decodeNull(value$6);
                  if (match$10 !== undefined) {
                    tmp$5 = undefined;
                  } else {
                    var match$11 = Js_json.decodeString(value$6);
                    tmp$5 = match$11 !== undefined ? match$11 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$6));
                  }
                } else {
                  tmp$5 = undefined;
                }
                var match$12 = Js_dict.get(value$4, "description");
                var tmp$6;
                if (match$12 !== undefined) {
                  var value$7 = Caml_option.valFromOption(match$12);
                  var match$13 = Js_json.decodeNull(value$7);
                  if (match$13 !== undefined) {
                    tmp$6 = undefined;
                  } else {
                    var match$14 = Js_json.decodeString(value$7);
                    tmp$6 = match$14 !== undefined ? match$14 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$7));
                  }
                } else {
                  tmp$6 = undefined;
                }
                var match$15 = Js_dict.get(value$4, "duration");
                var tmp$7;
                if (match$15 !== undefined) {
                  var value$8 = Caml_option.valFromOption(match$15);
                  var match$16 = Js_json.decodeNull(value$8);
                  if (match$16 !== undefined) {
                    tmp$7 = undefined;
                  } else {
                    var match$17 = Js_json.decodeNumber(value$8);
                    tmp$7 = match$17 !== undefined ? match$17 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$8));
                  }
                } else {
                  tmp$7 = undefined;
                }
                tmp$3 = {
                  id: tmp$4,
                  title: tmp$5,
                  description: tmp$6,
                  duration: tmp$7
                };
              } else {
                tmp$3 = Js_exn.raiseError("graphql_ppx: Object is not a value");
              }
              tmp$2 = Caml_option.some(tmp$3);
            }
          } else {
            tmp$2 = undefined;
          }
          tmp$1 = {
            item: tmp$2
          };
        } else {
          tmp$1 = Js_exn.raiseError("graphql_ppx: Object is not a value");
        }
        tmp = Caml_option.some(tmp$1);
      }
    } else {
      tmp = undefined;
    }
    var match$18 = Js_dict.get(value$1, "goals");
    var tmp$8;
    if (match$18 !== undefined) {
      var value$9 = Caml_option.valFromOption(match$18);
      var match$19 = Js_json.decodeNull(value$9);
      if (match$19 !== undefined) {
        tmp$8 = undefined;
      } else {
        var match$20 = Js_json.decodeArray(value$9);
        tmp$8 = match$20 !== undefined ? match$20.map((function (value) {
                  var match = Js_json.decodeNull(value);
                  if (match !== undefined) {
                    return ;
                  } else {
                    var match$1 = Js_json.decodeObject(value);
                    var tmp;
                    if (match$1 !== undefined) {
                      var value$1 = Caml_option.valFromOption(match$1);
                      var match$2 = Js_dict.get(value$1, "id");
                      var tmp$1;
                      if (match$2 !== undefined) {
                        var value$2 = Caml_option.valFromOption(match$2);
                        var match$3 = Js_json.decodeString(value$2);
                        tmp$1 = match$3 !== undefined ? match$3 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$2));
                      } else {
                        tmp$1 = Js_exn.raiseError("graphql_ppx: Field id on type Goal is missing");
                      }
                      var match$4 = Js_dict.get(value$1, "scheduledAt");
                      var match$5 = Js_dict.get(value$1, "item");
                      var tmp$2;
                      if (match$5 !== undefined) {
                        var match$6 = Js_json.decodeObject(Caml_option.valFromOption(match$5));
                        if (match$6 !== undefined) {
                          var value$3 = Caml_option.valFromOption(match$6);
                          var match$7 = Js_dict.get(value$3, "id");
                          var tmp$3;
                          if (match$7 !== undefined) {
                            var value$4 = Caml_option.valFromOption(match$7);
                            var match$8 = Js_json.decodeString(value$4);
                            tmp$3 = match$8 !== undefined ? match$8 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$4));
                          } else {
                            tmp$3 = Js_exn.raiseError("graphql_ppx: Field id on type Item is missing");
                          }
                          var match$9 = Js_dict.get(value$3, "title");
                          var tmp$4;
                          if (match$9 !== undefined) {
                            var value$5 = Caml_option.valFromOption(match$9);
                            var match$10 = Js_json.decodeNull(value$5);
                            if (match$10 !== undefined) {
                              tmp$4 = undefined;
                            } else {
                              var match$11 = Js_json.decodeString(value$5);
                              tmp$4 = match$11 !== undefined ? match$11 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$5));
                            }
                          } else {
                            tmp$4 = undefined;
                          }
                          var match$12 = Js_dict.get(value$3, "description");
                          var tmp$5;
                          if (match$12 !== undefined) {
                            var value$6 = Caml_option.valFromOption(match$12);
                            var match$13 = Js_json.decodeNull(value$6);
                            if (match$13 !== undefined) {
                              tmp$5 = undefined;
                            } else {
                              var match$14 = Js_json.decodeString(value$6);
                              tmp$5 = match$14 !== undefined ? match$14 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$6));
                            }
                          } else {
                            tmp$5 = undefined;
                          }
                          var match$15 = Js_dict.get(value$3, "duration");
                          var tmp$6;
                          if (match$15 !== undefined) {
                            var value$7 = Caml_option.valFromOption(match$15);
                            var match$16 = Js_json.decodeNull(value$7);
                            if (match$16 !== undefined) {
                              tmp$6 = undefined;
                            } else {
                              var match$17 = Js_json.decodeNumber(value$7);
                              tmp$6 = match$17 !== undefined ? match$17 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$7));
                            }
                          } else {
                            tmp$6 = undefined;
                          }
                          tmp$2 = {
                            id: tmp$3,
                            title: tmp$4,
                            description: tmp$5,
                            duration: tmp$6
                          };
                        } else {
                          tmp$2 = Js_exn.raiseError("graphql_ppx: Object is not a value");
                        }
                      } else {
                        tmp$2 = Js_exn.raiseError("graphql_ppx: Field item on type Goal is missing");
                      }
                      tmp = {
                        id: tmp$1,
                        scheduledAt: match$4 !== undefined ? Caml_option.valFromOption(match$4) : Js_exn.raiseError("graphql_ppx: Field scheduledAt on type Goal is missing"),
                        item: tmp$2
                      };
                    } else {
                      tmp = Js_exn.raiseError("graphql_ppx: Object is not a value");
                    }
                    return Caml_option.some(tmp);
                  }
                })) : Js_exn.raiseError("graphql_ppx: Expected array, got " + JSON.stringify(value$9));
      }
    } else {
      tmp$8 = undefined;
    }
    var match$21 = Js_dict.get(value$1, "feed");
    var tmp$9;
    if (match$21 !== undefined) {
      var value$10 = Caml_option.valFromOption(match$21);
      var match$22 = Js_json.decodeNull(value$10);
      if (match$22 !== undefined) {
        tmp$9 = undefined;
      } else {
        var match$23 = Js_json.decodeObject(value$10);
        var tmp$10;
        if (match$23 !== undefined) {
          var match$24 = Js_dict.get(Caml_option.valFromOption(match$23), "edges");
          var tmp$11;
          if (match$24 !== undefined) {
            var value$11 = Caml_option.valFromOption(match$24);
            var match$25 = Js_json.decodeNull(value$11);
            if (match$25 !== undefined) {
              tmp$11 = undefined;
            } else {
              var match$26 = Js_json.decodeArray(value$11);
              tmp$11 = match$26 !== undefined ? match$26.map((function (value) {
                        var match = Js_json.decodeNull(value);
                        if (match !== undefined) {
                          return ;
                        } else {
                          var match$1 = Js_json.decodeObject(value);
                          var tmp;
                          if (match$1 !== undefined) {
                            var value$1 = Caml_option.valFromOption(match$1);
                            var match$2 = Js_dict.get(value$1, "cursor");
                            var tmp$1;
                            if (match$2 !== undefined) {
                              var value$2 = Caml_option.valFromOption(match$2);
                              var match$3 = Js_json.decodeNull(value$2);
                              if (match$3 !== undefined) {
                                tmp$1 = undefined;
                              } else {
                                var match$4 = Js_json.decodeString(value$2);
                                tmp$1 = match$4 !== undefined ? match$4 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$2));
                              }
                            } else {
                              tmp$1 = undefined;
                            }
                            var match$5 = Js_dict.get(value$1, "node");
                            var tmp$2;
                            if (match$5 !== undefined) {
                              var value$3 = Caml_option.valFromOption(match$5);
                              var match$6 = Js_json.decodeNull(value$3);
                              if (match$6 !== undefined) {
                                tmp$2 = undefined;
                              } else {
                                var match$7 = Js_json.decodeObject(value$3);
                                var tmp$3;
                                if (match$7 !== undefined) {
                                  var match$8 = Js_dict.get(Caml_option.valFromOption(match$7), "__typename");
                                  if (match$8 !== undefined) {
                                    var match$9 = Js_json.decodeString(Caml_option.valFromOption(match$8));
                                    if (match$9 !== undefined) {
                                      var typename = match$9;
                                      switch (typename) {
                                        case "ActivityFeedItem" :
                                            var match$10 = Js_json.decodeObject(value$3);
                                            var tmp$4;
                                            if (match$10 !== undefined) {
                                              var value$4 = Caml_option.valFromOption(match$10);
                                              var match$11 = Js_dict.get(value$4, "id");
                                              var tmp$5;
                                              if (match$11 !== undefined) {
                                                var value$5 = Caml_option.valFromOption(match$11);
                                                var match$12 = Js_json.decodeString(value$5);
                                                tmp$5 = match$12 !== undefined ? match$12 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$5));
                                              } else {
                                                tmp$5 = Js_exn.raiseError("graphql_ppx: Field id on type ActivityFeedItem is missing");
                                              }
                                              var match$13 = Js_dict.get(value$4, "duration");
                                              var tmp$6;
                                              if (match$13 !== undefined) {
                                                var value$6 = Caml_option.valFromOption(match$13);
                                                var match$14 = Js_json.decodeNull(value$6);
                                                if (match$14 !== undefined) {
                                                  tmp$6 = undefined;
                                                } else {
                                                  var match$15 = Js_json.decodeNumber(value$6);
                                                  tmp$6 = match$15 !== undefined ? match$15 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$6));
                                                }
                                              } else {
                                                tmp$6 = undefined;
                                              }
                                              var match$16 = Js_dict.get(value$4, "completedAt");
                                              var match$17 = Js_dict.get(value$4, "rating");
                                              var tmp$7;
                                              if (match$17 !== undefined) {
                                                var value$7 = Caml_option.valFromOption(match$17);
                                                var match$18 = Js_json.decodeNull(value$7);
                                                if (match$18 !== undefined) {
                                                  tmp$7 = undefined;
                                                } else {
                                                  var match$19 = Js_json.decodeNumber(value$7);
                                                  tmp$7 = match$19 !== undefined ? match$19 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$7));
                                                }
                                              } else {
                                                tmp$7 = undefined;
                                              }
                                              var match$20 = Js_dict.get(value$4, "participants");
                                              var tmp$8;
                                              if (match$20 !== undefined) {
                                                var value$8 = Caml_option.valFromOption(match$20);
                                                var match$21 = Js_json.decodeArray(value$8);
                                                tmp$8 = match$21 !== undefined ? match$21.map((function (value) {
                                                          var match = Js_json.decodeObject(value);
                                                          if (match !== undefined) {
                                                            var value$1 = Caml_option.valFromOption(match);
                                                            var match$1 = Js_dict.get(value$1, "id");
                                                            var tmp;
                                                            if (match$1 !== undefined) {
                                                              var value$2 = Caml_option.valFromOption(match$1);
                                                              var match$2 = Js_json.decodeString(value$2);
                                                              tmp = match$2 !== undefined ? match$2 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$2));
                                                            } else {
                                                              tmp = Js_exn.raiseError("graphql_ppx: Field id on type User is missing");
                                                            }
                                                            var match$3 = Js_dict.get(value$1, "name");
                                                            var tmp$1;
                                                            if (match$3 !== undefined) {
                                                              var value$3 = Caml_option.valFromOption(match$3);
                                                              var match$4 = Js_json.decodeString(value$3);
                                                              tmp$1 = match$4 !== undefined ? match$4 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$3));
                                                            } else {
                                                              tmp$1 = Js_exn.raiseError("graphql_ppx: Field name on type User is missing");
                                                            }
                                                            return {
                                                                    id: tmp,
                                                                    name: tmp$1
                                                                  };
                                                          } else {
                                                            return Js_exn.raiseError("graphql_ppx: Object is not a value");
                                                          }
                                                        })) : Js_exn.raiseError("graphql_ppx: Expected array, got " + JSON.stringify(value$8));
                                              } else {
                                                tmp$8 = Js_exn.raiseError("graphql_ppx: Field participants on type ActivityFeedItem is missing");
                                              }
                                              var match$22 = Js_dict.get(value$4, "activity");
                                              var tmp$9;
                                              if (match$22 !== undefined) {
                                                var match$23 = Js_json.decodeObject(Caml_option.valFromOption(match$22));
                                                if (match$23 !== undefined) {
                                                  var value$9 = Caml_option.valFromOption(match$23);
                                                  var match$24 = Js_dict.get(value$9, "id");
                                                  var tmp$10;
                                                  if (match$24 !== undefined) {
                                                    var value$10 = Caml_option.valFromOption(match$24);
                                                    var match$25 = Js_json.decodeString(value$10);
                                                    tmp$10 = match$25 !== undefined ? match$25 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$10));
                                                  } else {
                                                    tmp$10 = Js_exn.raiseError("graphql_ppx: Field id on type Activity is missing");
                                                  }
                                                  var match$26 = Js_dict.get(value$9, "title");
                                                  var tmp$11;
                                                  if (match$26 !== undefined) {
                                                    var value$11 = Caml_option.valFromOption(match$26);
                                                    var match$27 = Js_json.decodeNull(value$11);
                                                    if (match$27 !== undefined) {
                                                      tmp$11 = undefined;
                                                    } else {
                                                      var match$28 = Js_json.decodeString(value$11);
                                                      tmp$11 = match$28 !== undefined ? match$28 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$11));
                                                    }
                                                  } else {
                                                    tmp$11 = undefined;
                                                  }
                                                  var match$29 = Js_dict.get(value$9, "description");
                                                  var tmp$12;
                                                  if (match$29 !== undefined) {
                                                    var value$12 = Caml_option.valFromOption(match$29);
                                                    var match$30 = Js_json.decodeNull(value$12);
                                                    if (match$30 !== undefined) {
                                                      tmp$12 = undefined;
                                                    } else {
                                                      var match$31 = Js_json.decodeString(value$12);
                                                      tmp$12 = match$31 !== undefined ? match$31 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$12));
                                                    }
                                                  } else {
                                                    tmp$12 = undefined;
                                                  }
                                                  var match$32 = Js_dict.get(value$9, "duration");
                                                  var tmp$13;
                                                  if (match$32 !== undefined) {
                                                    var value$13 = Caml_option.valFromOption(match$32);
                                                    var match$33 = Js_json.decodeNull(value$13);
                                                    if (match$33 !== undefined) {
                                                      tmp$13 = undefined;
                                                    } else {
                                                      var match$34 = Js_json.decodeNumber(value$13);
                                                      tmp$13 = match$34 !== undefined ? match$34 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$13));
                                                    }
                                                  } else {
                                                    tmp$13 = undefined;
                                                  }
                                                  tmp$9 = {
                                                    id: tmp$10,
                                                    title: tmp$11,
                                                    description: tmp$12,
                                                    duration: tmp$13
                                                  };
                                                } else {
                                                  tmp$9 = Js_exn.raiseError("graphql_ppx: Object is not a value");
                                                }
                                              } else {
                                                tmp$9 = Js_exn.raiseError("graphql_ppx: Field activity on type ActivityFeedItem is missing");
                                              }
                                              tmp$4 = {
                                                id: tmp$5,
                                                duration: tmp$6,
                                                completedAt: match$16 !== undefined ? Caml_option.valFromOption(match$16) : Js_exn.raiseError("graphql_ppx: Field completedAt on type ActivityFeedItem is missing"),
                                                rating: tmp$7,
                                                participants: tmp$8,
                                                activity: tmp$9
                                              };
                                            } else {
                                              tmp$4 = Js_exn.raiseError("graphql_ppx: Object is not a value");
                                            }
                                            tmp$3 = /* `ActivityFeedItem */[
                                              886006944,
                                              tmp$4
                                            ];
                                            break;
                                        case "GoalFeedItem" :
                                            var match$35 = Js_json.decodeObject(value$3);
                                            var tmp$14;
                                            if (match$35 !== undefined) {
                                              var value$14 = Caml_option.valFromOption(match$35);
                                              var match$36 = Js_dict.get(value$14, "id");
                                              var tmp$15;
                                              if (match$36 !== undefined) {
                                                var value$15 = Caml_option.valFromOption(match$36);
                                                var match$37 = Js_json.decodeString(value$15);
                                                tmp$15 = match$37 !== undefined ? match$37 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$15));
                                              } else {
                                                tmp$15 = Js_exn.raiseError("graphql_ppx: Field id on type GoalFeedItem is missing");
                                              }
                                              var match$38 = Js_dict.get(value$14, "completedAt");
                                              var match$39 = Js_dict.get(value$14, "rating");
                                              var tmp$16;
                                              if (match$39 !== undefined) {
                                                var value$16 = Caml_option.valFromOption(match$39);
                                                var match$40 = Js_json.decodeNull(value$16);
                                                if (match$40 !== undefined) {
                                                  tmp$16 = undefined;
                                                } else {
                                                  var match$41 = Js_json.decodeNumber(value$16);
                                                  tmp$16 = match$41 !== undefined ? match$41 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$16));
                                                }
                                              } else {
                                                tmp$16 = undefined;
                                              }
                                              var match$42 = Js_dict.get(value$14, "participants");
                                              var tmp$17;
                                              if (match$42 !== undefined) {
                                                var value$17 = Caml_option.valFromOption(match$42);
                                                var match$43 = Js_json.decodeArray(value$17);
                                                tmp$17 = match$43 !== undefined ? match$43.map((function (value) {
                                                          var match = Js_json.decodeObject(value);
                                                          if (match !== undefined) {
                                                            var value$1 = Caml_option.valFromOption(match);
                                                            var match$1 = Js_dict.get(value$1, "id");
                                                            var tmp;
                                                            if (match$1 !== undefined) {
                                                              var value$2 = Caml_option.valFromOption(match$1);
                                                              var match$2 = Js_json.decodeString(value$2);
                                                              tmp = match$2 !== undefined ? match$2 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$2));
                                                            } else {
                                                              tmp = Js_exn.raiseError("graphql_ppx: Field id on type User is missing");
                                                            }
                                                            var match$3 = Js_dict.get(value$1, "name");
                                                            var tmp$1;
                                                            if (match$3 !== undefined) {
                                                              var value$3 = Caml_option.valFromOption(match$3);
                                                              var match$4 = Js_json.decodeString(value$3);
                                                              tmp$1 = match$4 !== undefined ? match$4 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$3));
                                                            } else {
                                                              tmp$1 = Js_exn.raiseError("graphql_ppx: Field name on type User is missing");
                                                            }
                                                            return {
                                                                    id: tmp,
                                                                    name: tmp$1
                                                                  };
                                                          } else {
                                                            return Js_exn.raiseError("graphql_ppx: Object is not a value");
                                                          }
                                                        })) : Js_exn.raiseError("graphql_ppx: Expected array, got " + JSON.stringify(value$17));
                                              } else {
                                                tmp$17 = Js_exn.raiseError("graphql_ppx: Field participants on type GoalFeedItem is missing");
                                              }
                                              var match$44 = Js_dict.get(value$14, "item");
                                              var tmp$18;
                                              if (match$44 !== undefined) {
                                                var match$45 = Js_json.decodeObject(Caml_option.valFromOption(match$44));
                                                if (match$45 !== undefined) {
                                                  var value$18 = Caml_option.valFromOption(match$45);
                                                  var match$46 = Js_dict.get(value$18, "id");
                                                  var tmp$19;
                                                  if (match$46 !== undefined) {
                                                    var value$19 = Caml_option.valFromOption(match$46);
                                                    var match$47 = Js_json.decodeString(value$19);
                                                    tmp$19 = match$47 !== undefined ? match$47 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$19));
                                                  } else {
                                                    tmp$19 = Js_exn.raiseError("graphql_ppx: Field id on type Item is missing");
                                                  }
                                                  var match$48 = Js_dict.get(value$18, "title");
                                                  var tmp$20;
                                                  if (match$48 !== undefined) {
                                                    var value$20 = Caml_option.valFromOption(match$48);
                                                    var match$49 = Js_json.decodeNull(value$20);
                                                    if (match$49 !== undefined) {
                                                      tmp$20 = undefined;
                                                    } else {
                                                      var match$50 = Js_json.decodeString(value$20);
                                                      tmp$20 = match$50 !== undefined ? match$50 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$20));
                                                    }
                                                  } else {
                                                    tmp$20 = undefined;
                                                  }
                                                  var match$51 = Js_dict.get(value$18, "description");
                                                  var tmp$21;
                                                  if (match$51 !== undefined) {
                                                    var value$21 = Caml_option.valFromOption(match$51);
                                                    var match$52 = Js_json.decodeNull(value$21);
                                                    if (match$52 !== undefined) {
                                                      tmp$21 = undefined;
                                                    } else {
                                                      var match$53 = Js_json.decodeString(value$21);
                                                      tmp$21 = match$53 !== undefined ? match$53 : Js_exn.raiseError("graphql_ppx: Expected string, got " + JSON.stringify(value$21));
                                                    }
                                                  } else {
                                                    tmp$21 = undefined;
                                                  }
                                                  var match$54 = Js_dict.get(value$18, "duration");
                                                  var tmp$22;
                                                  if (match$54 !== undefined) {
                                                    var value$22 = Caml_option.valFromOption(match$54);
                                                    var match$55 = Js_json.decodeNull(value$22);
                                                    if (match$55 !== undefined) {
                                                      tmp$22 = undefined;
                                                    } else {
                                                      var match$56 = Js_json.decodeNumber(value$22);
                                                      tmp$22 = match$56 !== undefined ? match$56 | 0 : Js_exn.raiseError("graphql_ppx: Expected int, got " + JSON.stringify(value$22));
                                                    }
                                                  } else {
                                                    tmp$22 = undefined;
                                                  }
                                                  tmp$18 = {
                                                    id: tmp$19,
                                                    title: tmp$20,
                                                    description: tmp$21,
                                                    duration: tmp$22
                                                  };
                                                } else {
                                                  tmp$18 = Js_exn.raiseError("graphql_ppx: Object is not a value");
                                                }
                                              } else {
                                                tmp$18 = Js_exn.raiseError("graphql_ppx: Field item on type GoalFeedItem is missing");
                                              }
                                              tmp$14 = {
                                                id: tmp$15,
                                                completedAt: match$38 !== undefined ? Caml_option.valFromOption(match$38) : Js_exn.raiseError("graphql_ppx: Field completedAt on type GoalFeedItem is missing"),
                                                rating: tmp$16,
                                                participants: tmp$17,
                                                item: tmp$18
                                              };
                                            } else {
                                              tmp$14 = Js_exn.raiseError("graphql_ppx: Object is not a value");
                                            }
                                            tmp$3 = /* `GoalFeedItem */[
                                              688985060,
                                              tmp$14
                                            ];
                                            break;
                                        default:
                                          tmp$3 = Js_exn.raiseError("graphql_ppx: Union FeedItem returned unknown type " + typename);
                                      }
                                    } else {
                                      tmp$3 = Js_exn.raiseError("graphql_ppx: Union FeedItem has a __typename field that is not a string");
                                    }
                                  } else {
                                    tmp$3 = Js_exn.raiseError("graphql_ppx: Union FeedItem is missing the __typename field");
                                  }
                                } else {
                                  tmp$3 = Js_exn.raiseError("graphql_ppx: Expected union FeedItem to be an object, got " + JSON.stringify(value$3));
                                }
                                tmp$2 = tmp$3;
                              }
                            } else {
                              tmp$2 = undefined;
                            }
                            tmp = {
                              cursor: tmp$1,
                              node: tmp$2
                            };
                          } else {
                            tmp = Js_exn.raiseError("graphql_ppx: Object is not a value");
                          }
                          return Caml_option.some(tmp);
                        }
                      })) : Js_exn.raiseError("graphql_ppx: Expected array, got " + JSON.stringify(value$11));
            }
          } else {
            tmp$11 = undefined;
          }
          tmp$10 = {
            edges: tmp$11
          };
        } else {
          tmp$10 = Js_exn.raiseError("graphql_ppx: Object is not a value");
        }
        tmp$9 = Caml_option.some(tmp$10);
      }
    } else {
      tmp$9 = undefined;
    }
    return {
            dailyContent: tmp,
            goals: tmp$8,
            feed: tmp$9
          };
  } else {
    return Js_exn.raiseError("graphql_ppx: Object is not a value");
  }
}
@johnhaley81
Copy link
Collaborator

Yep! This is exactly what I was seeing too and that PR starts to fix this issue. There's a bunch more optimization that can be done as well but just that one gave us a big perf boost.

@baransu
Copy link
Collaborator

baransu commented Oct 19, 2019

@jfrolich @johnhaley81 Should be improved in 0.3.1 release. Thank you 🙂

@baransu baransu closed this as completed Oct 19, 2019
@jfrolich
Copy link
Collaborator Author

Thanks. But this doesn't really solve the issue for me. The generated code is not that much shorter (maybe 10% shorter). Can't we rely on the GraphQL server to give us the correct data structure (according to the spec). This is how all GraphQL clients work and I think it's acceptable to have a runtime error if the returned data is not correct. In this case the parse function can be omitted (identity). Also in terms of bundle size it's not great to have hundreds/thousands lines of generated (unnecessary) code added for each query.

@baransu baransu reopened this Oct 21, 2019
@baransu
Copy link
Collaborator

baransu commented Oct 21, 2019

Ok, while there are places where we can improve, a lot of code generated is due to how BuckleScript works. Using thinks like Js.Option.getExn helps but I’m not sure in how many places we can replace it.

I’ll try to look more into it.

@jfrolich
Copy link
Collaborator Author

jfrolich commented Oct 21, 2019

Yeah I thought so. But isn't there a way to short circuit parse json bucklescript code by an external bucklescript call that yields the known graphql return type shape.
Now that I think of it, is if you don't use apollo there is json to be parsed. It's probably not as easy to get rid of that code. Perhaps necessary to do a custom "json" parse with the expected type of the json to be in line with the query so it doesn't need as many parsing assertions.

@baransu
Copy link
Collaborator

baransu commented Jan 23, 2020

Resolved in #58 and released as 0.6.0 under experimental -lean-parse flag.

@baransu baransu closed this as completed Jan 23, 2020
@johnhaley81
Copy link
Collaborator

@baransu I just updated to 0.6.0 and putting "ppx-flags": [["@baransu/graphql_ppx_re/ppx6", "-lean-parse"]] in my bsconfig.json is giving me the error: ppx6: unknown option '-lean-parse'.

@baransu
Copy link
Collaborator

baransu commented Jan 23, 2020

@johnhaley81 I merged branch but wasn't targeting master 😂 Sorry for that. Will fix in 0.6.1

@baransu
Copy link
Collaborator

baransu commented Jan 23, 2020

@johnhaley81 Available in 0.6.1

@johnhaley81
Copy link
Collaborator

@baransu it works great!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants