Repository navigation
Go SDK: Pass objects by reference instead of ID - #3598
Conversation
6a1cb46 to
946167c
Compare
|
btw -- @sipsma I was able to use a common mechanism in XDX (used when we return e.g. a This could be adapted so it also works with XDX arguments (e.g. take a |
d1fd478 to
fe16963
Compare
There was a problem hiding this comment.
Currently it doesn't work on optional fields, because they're structures. I don't know yet how to get around that without recursive reflection (blargh)
That's the main thing that stuck out to me. I feel like this is worth fixing before we release it, otherwise there's still a huge gotcha.
I can't think of anything besides recursive reflection either off the top of my head.
We could potentially analyze the schema and look for objects that have an id field pointing to a scalar -- and recording that as scalar->object conversion
Yeah, so I guess this makes "id" a very special field to have. I think I'm fine with that though. The only other thing would be a directive or something that let you manually create the association. But in the future we won't want to expose that to code-first extensions, so at that time we may very well end up making "id" a special field anyways.
All that being said, this is amazing! Such a huge improvement from a relatively straightforward modification. I also love that when you inline queries as args to other queries you can start to actually see the structure of the DAG in your code 😎
I could potentially do this in MarshalGQL, but the downside is we lose parallelism (in this PR, all IDs are looked up at once in parallel whereas with more "dynamic" MarshalGQL it would be on the fly, sequentially). I'll try to find a workaround. Note: Initially I thought this could be a problem for later since there's no function that takes an optional ID today. However, that is going to change with #3119 (
Yeah, we might have to rethink that with extensions. The rule of thumb seemed to make sense: If you have a scalar named |
The other existing one I noticed was |
f22e71d to
55a838d
Compare
There was a problem hiding this comment.
Guessing these will show up in the SDK godoc now?
There was a problem hiding this comment.
Unfortunately, yes :/
Problem stems from these being defined in the root package (e.g. api.gen.go) and then used in internal/querybuilder -- can't use private functions there.
Unless there's a way out?
I've used the (crappy) XX convention to somewhat make it explicit in code completion that they're not to be toyed with.
Perhaps we should use gRPC convention (XXX_Whatever) or make them part of the public API by having more generic name/meaning
There was a problem hiding this comment.
Hm yeah unfortunate but not a dealbreaker, makes sense why it's needed.
The only idea I have is to move it to an embedded type, which I think will keep it from showing up in the godoc (and MAYBE we can even make the type private...?), but it has a dependency on r for r.ID so that won't help. Unless it just has its own ID(ctx) implementation somehow.
edit: Also it might not even hide it from the godoc. Not sure. 🙃
Failing that it might be worth following the gRPC/protobuf convention, just because, but not a blocker.
55a838d to
db2aead
Compare
|
@sipsma @vito @helderco PTAL :)
|
6dcd949 to
7ffebe9
Compare
There was a problem hiding this comment.
Using reflect.Value as a map key makes me slightly nervous since the docs say "To compare two Values, compare the results of the Interface method. Using == on two Values does not compare the underlying values they represent." (though the exact meaning of that statement is ambiguous): https://pkg.go.dev/reflect#Value
My fear would be that since there's an unsafe.Pointer in the Value struct then we could have code that works most of the time but randomly doesn't cause the go runtime moved a value around in memory or weird stuff like that.
This is all completely hypothetical though, easily could be a non-issue. If there's a really easy way to avoid needing to cache this beforehand in this map, then maybe it's worth it. Otherwise maybe just a comment here would be useful in case we hit a weird bug someday and are looking over this code.
There was a problem hiding this comment.
100% -- I feel really weird about this.
The alternative would be to make a key somehow -- but it gets messy, fast (array, structs, etc).
The simplest way though is to just get rid of this and do things sequentially.
query builder is already parallelizing arguments -- this second parallelization is at the argument level (e.g. run a container array in parallel, for instance the one you added on Publish)
Maybe it's overkill though and I can just remove this? WDYT?
There was a problem hiding this comment.
Actually, I'll try something else, should be quick.
There was a problem hiding this comment.
Yeah if it's easy to just remove that extra parallelization for now that seems like the right approach. We can easily re-add it in the future if needed and get a better understanding of the reflect/unsafe stuff.
There was a problem hiding this comment.
Done!
I found a way to keep the parallelism, get rid of the map key AND simplify the code! It was over engineered to begin with.
sipsma
left a comment
There was a problem hiding this comment.
One comment about possible reflect weirdness, but I'm okay if we just leave a code comment there for now. Otherwise LGTM!
7ffebe9 to
e3997b2
Compare
Related to dagger#3558 Signed-off-by: Andrea Luzzardi <[email protected]>
e3997b2 to
8b7d944
Compare
Partial implementation of #3558. Example:
Before:
After:
See our changes to
magefile.goand various tests for other examples.Caveats
idin the API (e.g.id FileID), but they're not IDs in the SDK (e.g.id *File)File(id FileID)) become weird (File(id *File)) -- if you already have the file, what's the point of having a function to look it up? I've kept them asFile(id FileID)DirectoryIDtoDirectory(stripping theIDis not enough, for instance it'sCacheID->CacheVolume), so the conversion map is hardcodedidfield pointing to a scalar -- and recording that as scalar->object conversion