| 30 | } |
| 31 | |
| 32 | func QueryWithClient[R any](c *dagger.Client, t *testctx.T, query string, opts *QueryOptions) (*R, error) { |
| 33 | t.Helper() |
| 34 | ctx := t.Context() |
| 35 | |
| 36 | if opts == nil { |
| 37 | opts = &QueryOptions{} |
| 38 | } |
| 39 | if opts.Variables == nil { |
| 40 | opts.Variables = make(map[string]any) |
| 41 | } |
| 42 | if opts.Secrets == nil { |
| 43 | opts.Secrets = make(map[string]string) |
| 44 | } |
| 45 | for n, v := range opts.Secrets { |
| 46 | s, err := newSecret(ctx, c, n, v) |
| 47 | if err != nil { |
| 48 | return nil, err |
| 49 | } |
| 50 | opts.Variables[n] = s |
| 51 | } |
| 52 | |
| 53 | r := new(R) |
| 54 | err := c.Do(ctx, |
| 55 | &dagger.Request{ |
| 56 | Query: query, |
| 57 | Variables: opts.Variables, |
| 58 | OpName: opts.Operation, |
| 59 | }, |
| 60 | &dagger.Response{Data: r}, |
| 61 | ) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | return r, nil |
| 66 | } |
| 67 | |
| 68 | func newSecret(ctx context.Context, c *dagger.Client, name, value string) (*core.SecretID, error) { |
| 69 | query := `query Secret($name: String!, $value: String!) { |