From 58fb578d4ec14b5e7c4639ae5e2a2c11647ec307 Mon Sep 17 00:00:00 2001 From: Luca Steeb Date: Thu, 19 Sep 2019 19:24:02 +0200 Subject: [PATCH] go client: fix relay connections (#4730) * go client: fix relay connections - use correct fields for edge->node selection - use different Connection.Exec() method to return edges & pageInfo - fix singular/plural naming with objectName * feat(go-client): add Nodes() method --- .../__snapshots__/go-client.test.js.md | 354 +++++++++++------- .../__snapshots__/go-client.test.js.snap | Bin 4902 -> 5223 bytes .../src/codegen/generators/go-client.ts | 94 ++++- 3 files changed, 295 insertions(+), 153 deletions(-) diff --git a/cli/packages/prisma-client-lib/src/codegen/generators/__tests__/__snapshots__/go-client.test.js.md b/cli/packages/prisma-client-lib/src/codegen/generators/__tests__/__snapshots__/go-client.test.js.md index a908719639..04cdb42167 100644 --- a/cli/packages/prisma-client-lib/src/codegen/generators/__tests__/__snapshots__/go-client.test.js.md +++ b/cli/packages/prisma-client-lib/src/codegen/generators/__tests__/__snapshots__/go-client.test.js.md @@ -130,6 +130,25 @@ Generated by [AVA](https://ava.li). First *int32 `json:"first,omitempty"`␊ Last *int32 `json:"last,omitempty"`␊ }␊ + // Nodes return just nodes without cursors. It uses the already fetched edges.␊ + func (s *PostConnection) Nodes() []Post {␊ + var nodes []Post␊ + for _, edge := range s.Edges {␊ + nodes = append(nodes, edge.Node)␊ + }␊ + return nodes␊ + }␊ + ␊ + // Nodes return just nodes without cursors, but as a slice of pointers. It uses the already fetched edges.␊ + func (s *PostConnection) NodesPtr() []*Post {␊ + var nodes []*Post␊ + for _, edge := range s.Edges {␊ + item := edge␊ + nodes = append(nodes, &item.Node)␊ + }␊ + return nodes␊ + }␊ + ␊ func (client *Client) PostsConnection (params *PostsConnectionParams) (*PostConnectionExec) {␊ var wparams *prisma.WhereParams␊ if params != nil {␊ @@ -207,6 +226,25 @@ Generated by [AVA](https://ava.li). First *int32 `json:"first,omitempty"`␊ Last *int32 `json:"last,omitempty"`␊ }␊ + // Nodes return just nodes without cursors. It uses the already fetched edges.␊ + func (s *UserConnection) Nodes() []User {␊ + var nodes []User␊ + for _, edge := range s.Edges {␊ + nodes = append(nodes, edge.Node)␊ + }␊ + return nodes␊ + }␊ + ␊ + // Nodes return just nodes without cursors, but as a slice of pointers. It uses the already fetched edges.␊ + func (s *UserConnection) NodesPtr() []*User {␊ + var nodes []*User␊ + for _, edge := range s.Edges {␊ + item := edge␊ + nodes = append(nodes, &item.Node)␊ + }␊ + return nodes␊ + }␊ + ␊ func (client *Client) UsersConnection (params *UsersConnectionParams) (*UserConnectionExec) {␊ var wparams *prisma.WhereParams␊ if params != nil {␊ @@ -398,10 +436,6 @@ Generated by [AVA](https://ava.li). PostOrderByInputTitleDesc PostOrderByInput = "title_DESC"␊ PostOrderByInputTextAsc PostOrderByInput = "text_ASC"␊ PostOrderByInputTextDesc PostOrderByInput = "text_DESC"␊ - PostOrderByInputCreatedAtAsc PostOrderByInput = "createdAt_ASC"␊ - PostOrderByInputCreatedAtDesc PostOrderByInput = "createdAt_DESC"␊ - PostOrderByInputUpdatedAtAsc PostOrderByInput = "updatedAt_ASC"␊ - PostOrderByInputUpdatedAtDesc PostOrderByInput = "updatedAt_DESC"␊ )␊ ␊ type UserOrderByInput string␊ @@ -414,10 +448,6 @@ Generated by [AVA](https://ava.li). UserOrderByInputPasswordDesc UserOrderByInput = "password_DESC"␊ UserOrderByInputNameAsc UserOrderByInput = "name_ASC"␊ UserOrderByInputNameDesc UserOrderByInput = "name_DESC"␊ - UserOrderByInputCreatedAtAsc UserOrderByInput = "createdAt_ASC"␊ - UserOrderByInputCreatedAtDesc UserOrderByInput = "createdAt_DESC"␊ - UserOrderByInputUpdatedAtAsc UserOrderByInput = "updatedAt_ASC"␊ - UserOrderByInputUpdatedAtDesc UserOrderByInput = "updatedAt_DESC"␊ )␊ ␊ type MutationType string␊ @@ -750,17 +780,19 @@ Generated by [AVA](https://ava.li). ␊ ␊ ␊ - func (instance UserPreviousValuesExec) Exec(ctx context.Context) (*UserPreviousValues, error) {␊ - var v UserPreviousValues␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance UserPreviousValuesExec) Exec(ctx context.Context) (*UserPreviousValues, error) {␊ + var v UserPreviousValues␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance UserPreviousValuesExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -775,6 +807,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var UserPreviousValuesFields = []string{"id", "email", "password", "name"}␊ ␊ type UserPreviousValues struct {␊ ID string `json:"id"`␊ @@ -789,17 +823,19 @@ Generated by [AVA](https://ava.li). ␊ ␊ ␊ - func (instance PageInfoExec) Exec(ctx context.Context) (*PageInfo, error) {␊ - var v PageInfo␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance PageInfoExec) Exec(ctx context.Context) (*PageInfo, error) {␊ + var v PageInfo␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance PageInfoExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -814,6 +850,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var PageInfoFields = []string{"hasNextPage", "hasPreviousPage", "startCursor", "endCursor"}␊ ␊ type PageInfo struct {␊ HasNextPage bool `json:"hasNextPage"`␊ @@ -860,17 +898,19 @@ Generated by [AVA](https://ava.li). return &PostExecArray{ret}␊ }␊ ␊ - func (instance UserExec) Exec(ctx context.Context) (*User, error) {␊ - var v User␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance UserExec) Exec(ctx context.Context) (*User, error) {␊ + var v User␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance UserExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -885,6 +925,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var UserFields = []string{"id", "email", "password", "name"}␊ ␊ type User struct {␊ ID string `json:"id"`␊ @@ -909,17 +951,19 @@ Generated by [AVA](https://ava.li). return &PostExec{ret}␊ }␊ ␊ - func (instance PostEdgeExec) Exec(ctx context.Context) (*PostEdge, error) {␊ - var v PostEdge␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance PostEdgeExec) Exec(ctx context.Context) (*PostEdge, error) {␊ + var v PostEdge␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance PostEdgeExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -934,6 +978,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var PostEdgeFields = []string{"cursor"}␊ ␊ type PostEdge struct {␊ Node Post `json:"node"`␊ @@ -967,17 +1013,19 @@ Generated by [AVA](https://ava.li). return &PostPreviousValuesExec{ret}␊ }␊ ␊ - func (instance PostSubscriptionPayloadExec) Exec(ctx context.Context) (*PostSubscriptionPayload, error) {␊ - var v PostSubscriptionPayload␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance PostSubscriptionPayloadExec) Exec(ctx context.Context) (*PostSubscriptionPayload, error) {␊ + var v PostSubscriptionPayload␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance PostSubscriptionPayloadExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -992,6 +1040,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var PostSubscriptionPayloadFields = []string{"mutation", "updatedFields"}␊ ␊ type PostSubscriptionPayload struct {␊ Mutation MutationType `json:"mutation"`␊ @@ -1023,12 +1073,12 @@ Generated by [AVA](https://ava.li). "edges",␊ []string{"cursor"})␊ ␊ - nodes := edges.Client.GetMany(␊ + nodes := edges.Client.GetOne(␊ edges,␊ nil,␊ - [3]string{"", "", "Post"},␊ + [2]string{"", "Post"},␊ "node",␊ - []string{"id", "createdAt", "updatedAt", "name", "desc"})␊ + PostFields)␊ ␊ return &PostEdgeExecArray{nodes}␊ }␊ @@ -1046,17 +1096,24 @@ Generated by [AVA](https://ava.li). return &v, err␊ }␊ ␊ - func (instance PostConnectionExec) Exec(ctx context.Context) (*PostConnection, error) {␊ - var v PostConnection␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance PostConnectionExec) Exec(ctx context.Context) (*PostConnection, error) {␊ + edges, err := instance.Edges().Exec(ctx)␊ + if err != nil {␊ + return nil, err␊ + }␊ + ␊ + pageInfo, err := instance.PageInfo().Exec(ctx)␊ + if err != nil {␊ + return nil, err␊ + }␊ + ␊ + return &PostConnection{␊ + Edges: edges,␊ + PageInfo: *pageInfo,␊ + }, nil␊ + }␊ + ␊ ␊ func (instance PostConnectionExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -1071,6 +1128,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var PostConnectionFields = []string{}␊ ␊ type PostConnection struct {␊ PageInfo PageInfo `json:"pageInfo"`␊ @@ -1093,17 +1152,19 @@ Generated by [AVA](https://ava.li). return &UserExec{ret}␊ }␊ ␊ - func (instance PostExec) Exec(ctx context.Context) (*Post, error) {␊ - var v Post␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance PostExec) Exec(ctx context.Context) (*Post, error) {␊ + var v Post␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance PostExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -1118,6 +1179,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var PostFields = []string{"id", "isPublished", "title", "text"}␊ ␊ type Post struct {␊ ID string `json:"id"`␊ @@ -1132,17 +1195,19 @@ Generated by [AVA](https://ava.li). ␊ ␊ ␊ - func (instance PostPreviousValuesExec) Exec(ctx context.Context) (*PostPreviousValues, error) {␊ - var v PostPreviousValues␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance PostPreviousValuesExec) Exec(ctx context.Context) (*PostPreviousValues, error) {␊ + var v PostPreviousValues␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance PostPreviousValuesExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -1157,6 +1222,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var PostPreviousValuesFields = []string{"id", "isPublished", "title", "text"}␊ ␊ type PostPreviousValues struct {␊ ID string `json:"id"`␊ @@ -1183,17 +1250,19 @@ Generated by [AVA](https://ava.li). return &UserExec{ret}␊ }␊ ␊ - func (instance UserEdgeExec) Exec(ctx context.Context) (*UserEdge, error) {␊ - var v UserEdge␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance UserEdgeExec) Exec(ctx context.Context) (*UserEdge, error) {␊ + var v UserEdge␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance UserEdgeExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -1208,6 +1277,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var UserEdgeFields = []string{"cursor"}␊ ␊ type UserEdge struct {␊ Node User `json:"node"`␊ @@ -1242,17 +1313,19 @@ Generated by [AVA](https://ava.li). return &UserPreviousValuesExec{ret}␊ }␊ ␊ - func (instance UserSubscriptionPayloadExec) Exec(ctx context.Context) (*UserSubscriptionPayload, error) {␊ - var v UserSubscriptionPayload␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance UserSubscriptionPayloadExec) Exec(ctx context.Context) (*UserSubscriptionPayload, error) {␊ + var v UserSubscriptionPayload␊ + ok, err := instance.exec.Exec(ctx, &v)␊ + if err != nil {␊ + return nil, err␊ + }␊ + if !ok {␊ + return nil, ErrNoResult␊ + }␊ + return &v, nil␊ + }␊ + ␊ ␊ func (instance UserSubscriptionPayloadExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -1267,6 +1340,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var UserSubscriptionPayloadFields = []string{"mutation", "updatedFields"}␊ ␊ type UserSubscriptionPayload struct {␊ Mutation MutationType `json:"mutation"`␊ @@ -1298,12 +1373,12 @@ Generated by [AVA](https://ava.li). "edges",␊ []string{"cursor"})␊ ␊ - nodes := edges.Client.GetMany(␊ + nodes := edges.Client.GetOne(␊ edges,␊ nil,␊ - [3]string{"", "", "User"},␊ + [2]string{"", "User"},␊ "node",␊ - []string{"id", "createdAt", "updatedAt", "name", "desc"})␊ + UserFields)␊ ␊ return &UserEdgeExecArray{nodes}␊ }␊ @@ -1321,17 +1396,24 @@ Generated by [AVA](https://ava.li). return &v, err␊ }␊ ␊ - func (instance UserConnectionExec) Exec(ctx context.Context) (*UserConnection, error) {␊ - var v UserConnection␊ - ok, err := instance.exec.Exec(ctx, &v)␊ - if err != nil {␊ - return nil, err␊ - }␊ - if !ok {␊ - return nil, ErrNoResult␊ - }␊ - return &v, nil␊ - }␊ + ␊ + func (instance UserConnectionExec) Exec(ctx context.Context) (*UserConnection, error) {␊ + edges, err := instance.Edges().Exec(ctx)␊ + if err != nil {␊ + return nil, err␊ + }␊ + ␊ + pageInfo, err := instance.PageInfo().Exec(ctx)␊ + if err != nil {␊ + return nil, err␊ + }␊ + ␊ + return &UserConnection{␊ + Edges: edges,␊ + PageInfo: *pageInfo,␊ + }, nil␊ + }␊ + ␊ ␊ func (instance UserConnectionExec) Exists(ctx context.Context) (bool, error) {␊ return instance.exec.Exists(ctx)␊ @@ -1346,6 +1428,8 @@ Generated by [AVA](https://ava.li). err := instance.exec.ExecArray(ctx, &v)␊ return v, err␊ }␊ + ␊ + var UserConnectionFields = []string{}␊ ␊ type UserConnection struct {␊ PageInfo PageInfo `json:"pageInfo"`␊ diff --git a/cli/packages/prisma-client-lib/src/codegen/generators/__tests__/__snapshots__/go-client.test.js.snap b/cli/packages/prisma-client-lib/src/codegen/generators/__tests__/__snapshots__/go-client.test.js.snap index 411d4c1a56e6051db1ce3653e9fbcc2da9ea894e..8451dfd2b5e47b4797952488f51a8707a9e22106 100644 GIT binary patch literal 5223 zcmV-t6qxHlRzV=+i3q79UZ?!2m6OF zw>zClGI*1WQp6s2I@#5vC~LIY`LsJI@;ZH2cd_PfT9!pwbvyWT2S0L&|9mjc(!BnB zoSlC@D!K;gRWi8D^7OoT_jpt$lgppR-A=F5xlT%SP?kr{kFL=jBRtIOFTcQ~ z8TpSX>3@juSy7BJ-8ue;$;dxqd!4XT-%L{UEU5>VC&|sYNQMXR(g7*PbWo$)&ZjB) zy2Z@0ZPIn_c;Po0nfbhj$luLD{SNU~*xqCRduUT7?lW8Uh{p2?{f2h10WtSB{UhY` zD1LYiR0*5slOE;kK$TJ|v{y{?ni%h^C+PK$Rgv#tgy37<*OGI0G%C{(#$`LUJqj9? zhusj?V_zlt@ubd*ylUtXI>?8U0_#8=2l&=$I>5ChmFBmE$qA|oE@F$z>LYxoLfhNh z9GF}udyGNAOGI>~LKn94etMB$EDH7bX(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MhMSL`R>o6Gz0 z_vUKx8F2Pm`+kH*>E#uRpctZV5dbT)7k{+zU7UY{Sf&I)U;2sdmU4ANZM z7X^b3mQGu)$iKFCt){)b!$Lvn9~KItwVQ>EdmqC>Eep#S7TV%N!@?L4HDs$B77B%s zb=Z;wCD7L)=xj3|q?t~1$Q}mVsJ?yxo=G25F>SpnqIB(Km*8ai)v6ZEc}iIy!1BWb zt{pSC_=v+KTYjy%R$Q#3L@O`UZvu?LZWQ2I_o@*!aZjBrlA|T{xjtHGR*JpHD-oM{hrE}9La!-BY~qS2@d1I!2>p7CV|M=mxY8C7SV zT3c{Rl9mdqSyH#+Buq@BQaJS06v;Jd)XXkSUZg!3?d|M9OBo7V{ei0;1Zm%FrZ?*I z$@GRz-mtIrS3}ekMkK34>Y5oTdgeO@zq{jcnT_cYK}cMr2#}sf%k)BS=ng<_IwO_g zK#iu7E(W{Be2PV|vh!7XL7LYp3M7W|#D4P9LM}VV<5wv3a2!=2_HF2cA#X#vj&yNz z-&25VW@JfmEubVKoQ;H-O}x_JRXbW7u$tPIp#D))_9DP}(_s;THaVsy{aHU5n7Cj5 zq&@fgY^UWWe14B%xcd$*(PQ2c>caVFZlh)0J6A3|&jJJorHebn(lBs}AQ(WBWY1qzvwHTW`R?jC)^u z-Ilij!x0~`=PhCb1_+~N?|t)bz+ju>7Tkd0s#^ID43E;ai@j?X%db{d;BLP_-u`^R zRkOtH7mHWfUF^N^)?BE|Y`fNt@T@!3h?=;k{&jVGFGAWPdf)bYFJch6?7n#^doS!h zeW`md*vHgXwD-c*wcdO2K=)oiRi*Y`xWW6w-g{wwUQxyc_TCFws*iu~g)d;er@a>r zq;9$Q0>bY{y!RqdqaX3!3(!#oV&BMxag%f%>EhuW0KZX*JOBUy diff --git a/cli/packages/prisma-client-lib/src/codegen/generators/go-client.ts b/cli/packages/prisma-client-lib/src/codegen/generators/go-client.ts index 948ac6b66c..da55d966e6 100644 --- a/cli/packages/prisma-client-lib/src/codegen/generators/go-client.ts +++ b/cli/packages/prisma-client-lib/src/codegen/generators/go-client.ts @@ -153,6 +153,8 @@ export class GoGenerator extends Generator { return `` } + const objectName = type.name.replace('Connection', '') + return ` type ${goCase(type.name)}Exec struct { exec *prisma.Exec @@ -276,7 +278,7 @@ export class GoGenerator extends Generator { field.name === 'edges' ) { // edges is a special case where a field `node` has nested fields - const objectName = type.name.replace('Connection', '') + return ( sTyp + ` @@ -288,14 +290,14 @@ export class GoGenerator extends Generator { nil, [3]string{"${objectName}WhereInput", "${objectName}OrderByInput", "${objectName}Edge"}, "edges", - []string{"cursor"}) + []string{${typeFields.join(',')}}) - nodes := edges.Client.GetMany( + nodes := edges.Client.GetOne( edges, nil, - [3]string{"", "", "${objectName}"}, + [2]string{"", "${objectName}"}, "node", - []string{"id", "createdAt", "updatedAt", "name", "desc"}) + ${objectName}Fields) return &${objectName}EdgeExecArray{nodes} }` @@ -321,18 +323,42 @@ export class GoGenerator extends Generator { }) .join('\n')} - func (instance ${goCase( - type.name, - )}Exec) Exec(ctx context.Context) (*${goCase(type.name)}, error) { - var v ${goCase(type.name)} - ok, err := instance.exec.Exec(ctx, &v) - if err != nil { - return nil, err - } - if !ok { - return nil, ErrNoResult - } - return &v, nil + ${ + // Connection.Exec gets a special method, because it's the only time it fetches multiple things + // currently, there is no easy way to fetch multiple things, so we just get Edges() and PageInfo() + // separately, which is not super efficient but sufficient for the v1 client + type.name.endsWith('Connection') + ? ` + func (instance ${goCase(type.name)}Exec) Exec(ctx context.Context) (*${goCase(type.name)}, error) { + edges, err := instance.Edges().Exec(ctx) + if err != nil { + return nil, err + } + + pageInfo, err := instance.PageInfo().Exec(ctx) + if err != nil { + return nil, err + } + + return &${goCase(type.name)}{ + Edges: edges, + PageInfo: *pageInfo, + }, nil + } + ` + : ` + func (instance ${goCase(type.name)}Exec) Exec(ctx context.Context) (*${goCase(type.name)}, error) { + var v ${goCase(type.name)} + ok, err := instance.exec.Exec(ctx, &v) + if err != nil { + return nil, err + } + if !ok { + return nil, ErrNoResult + } + return &v, nil + } + ` } func (instance ${goCase( @@ -355,6 +381,19 @@ export class GoGenerator extends Generator { return v, err } + var ${type.name}Fields = []string{${ + // saves all node fields + // this type is required to make it available in connection queries (connection->edge->node) + Object + .keys(fieldMap) + .filter(key => { + const field = fieldMap[key] + const { isScalar, isEnum } = this.extractFieldLikeType(field as GraphQLField) + return isScalar || isEnum + }) + .map((i) => `"${i}"`) + .join(', ')}} + type ${goCase(type.name)} struct { ${Object.keys(fieldMap) .filter(key => { @@ -625,10 +664,29 @@ export class GoGenerator extends Generator { opGetConnection(field) { const { typeName } = this.extractFieldLikeType(field) const param = this.paramsType(field) - const objectName = goCase(field.name).replace('s' + 'Connection', '') + const objectName = goCase(typeName).replace('Connection', '') return ( param.code + ` + // Nodes return just nodes without cursors. It uses the already fetched edges. + func (s *${goCase(typeName)}) Nodes() []${objectName} { + var nodes []${objectName} + for _, edge := range s.Edges { + nodes = append(nodes, edge.Node) + } + return nodes + } + + // Nodes return just nodes without cursors, but as a slice of pointers. It uses the already fetched edges. + func (s *${goCase(typeName)}) NodesPtr() []*${objectName} { + var nodes []*${objectName} + for _, edge := range s.Edges { + item := edge + nodes = append(nodes, &item.Node) + } + return nodes + } + func (client *Client) ${goCase(field.name)} (params *${ param.type }) (*${goCase(typeName)}Exec) {