edgedb / edgedb-go

The official Go client library for EdgeDB
https://pkg.go.dev/github.com/edgedb/edgedb-go
Apache License 2.0
167 stars 11 forks source link

Add mechanism for generating public query functions and types #287

Closed fmoor closed 1 day ago

fmoor commented 9 months ago

Currently edgeql-go generates private query functions and return types. The user can export these types but it requires a few lines of code for each query. There should be a way to make the generator generate public instead of private functions and types.

Maybe this should be configurable on a per query basis?

chirino commented 1 month ago

I would default to being public, if a user really wants to keep it private he can place it under an internal directory so that it's public to external packages.

fmoor commented 1 month ago

I would default to being public, if a user really wants to keep it private he can place it under an internal directory so that it's public to external packages.

Public by default seems reasonable, but I think there should still be a way to make them private to a package. In a large enough project it is reasonable to want to restrict some things more than just internal/.

GenarLoya commented 1 week ago

Hello, a little question, @fmoor, triying use the flag -pubfuncs not working, I suppose is not in the current release, can you help me to pass private to public, without modify generated files?

fmoor commented 1 week ago

Hello, a little question, @fmoor, triying use the flag -pubfuncs not working, I suppose is not in the current release, can you help me to pass private to public, without modify generated files?

This is not implemented yet. You can achieve the same thing by putting this in a non generated file in the same package.

var MyQueryFunc = myQueryFunc

I'll try to get this implemented soon.

mdthompson-helium commented 20 hours ago

pubtypes generates a public struct but its fields are all private if the fields in the .esdl are lower case. Is best practice to name those fields so they will be generated so they are public or generate the return type by default with public fields if you use pubtypes?

fmoor commented 20 hours ago

pubtypes generates a public struct but its fields are all private. Was it intentional to keep these private as well?

Yes. In EdgeQL it is possible to have both a Name and a name property in a shape/type. Because of this the default behavior is to preserved field names exactly as you spell them in your query. You can change this by either using the -mixedcaps flag or by using mixed caps names in your query. For example:

select schema::Function {
  Name := .name,
  Language := .language,
  Params := .params {
    Name := .name,
    Default := .default,
  }
}

You could also define your types with mixed caps names so you don't need to rename everything in every query.

fmoor commented 20 hours ago

Is best practice to name those fields so they will be generated so they are public or generate the return type by default with public fields if you use pubtypes?

I don't think there is an established best practice here. For most schemas it is probably easiest to use -mixedcaps.

mdthompson-helium commented 20 hours ago

Yes. In EdgeQL it is possible to have both a Name and a name property in a shape/type. Because of this the default behavior is to preserved field names exactly as you spell them in your query. You can change this by either using the -mixedcaps flag or by using mixed caps names in your query.

Thanks, that helped clarify the behavior