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

Qn: possible to get the type of a sub object in query #80

Open
anisjonischkeit opened this issue Apr 23, 2019 · 5 comments
Open

Qn: possible to get the type of a sub object in query #80

anisjonischkeit opened this issue Apr 23, 2019 · 5 comments

Comments

@anisjonischkeit
Copy link

Say I have the following query:

module GetUser = [%graphql
  {|
    {
        users @bsRecord {
          first_name
          last_name
          email
          company {
            name 
          }
        }
      }
   |}
];

Is it possible to get the type of company?

@anisjonischkeit
Copy link
Author

anisjonischkeit commented Apr 23, 2019

I guess I'm probably looking for a directive (like @genType) on company to tell the ppx to generate the type.

@baransu
Copy link
Contributor

baransu commented Apr 23, 2019

Right now the whole response is just one big object. There is no way to get the type of the part of the response, unfortunately.

@anisjonischkeit
Copy link
Author

ahh lameeee! Well I've been curious about what writing ppxs is like anyway so I might give adding something a go over the next couple of weeks 😄

@maarekj
Copy link

maarekj commented Aug 9, 2019

It's possible:

module Meta = {
  module type mt_ret = {type t;};
  type typed_ret('a) = (module mt_ret with type t = 'a);
  let ret_type = (type a, _: _ => a): typed_ret(a) => {
    module MT_Ret = {
      type t = a;
    };
    (module MT_Ret);
  };
};

module GetUser = [%graphql
  {|
    {
        users @bsRecord {
          first_name
          last_name
          email
          company {
            name 
          }
        }
      }
   |}
];

/** this code will never be executed. It is used only by the compiler to retrieve the type of return */
module MT_Ret = (val Meta.ret_type((json) => GetUser.parse(json)##users[0]##company));
type company = MT_Ret.t;

@anisjonischkeit
Copy link
Author

Very clever! This looks useful in lots of other places too.

ylecornec pushed a commit to o1-labs/graphql_ppx that referenced this issue Jun 3, 2022
* adds decoder support to fragments
* deletes newline
* fixes builds
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