Skip to content

🐞 codegen: Handling lists of objects #3457

Description

@helderco

Overview

The Go generated client executes the query when a scalar or list is requested.

{{- else if or $field.TypeRef.IsScalar $field.TypeRef.IsList }}
var response {{ $field.TypeRef | FormatType }}
q = q.Bind(&response)
return response, q.Execute(ctx, r.c)
{{- end }}

Problem

When it's a list of objects you need to select the fields.

I looked the API for a problematic case:

type Container {
  envVariables: [EnvVariable!]!
}

type EnvVariable {
  name: String!
  value: String!
}

In fact when attempting to call it:

diff --git a/sdk/go/dagger/client_test.go b/sdk/go/dagger/client_test.go
index bd5fbefd..74263ae0 100644
--- a/sdk/go/dagger/client_test.go
+++ b/sdk/go/dagger/client_test.go
@@ -101,3 +101,23 @@ func TestContainer(t *testing.T) {
        require.NoError(t, err)
        require.Equal(t, "3.16.2\n", contents)
 }
+
+func TestList(t *testing.T) {
+       t.Parallel()
+
+       ctx := context.Background()
+
+       c, err := Connect(ctx)
+       require.NoError(t, err)
+       defer c.Close()
+
+       alpine := c.
+               Core().
+               Container().
+               From("alpine:3.16.2")
+
+       envs, err := alpine.EnvVariables(ctx)
+
+       require.NoError(t, err)
+       require.Equal(t, "PATH", envs[0].Name)
+}

I get an error:

Error: Received unexpected error:
       input:1: Field "envVariables" of type "[EnvVariable!]!" must have a sub selection.

/cc @aluzzardi @dolanor

### Tasks
- [x] https://github.com/dagger/dagger/pull/4683
- [x] https://github.com/dagger/dagger/pull/5375
- [x] https://github.com/dagger/dagger/pull/5523

Activity

  1. added
    kind/bug
    on Oct 20, 2022
  2. aluzzardi commented on Nov 16, 2022

    @aluzzardi
    Contributor

    @sipsma's suggestion in #3860

    RE: how we can fix this. @aluzzardi mentioned not allowing objects in lists in Discord (at least that was my understanding, correct me if wrong).

    Yeah, basically a workaround so it stops hurting. Of course with extensions the pain will come back at some point.

    If we are willing to trade more complexity for having the ability to return objects in lists maybe we could figure something out around autoselecting all trivial scalars for those cases, though I don't know if we'll have to apply that fix on a case-by-case basis (ugly) or if there's a way of cleanly generalizing it. Vague idea, might be a deadend, but in general it would be nice to support these return types.

    Agreed. This might be the cleanest solution, although I'm not sure either about edge cases.

    Also creates a disparity between Object (need to call Object.Field(ctx) -- it's lazy) and [Object!]! (need to call Object.Field -- it's not lazy). We could have both APIs be the same, but the behavior would be different (lazy vs non-lazy) which can be confusing. Implementation would also need to be different (return "pre-fetched" field vs query the API).

    There's also a half-baked option which is auto-selecting ID to make the query work, keeping leaf resolvers as usual. The problem though is how to "resume the query" (when calling .Field(ctx)). Either we re-run the full query with the extra field (but there's no way in GraphQL to select the Nth element, so we'd be calling field on ALL elements) OR we find a different way to resume the query (top-level selector ... but that's very manual)

  3. added
    area/engine
    and removed on Dec 5, 2022
  4. vito commented on Jan 19, 2023

    @vito
    Contributor

    A similar issue came up when I was implementing Stat (#3764). The GraphQL interface is something like Directory { stat(path: String): FileInfo! } with FileInfo being a type sort of like fs.FileInfo. But right now you actually have to select each individual field like size, err := dir.Stat().Size(ctx).

    It's not the exact same problem, but maybe our solution here could address both.

  5. 19 remaining items

  6. helderco commented on Jun 30, 2023

    @helderco
    ContributorAuthor

    Python fix has been merged. Only Node.js remaining.

  7. gerhard commented on Jun 30, 2023

    @gerhard
    Contributor

    OK!

    We want to get a new release out this week, which leaves us with today.

    Would delaying it by a few more hours make a difference for Node.js?

  8. helderco commented on Jun 30, 2023

    @helderco
    ContributorAuthor

    That depends on @TomChv, but probably not enough time.

  9. gerhard commented on Jun 30, 2023

    @gerhard
    Contributor

    Not a problem, it's OK to leave it for the next release. 👍 @TomChv

  10. helderco commented on Jul 26, 2023

    @helderco
    ContributorAuthor

    As early as @TomChv can do it. 👍

  11. added a commit that references this issue on Aug 1, 2023
    831924f
  12. added a commit that references this issue on Aug 1, 2023
    a22353b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions