graphql-ppx icon indicating copy to clipboard operation
graphql-ppx copied to clipboard

Type errors when defining fragments using @ppxCustom

Open mbirkegaard opened this issue 3 years ago • 4 comments

I've been trying to use @ppxCustom on this fragment

    fragment UserFragment on User {
      id
      firstName
    }

where User has the following definition

type User {
  id: ID!
  firstName: string
}

The recursive version

module rec UserFragment = [%graphql
  {|
    fragment UserFragment on User @ppxCustom(module: "UserFragmentDecoder") {
      id
      firstName
    }
  |}
]
and UserFragmentDecoder: {
  type t = {
    id: string,
    name: string,
  };
  let parse: UserFragment.t => t;
  let serialize: t => UserFragment.t;
} = {
  type t = {
    id: string,
    name: string,
  };
  let parse: UserFragment.t => t =
    v =>
      switch (v) {
      | {id, firstName: Some(name)} => {id, name}
      | {id, firstName: None} => {id, name: "Default Name"}
      };
  let serialize: t => UserFragment.t =
    ({id, name}) => {id, firstName: Some(name)};
};

fails with

  We've found a bug for you!
  (No file name)
  
  This has type:
    UserFragmentDecoder.t
  But somewhere wanted:
    t (defined as UserFragment.t)

I also tried the less compact non-recursive version which fails with the same error (naming is slightly different but that is immaterial).

A very similar approach works with a query currentUser: User

module rec UserQuery = [%graphql
  {|
  query {
    currentUser @ppxCustom(module: "UserQueryDecoder") {
      id
      firstName
    }
  } |}
]
and UserQueryDecoder: {
  type t = {
    id: string,
    name: string,
  };
  let parse: UserQuery.t_currentUser => t;
  let serialize: t => UserQuery.t_currentUser;
} = {
  type t = {
    id: string,
    name: string,
  };
  let parse: UserQuery.t_currentUser => t =
    v =>
      switch (v) {
      | {id, firstName: Some(name)} => {id, name}
      | {id, firstName: None} => {id, name: "Default Name"}
      };
  let serialize: t => UserQuery.t_currentUser =
    ({id, name}) => {id, firstName: Some(name)};
};

mbirkegaard avatar Sep 22 '20 17:09 mbirkegaard

Possibly this is a bug indeed (perhaps particularly with a top level ppxCustom on fragments). It would be great if you can contribute the failing query to the tests, then I'll look into fixing it.

The recursive modules where one of the last features that were added to the 1.0 release. Thanks for stress testing them!

jfrolich avatar Sep 23 '20 03:09 jfrolich

I'd be happy to add the failing query and seeing about fixing it also. Let me get get the build and tests working first (as per my questions in #205)

mbirkegaard avatar Sep 23 '20 09:09 mbirkegaard

@jfrolich Just following up on this since it's blocking our migration from the old ppx. I have a branch with a non-compiling test. (I didn't write a proper test since that's hard to do with a query that doesn't compile, the branch is mainly for illustrative purposes.) Could you please look into fixing it?

Emilios1995 avatar Nov 30 '21 23:11 Emilios1995

Yes will look at this ASAP!

jfrolich avatar Dec 07 '21 05:12 jfrolich