Repository navigation
feat: improve Go SDK to fetch list of objects - #4683
Conversation
|
FIY: After the first check that validates the concept, I plan to add bunch of documentation in the Go Code and the templates |
|
Hey @TomChv, can you update the description with the approach you're taking, expected usage and result? |
|
Please correct me if I'm wrong, just checking if I understand the approach. You're storing in a private property (to work as a cache) any value in the response that matches a field (e.g. |
Yeah, I would have preferred to directly access the property instead of passing a context and unpacking the error but it's unfortunately not possible because of the current SDK API design :/ However, I only store in cache values retrieved as a list so this will not conflict with a singular value that you may want to executes later. For instance // Retrieve the list
alpine, err := c.
Container().
From("alpine:3.16.2").
WithEnvVariable("FOO", "BAR").
// Call to method in envs are scoped to these variables and will not conflict with other calls.
envs, err := alpine.EnvVariables(ctx)
name, err := envs[0].Name(ctx)
// Can still exec or do anything, the query will be executed properly
alpine.WithExec(...)
alpine.EnvVariable(...) |
6ce1d60 to
2bbdf81
Compare
2bbdf81 to
1bde492
Compare
vito
left a comment
There was a problem hiding this comment.
Nicely done!
I think it's probably for the best that we end up using the same types with the inner fields pre-populated, even though the ctx and error make it a bit verbose. I wanted to suggest introducing sibling types like type PrefetchedFoo { Field1, Field2 string } and returning that for arrays of Foo, but even that isn't very good because it prevents you from writing a generic function that deals with Foos.
I left some suggestions but free to just merge as-is, they're SUPER nitpicky. (I left them anticipating more suggestions but didn't find any.)
There was a problem hiding this comment.
nit:
| data = data.(map[string]interface{})[k] | |
| data = f[k] |
There was a problem hiding this comment.
same nit:
| data = data.([]interface{}) | |
| data = f |
There was a problem hiding this comment.
super nitpick:
| q = q.Select("{{ range $v := $field | GetArrayField }}{{ $v.Name }} {{ end }}") | |
| q = q.Select("{{ range $i, $v := $field | GetArrayField }}{{if $i}} {{end}}{{ $v.Name }}{{ end }}") |
This should end up with "foo bar" instead of "foo bar ".
There was a problem hiding this comment.
Thanks! I was looking for this haha
Fixed in 2001712
|
I like it too. |
Extends Go template capabilities. Store scalar type private field to return it if it's set by a list execution. Add test on list of object. Add Schema setter and getter to fetch global GQL types. Add utility functions to Go template. Improve querybuilder to also parse list of object. Resolves: #3457 Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
Signed-off-by: Tom Chauveau <[email protected]>
1bde492 to
2001712
Compare
Changes
Extends Go template capabilities.
Store scalar type private field to return it if it's set by a list execution.
Add test on list of objects.
Add Schema setter and getter to fetch global GQL
types.
Add utility functions to the Go template.
Improve query builder to also parse a list of objects.
Approach
This feature has a simple concept but is actually really hard to implement.
The purpose is to make possible to fetch list of objects.
For the whole context: See #3457.
That implies multiple constraints:
Our current way of building GQL query is simple, each method called adds a level of depth.
For instance:
container.ID(ctx)will build the query{ query { container { id } } }.But it's different for array: you cannot do
container.EnvVariables(ctx).Name(ctx).For 2 reasons:
EnvVariableis an array so no method can be called on it.As a workaround, the GQL query built on call to array includes all possible scalar fields in it.
For instance:
container.EnvVariables(ctx)build:{ query { container { envVariables { name value } } } }.💡 See last section of this comment for a possible workaround.
EnvVariable.This type is designed to be used as a single value that will directly update the GQL query and execute it.
To fix that, each type stores as privates members its scalar types.
For instance:
And on call to array methods, those private fields will be set so they can be returned instead of executing a GQL query (see usage for more details).
Those private fields are set thanks a temporary data structure with the public field of those values, so it can be
Unmarshalledby the query response.This was the most challenging part, the Go generator (and probably the Typescript one too) had no way to access the whole GQL schema.
I'm not sure if it is a design problem but I created special methods so I can access any part of the GQL schema in resolvers so I can introspect a high level type from another.
For instance
Containercan returnsEnvVariablebutEnvVariableis a whole type too defined at the same level asContainerso I could not access its field without going through the whole schema.I think generators need a complete refactor, as much about Go text templates as the Go code to make that kind of feature naturally possible in the code.
Usage & Result
Test already shows an example of usage and expected result.
I think the DX is acceptable but could be better.
However, it's quite straightforward and the experience is simple:
Limitation
Current changes only support the fetching of Scalar types like
String,Boolean, orInt.As a consequence, it is not possible to fetch data in recursivity.
For instance, you can call
project.Extensions()but each returned project will haveextensionsstoredin the list.
An idea that may improve the DX.
Create a special class or wrapper around arrays to still take benefits of GraphQL by allowing the fetch of only
selected fields, removing the
errorreturns and context usage.I'm not sure about the design for now but we shall think about it.
Resolves: #3457