teamwalnut / graphql-ppx

GraphQL language primitives for ReScript/ReasonML written in ReasonML
https://graphql-ppx.com
MIT License
258 stars 53 forks source link

Incompatible type error with 1.0.2-4911e5e.0 #222

Closed dfalling closed 3 years ago

dfalling commented 3 years ago
  This has type:
    Belt.List.t(TripsQuery.TripsQuery_inner.t_trips) (defined as
      list(TripsQuery.TripsQuery_inner.t_trips))
  Somewhere wanted: list(trip)

  The incompatible parts:
    TripsQuery.TripsQuery_inner.t_trips vs
    trip (defined as TripsQuery.t_trips)

This worked in 1.0.2-f9af62e.0, but not in 1.0.1. The full source of my component is:

[%graphql
  {|
    query TripsQuery @ppxConfig(
    templateTagImport: "gql",
    templateTagLocation: "@apollo/client",
    templateTagIsFunction: true
  ){
      trips {
        id
        name
      }
    }
  |}
];

type trip = TripsQuery.t_trips;

module TripItem = {
  [@react.component]
  let make = (~trip: trip) => {
    React.string(trip.name);
  };
};

[@react.component]
let make = () => {
  let queryResult = TripsQuery.use();

  let intl = ReactIntl.useIntl();
  let (trips, setTrips) = React.useState(_ => [||]);
  React.useEffect1(
    _ => {
      switch (queryResult.data) {
      | Some({trips}) => setTrips(_ => trips)
      | _ => ()
      };
      None;
    },
    [|queryResult|],
  );

  switch (queryResult, trips) {
  | ({loading: true}, _) => <Loading />
  | ({error: Some(error)}, _) => <div> {React.string(error.message)} </div>
  | (_, trips) =>
    <>
      {trips
       |> Array.map(trip => <TripItem trip key={trip.id} />)
       |> ReasonReact.array}
    </>
  };
};

And the error's happening on the |> Array.map(... line near the bottom.

jfrolich commented 3 years ago

You say it broke in 1.0.1 and works in 1.0.2-f9af62e.0, but the latter is a newer development version. Does that mean it is something that has been fixed in the newest version?

dfalling commented 3 years ago

Sorry, I explained that poorly. Using that root type t_trips never worked until 1.0.2-x. Before I had to use that internal type of TripsQuery_inner.t_trips. So 1.0.2-4911e5e.0 is a regression for me to the behavior in 1.0.1.

jfrolich commented 3 years ago

@dfalling: The root type should be the type that is exposed. The reason that TripsQuery_inner.t_trips is there is because of the extension of the query module. We constrained the type to much in 1.0.0 so using TripsQuery.t_trips wouldn't work when you used the hook, which was fixed in 1.0.1. Why is this a regression for you?

dfalling commented 3 years ago

@jfrolich In 1.0.2-4911e5e.0 I can no longer use the root exposed type. The type system now complains like it did in 1.0.1 that this type is incompatible with the expected type of TripsQuery_inner.t_trips. Am I misunderstanding these types? I had moved to 1.0.2x in order to use the root type.

jfrolich commented 3 years ago

Are you sure this is a bug in 1.0.1? Because we use this pattern a lot in 1.0.1 and there are no problems.

jfrolich commented 3 years ago

Can you remove the node modules and run npm install again. Sometimes an old version of the ppx is stuck.

dfalling commented 3 years ago

Yes, this happens with a fresh build.

And sorry, I forgot why I bumped versions. This worked in 1.0.1 and in 1.0.2-f9af62e.0. I moved to 1.0.2 for the templateTagIsFunction.

jfrolich commented 3 years ago

The diff (f9af62e...4911e5e) doesn't really add anything new. Can you try 1.0.2-b5c3318.0 because that one fixes an issue with upgrading the ppx (in the postinstall script). Besides 4911e5e is an old version that doesn't include the templateTagIsFunction yet!

dfalling commented 3 years ago

1.0.2-b5c3318.0 works for me! Thanks!