Skip to content

Deriving query code from type #66

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

Open
CGenie opened this issue Oct 26, 2021 · 7 comments
Open

Deriving query code from type #66

CGenie opened this issue Oct 26, 2021 · 7 comments

Comments

@CGenie
Copy link

CGenie commented Oct 26, 2021

Hello,

Suppose I have a type

newtype User = 
  { id :: Int
  , name  :: String
}

Is it possible to automatically create a "full" query for it? I.e I don't want to type

type UserGQL =
  { id :: Int
  , name :: String
  }

and then create a schema out of it, and then remap the UserGQL from the response but just directly define User in schema and have it automatically generate all fields for the query?

Just a note: my model is more complex, with complex types inside of it. On the Haskell side I'm able to derive the deeply nested models structure via the Morpheus library without hand-writing anything.

@roryc89
Copy link
Member

roryc89 commented Oct 27, 2021

@CGenie I can see the use of this but unfortunately this library does not support this yet.

This could probably be achieved outside of this library by defining your own type class that would deep map a record proxy (in this case User) and return a query record for it.

i.e.

class GetQueryFields r query | r -> query where 
   getQueryFields :: Proxy (Record r) -> query

The the instances could be defined using https://github.com/natefaubion/purescript-heterogeneous or some sort of generics.

The only part I'm not sure of is how to handle arguments on fields. Do you know how Morpheus handles this?

Is this something that you may work on? If so let please make a PR and I'll add it. If not, I'll probably be able to work on it soon.

I'll have a look at adding this today

roryc89 added a commit that referenced this issue Oct 27, 2021
@roryc89
Copy link
Member

roryc89 commented Oct 27, 2021

@CGenie I've added some functions that generate queries from types at https://github.com/OxfordAbstracts/purescript-graphql-client/pull/68/files . Is this the sort of thing you had in mind?

@CGenie
Copy link
Author

CGenie commented Oct 28, 2021

For Morpheus you basically need to derive the GQLType class
https://hackage.haskell.org/package/morpheus-graphql-0.17.0/docs/Data-Morpheus-Types.html
and it works (i.e. the server-side is able to generate all the code to handle queries, that includes listing all of model's fields in the playground).

Thanks @roryc89 for that solution, looks interesting, I'll check out in more detail and will let you know.

@CGenie
Copy link
Author

CGenie commented Oct 28, 2021

Is it possible to do a similar thing for newtype? (I guess sum types would be more difficult, does GraphQL support querying sum types at all?)

Then my question is https://github.com/OxfordAbstracts/purescript-graphql-client/pull/68/files#diff-404493334c4108c2411bd1767059be2e94295df5742d14d58116667dc5e80dd7R16-R19 really necessary, seems like duplication of the User type defined above.

@roryc89
Copy link
Member

roryc89 commented Oct 29, 2021

is https://github.com/OxfordAbstracts/purescript-graphql-client/pull/68/files#diff-404493334c4108c2411bd1767059be2e94295df5742d14d58116667dc5e80dd7R16-R19 really necessary, seems like duplication of the User type defined above.

No that's not necessary. It was just a type level test so I could check that getFieldsStandard returns the corrects query type. Going forward I would imagine adding a function that derived the query and ran it so that from the library user's point of view it could work similarly to morpheus, if you wanted.

For custom types, you could either derive the type class instance using derive newtype instance or create your instance if you wanted you a custom query with arguments, directives, aliases or anything else.

@roryc89
Copy link
Member

roryc89 commented Oct 29, 2021

I think that keeping the ability to run the queries without a type is also useful as sometimes I want to query a graphql api without having to write out the return type but still have it inferred

@roryc89
Copy link
Member

roryc89 commented Oct 29, 2021

(I guess sum types would be more difficult, does GraphQL support querying sum types at all?)

At the moment we generate the purescript sum type from graphql enums during codegen. You can see an example of this at https://github.com/OxfordAbstracts/purescript-graphql-client/blob/main/examples/3-enums/src/Main.purs

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

2 participants