|
| 1 | +module Prometo = Yawaramin__Prometo; |
| 2 | + |
| 3 | +module AddPersonMutation = [%graphql |
| 4 | + {| |
| 5 | + mutation addPerson($age: Int!, $name: String!) { |
| 6 | + person: createPerson(age: $age, name: $name) { |
| 7 | + id |
| 8 | + age |
| 9 | + name |
| 10 | + } |
| 11 | + } |
| 12 | + |} |
| 13 | +]; |
| 14 | + |
| 15 | +module PeopleQuery = [%graphql |
| 16 | + {| |
| 17 | + query PeopleQuery { |
| 18 | + people: allPersons { |
| 19 | + id |
| 20 | + name |
| 21 | + } |
| 22 | + } |
| 23 | +|} |
| 24 | +]; |
| 25 | + |
| 26 | +module PersonsOlderThanQuery = [%graphql |
| 27 | + {| |
| 28 | + query getPersonsOlderThan($age: Int!) { |
| 29 | + allPersons(filter: { age_gte: $age } ) { |
| 30 | + id |
| 31 | + } |
| 32 | + } |
| 33 | +|} |
| 34 | +]; |
| 35 | + |
| 36 | +let logPeople_good_reasonPromise = _ => |
| 37 | + Client.instance |
| 38 | + ->ApolloClient.query0((module PeopleQuery)) |
| 39 | + ->Promise.Js.fromBsPromise |
| 40 | + ->Promise.Js.toResult |
| 41 | + ->Promise.get( |
| 42 | + fun |
| 43 | + | Ok({data: Some({people})}) => Js.log2("Data: ", people) |
| 44 | + | Ok(_) => Js.log("Error: no people!") |
| 45 | + | Error(error) => Js.log2("Error: ", error), |
| 46 | + ); |
| 47 | + |
| 48 | +let logPeople_good_prometo = _ => |
| 49 | + Client.instance |
| 50 | + ->ApolloClient.query0((module PeopleQuery)) |
| 51 | + ->Prometo.fromPromise |
| 52 | + ->Prometo.handle(~f=result => { |
| 53 | + switch (result) { |
| 54 | + | Ok({data: Some({people})}) => Js.log2("Data: ", people) |
| 55 | + | Ok(_) => Js.log("Error: no people!") |
| 56 | + | Error(error) => Js.log2("Error: ", error) |
| 57 | + }; |
| 58 | + result; |
| 59 | + }) |
| 60 | + ->ignore; |
| 61 | + |
| 62 | +/** |
| 63 | + * The ergonomice of the Js Module Promise methods are such that the compiler cannot |
| 64 | + * infer the type of result which means the record fields are not in scope forcing you |
| 65 | + * to annotate it :( I would highly recommend using one of the promise libraries above |
| 66 | + */ |
| 67 | +let logPeople_bad_jsPromise = _ => |
| 68 | + Client.instance |
| 69 | + ->ApolloClient.query0((module PeopleQuery)) |
| 70 | + ->Js.Promise.then_( |
| 71 | + (result: ApolloClient__ApolloClient.ApolloQueryResult.t(_)) => |
| 72 | + switch (result) { |
| 73 | + | {data: Some({PeopleQuery.people})} => |
| 74 | + Js.Promise.resolve(Js.log2("Data: ", people)) |
| 75 | + | _ => Js.Exn.raiseError("Error: no people!") |
| 76 | + }, |
| 77 | + _, |
| 78 | + ) |
| 79 | + ->Js.Promise.catch( |
| 80 | + eeyore => Js.Promise.resolve(Js.log2("Error: ", eeyore)), |
| 81 | + _, |
| 82 | + ) |
| 83 | + ->ignore; |
| 84 | + |
| 85 | +let addPerson = _ => |
| 86 | + /** |
| 87 | + * If the operation module were not the last argument, we could just use a record here |
| 88 | + * instead of requiring the use of `makeVariables`. :thinking_face: |
| 89 | + */ |
| 90 | + ( |
| 91 | + Client.instance |
| 92 | + ->ApolloClient.mutate( |
| 93 | + ~variables=AddPersonMutation.makeVariables(~age=40, ~name="Ted", ()), |
| 94 | + (module AddPersonMutation), |
| 95 | + ) |
| 96 | + ->Promise.Js.fromBsPromise |
| 97 | + ->Promise.Js.toResult |
| 98 | + ->Promise.get( |
| 99 | + fun |
| 100 | + | Ok(result) => Js.log2("Data: ", result.data) |
| 101 | + | Error(error) => Js.log2("Error: ", error), |
| 102 | + ) |
| 103 | + ); |
| 104 | +arositen; |
| 105 | + |
| 106 | +[@react.component] |
| 107 | +let make = () => { |
| 108 | + <div> |
| 109 | + <p> |
| 110 | + <button onClick=logPeople_good_reasonPromise> |
| 111 | + "Log People (Reason Promise)"->React.string |
| 112 | + </button> |
| 113 | + </p> |
| 114 | + <p> |
| 115 | + <button onClick=logPeople_good_prometo> |
| 116 | + "Log People (Prometo)"->React.string |
| 117 | + </button> |
| 118 | + </p> |
| 119 | + <p> |
| 120 | + <button onClick=logPeople_bad_jsPromise> |
| 121 | + "Log People (Js Module)"->React.string |
| 122 | + </button> |
| 123 | + </p> |
| 124 | + <p> <button onClick=addPerson> "Add Person"->React.string </button> </p> |
| 125 | + </div>; |
| 126 | +}; |
0 commit comments