├── LICENSE ├── ast.go ├── ast_test.go ├── doc.go ├── exec.go ├── lib.go ├── read.go ├── read_test.go ├── write.go └── write_test.go /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /ast.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | import ( 4 | "fmt" 5 | "strings" 6 | ) 7 | 8 | type operationType uint8 9 | 10 | const ( 11 | opQuery operationType = iota + 1 12 | opMutation 13 | opSubscription 14 | ) 15 | 16 | type fielder interface { 17 | addField(field) 18 | } 19 | 20 | type argumentList []argument 21 | 22 | func (l argumentList) format(b *strings.Builder) { 23 | if len(l) == 0 { 24 | return 25 | } 26 | b.WriteByte('(') 27 | for i, arg := range l { 28 | if i != 0 { 29 | b.WriteString(", ") 30 | } 31 | b.WriteString(arg.name) 32 | b.WriteString(": ") 33 | b.WriteString(arg.value) 34 | } 35 | b.WriteByte(')') 36 | } 37 | 38 | type fieldList []field 39 | 40 | func (l fieldList) format(b *strings.Builder) { 41 | for _, f := range l { 42 | f.format(b) 43 | b.WriteByte('\n') 44 | } 45 | } 46 | 47 | type operation struct { 48 | typ operationType 49 | name string 50 | arguments argumentList 51 | fields fieldList 52 | } 53 | 54 | func (op *operation) addField(f field) { 55 | op.fields = append(op.fields, f) 56 | } 57 | 58 | type argument struct { 59 | name string 60 | value string 61 | } 62 | 63 | func formatOperation(op *operation) string { 64 | // TODO(dh): verify that all names are valid (e.g. don't contain spaces) 65 | 66 | b := &strings.Builder{} 67 | switch op.typ { 68 | case opQuery: 69 | b.WriteString("query") 70 | case opMutation: 71 | b.WriteString("mutation") 72 | case opSubscription: 73 | b.WriteString("subscription") 74 | default: 75 | panic(fmt.Sprintf("invalid operation type %q", op.typ)) 76 | } 77 | 78 | b.WriteByte(' ') 79 | b.WriteString(op.name) 80 | op.arguments.format(b) 81 | b.WriteString(" {\n") 82 | op.fields.format(b) 83 | b.WriteByte('}') 84 | 85 | return b.String() 86 | } 87 | 88 | type field interface { 89 | format(b *strings.Builder) 90 | isField() 91 | } 92 | 93 | type scalarField struct { 94 | name string 95 | arguments argumentList 96 | } 97 | 98 | func (f scalarField) format(b *strings.Builder) { 99 | b.WriteString(f.name) 100 | f.arguments.format(b) 101 | } 102 | 103 | func (scalarField) isField() {} 104 | 105 | type objectField struct { 106 | name string 107 | arguments argumentList 108 | fields fieldList 109 | } 110 | 111 | func (of *objectField) addField(f field) { 112 | of.fields = append(of.fields, f) 113 | } 114 | 115 | func (of objectField) format(b *strings.Builder) { 116 | b.WriteString(of.name) 117 | of.arguments.format(b) 118 | b.WriteString(" {\n") 119 | of.fields.format(b) 120 | b.WriteString("}") 121 | } 122 | 123 | func (objectField) isField() {} 124 | -------------------------------------------------------------------------------- /ast_test.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestFormatOperation(t *testing.T) { 8 | tests := []struct { 9 | op *operation 10 | out string 11 | }{ 12 | { 13 | &operation{ 14 | typ: opQuery, 15 | name: "foo", 16 | }, 17 | "query foo {\n}", 18 | }, 19 | { 20 | &operation{ 21 | typ: opQuery, 22 | name: "foo", 23 | arguments: []argument{{"$bar", "String!"}}, 24 | fields: []field{ 25 | scalarField{name: "id"}, 26 | scalarField{name: "weight", arguments: []argument{{"unit", `"lbs"`}}}, 27 | objectField{name: "parent", fields: []field{scalarField{name: "id"}}}, 28 | }, 29 | }, 30 | "query foo($bar: String!) {\nid\nweight(unit: \"lbs\")\nparent {\nid\n}\n}", 31 | }, 32 | } 33 | 34 | for _, tt := range tests { 35 | out := formatOperation(tt.op) 36 | if out != tt.out { 37 | t.Errorf("got %q expected %q", out, tt.out) 38 | } 39 | } 40 | } 41 | 42 | func BenchmarkFormatOperation(b *testing.B) { 43 | op := &operation{ 44 | typ: opQuery, 45 | name: "foo", 46 | arguments: []argument{{"$bar", "String!"}}, 47 | fields: []field{ 48 | scalarField{name: "id"}, 49 | scalarField{name: "weight", arguments: []argument{{"unit", `"lbs"`}}}, 50 | objectField{name: "parent", fields: []field{scalarField{name: "id"}}}, 51 | }, 52 | } 53 | for i := 0; i < b.N; i++ { 54 | formatOperation(op) 55 | } 56 | } 57 | -------------------------------------------------------------------------------- /doc.go: -------------------------------------------------------------------------------- 1 | // Package prisma provides runtime functions for generated Prisma clients. 2 | // Users should not have to interact with it directly. 3 | package prisma 4 | -------------------------------------------------------------------------------- /exec.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | import ( 4 | "context" 5 | "strconv" 6 | 7 | "github.com/mitchellh/mapstructure" 8 | ) 9 | 10 | func (client *Client) decode(exec *Exec, data map[string]interface{}, v interface{}) (bool, error) { 11 | var genericData interface{} // This can handle both map[string]interface{} and []interface[] 12 | 13 | unpackedData := data 14 | for _, instruction := range exec.Stack { 15 | v := (unpackedData[instruction.Name]) 16 | if v == nil { 17 | return false, nil 18 | } 19 | if isArray(v) { 20 | genericData = v.([]interface{}) 21 | break 22 | } else { 23 | unpackedData = v.(map[string]interface{}) 24 | } 25 | genericData = unpackedData 26 | } 27 | 28 | return true, mapstructure.Decode(genericData, v) 29 | } 30 | 31 | func (exec *Exec) buildQuery() (string, map[string]interface{}) { 32 | var allArgs []graphQLArg 33 | variables := make(map[string]interface{}) 34 | for i := range exec.Stack { 35 | instruction := &exec.Stack[i] 36 | for j := range instruction.Args { 37 | arg := &instruction.Args[j] 38 | isUnique := false 39 | for !isUnique { 40 | isUnique = true 41 | for key, existingArg := range allArgs { 42 | if existingArg.Name == arg.Name { 43 | isUnique = false 44 | arg.Name = arg.Name + "_" + strconv.Itoa(key) 45 | break 46 | } 47 | } 48 | } 49 | allArgs = append(allArgs, *arg) 50 | variables[arg.Name] = arg.Value 51 | } 52 | } 53 | query := exec.Client.ProcessInstructions(exec.Stack) 54 | return query, variables 55 | } 56 | 57 | func (exec *Exec) Exec(ctx context.Context, v interface{}) (bool, error) { 58 | query, variables := exec.buildQuery() 59 | data, err := exec.Client.GraphQL(ctx, query, variables) 60 | if err != nil { 61 | return false, err 62 | } 63 | if data == nil { 64 | return false, nil 65 | } 66 | 67 | return exec.Client.decode(exec, data, v) 68 | } 69 | 70 | func (exec *Exec) Exists(ctx context.Context) (bool, error) { 71 | query, variables := exec.buildQuery() 72 | data, err := exec.Client.GraphQL(ctx, query, variables) 73 | if err != nil { 74 | return false, err 75 | } 76 | if len(data) == 0 { 77 | return false, nil 78 | } 79 | for _, v := range data { 80 | return v != nil, nil 81 | } 82 | panic("unreachable") 83 | } 84 | 85 | func (exec *Exec) ExecArray(ctx context.Context, v interface{}) error { 86 | query := exec.Client.ProcessInstructions(exec.Stack) 87 | variables := make(map[string]interface{}) 88 | for _, instruction := range exec.Stack { 89 | for _, arg := range instruction.Args { 90 | variables[arg.Name] = arg.Value 91 | } 92 | } 93 | data, err := exec.Client.GraphQL(ctx, query, variables) 94 | if err != nil { 95 | return err 96 | } 97 | _, err = exec.Client.decode(exec, data, v) 98 | return err 99 | } 100 | 101 | func (exec *BatchPayloadExec) Exec(ctx context.Context) (BatchPayload, error) { 102 | sexec := &Exec{Stack: exec.stack} 103 | query, variables := sexec.buildQuery() 104 | 105 | data, err := exec.client.GraphQL(ctx, query, variables) 106 | if err != nil { 107 | return BatchPayload{}, err 108 | } 109 | 110 | var bp BatchPayload 111 | _, err = exec.client.decode(sexec, data, &bp) 112 | return bp, err 113 | } 114 | -------------------------------------------------------------------------------- /lib.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | import ( 4 | "context" 5 | "fmt" 6 | "reflect" 7 | 8 | jwt "github.com/dgrijalva/jwt-go" 9 | "github.com/machinebox/graphql" 10 | ) 11 | 12 | type Exec struct { 13 | Client *Client 14 | Stack []instruction 15 | } 16 | 17 | type graphQLField struct { 18 | Name string 19 | TypeName string 20 | TypeFields []string 21 | } 22 | 23 | type graphQLArg struct { 24 | Name string 25 | Key string 26 | TypeName string 27 | Value interface{} 28 | } 29 | 30 | type instruction struct { 31 | Name string 32 | Field graphQLField 33 | Operation string 34 | Args []graphQLArg 35 | } 36 | 37 | // TODO(dh): get rid of this function if we can 38 | func isArray(i interface{}) bool { 39 | v := reflect.ValueOf(i) 40 | switch v.Kind() { 41 | case reflect.Array: 42 | return true 43 | case reflect.Slice: 44 | return true 45 | default: 46 | return false 47 | } 48 | } 49 | 50 | func New(endpoint string, secret string, opts ...graphql.ClientOption) *Client { 51 | 52 | var tokenString string 53 | if secret != "" { 54 | token := jwt.New(jwt.SigningMethodHS256) 55 | signedToken, err := token.SignedString([]byte(secret)) 56 | if err != nil { 57 | fmt.Println("Failed to sign JWT token") 58 | panic(err) 59 | } 60 | tokenString = signedToken 61 | } 62 | 63 | return &Client{ 64 | Endpoint: endpoint, 65 | Secret: tokenString, 66 | GQLClient: graphql.NewClient(endpoint, opts...), 67 | } 68 | } 69 | 70 | type Client struct { 71 | Endpoint string 72 | Secret string 73 | // TODO(dh): find a better name for this field 74 | GQLClient *graphql.Client 75 | } 76 | 77 | // GraphQL Send a GraphQL operation request 78 | func (client *Client) GraphQL(ctx context.Context, query string, variables map[string]interface{}) (map[string]interface{}, error) { 79 | 80 | req := graphql.NewRequest(query) 81 | 82 | if client.Secret != "" { 83 | req.Header.Add("Authorization", "Bearer "+client.Secret) 84 | } 85 | 86 | for key, value := range variables { 87 | req.Var(key, value) 88 | } 89 | 90 | var respData map[string]interface{} 91 | if err := client.GQLClient.Run(ctx, req, &respData); err != nil { 92 | return nil, err 93 | } 94 | return respData, nil 95 | } 96 | 97 | func (client *Client) ProcessInstructions(stack []instruction) string { 98 | query := make(map[string]interface{}) 99 | argsByInstruction := make(map[string][]graphQLArg) 100 | var allArgs []graphQLArg 101 | firstInstruction := stack[0] 102 | 103 | // XXX why are we walking over the stack backwards? can't we just 104 | // walk it forwards and construct the AST? 105 | for i := len(stack) - 1; i >= 0; i-- { 106 | instruction := stack[i] 107 | if len(query) == 0 { 108 | query[instruction.Name] = instruction.Field.TypeFields 109 | argsByInstruction[instruction.Name] = instruction.Args 110 | allArgs = append(allArgs, instruction.Args...) 111 | } else { 112 | previousInstruction := stack[i+1] 113 | query[instruction.Name] = map[string]interface{}{ 114 | previousInstruction.Name: query[previousInstruction.Name], 115 | } 116 | argsByInstruction[instruction.Name] = instruction.Args 117 | allArgs = append(allArgs, instruction.Args...) 118 | delete(query, previousInstruction.Name) 119 | } 120 | } 121 | 122 | var opTyp operationType 123 | switch op := firstInstruction.Operation; op { 124 | case "query": 125 | opTyp = opQuery 126 | case "mutation": 127 | opTyp = opMutation 128 | case "subscription": 129 | opTyp = opSubscription 130 | default: 131 | panic(fmt.Sprintf("invalid operation type %q", op)) 132 | } 133 | op := operation{ 134 | typ: opTyp, 135 | name: firstInstruction.Name, 136 | } 137 | for _, arg := range allArgs { 138 | op.arguments = append(op.arguments, argument{ 139 | name: "$" + arg.Name, 140 | value: arg.TypeName, 141 | }) 142 | } 143 | 144 | var fn func(root fielder, query map[string]interface{}) 145 | fn = func(root fielder, query map[string]interface{}) { 146 | // XXX can len(query) ever be larger than 1? 147 | for k, v := range query { 148 | q := objectField{ 149 | name: k, 150 | } 151 | args := argsByInstruction[k] 152 | for _, arg := range args { 153 | q.arguments = append(q.arguments, argument{ 154 | name: arg.Key, 155 | value: "$" + arg.Name, 156 | }) 157 | } 158 | // TODO(dh): redesign the whole instruction processing step, 159 | // avoid excessive use of interface{} and maps 160 | switch v := v.(type) { 161 | case []string: 162 | for _, f := range v { 163 | q.fields = append(q.fields, scalarField{ 164 | name: f, 165 | }) 166 | } 167 | case map[string]interface{}: 168 | fn(&q, v) 169 | default: 170 | panic(fmt.Sprintf("unexpected type %T", v)) 171 | } 172 | root.addField(q) 173 | } 174 | } 175 | fn(&op, query) 176 | 177 | return formatOperation(&op) 178 | } 179 | -------------------------------------------------------------------------------- /read.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | func (client *Client) GetOne(base *Exec, params interface{}, typeNames [2]string, instrName string, typeFields []string) *Exec { 4 | var args []graphQLArg 5 | if params != nil { 6 | args = append(args, graphQLArg{ 7 | Name: "where", 8 | Key: "where", 9 | TypeName: typeNames[0], 10 | Value: params, 11 | }) 12 | } 13 | 14 | var stack []instruction 15 | if base != nil { 16 | stack = make([]instruction, len(base.Stack), len(base.Stack)+1) 17 | copy(stack, base.Stack) 18 | } 19 | stack = append(stack, instruction{ 20 | Name: instrName, 21 | Field: graphQLField{ 22 | Name: instrName, 23 | TypeName: typeNames[1], 24 | TypeFields: typeFields, 25 | }, 26 | Operation: "query", 27 | Args: args, 28 | }) 29 | 30 | return &Exec{ 31 | Client: client, 32 | Stack: stack, 33 | } 34 | } 35 | 36 | type WhereParams struct { 37 | Where interface{} `json:"where,omitempty"` 38 | OrderBy *string `json:"orderBy,omitempty"` 39 | Skip *int32 `json:"skip,omitempty"` 40 | After *string `json:"after,omitempty"` 41 | Before *string `json:"before,omitempty"` 42 | First *int32 `json:"first,omitempty"` 43 | Last *int32 `json:"last,omitempty"` 44 | } 45 | 46 | func (client *Client) GetMany(base *Exec, params *WhereParams, typeNames [3]string, instrName string, typeFields []string) *Exec { 47 | var args []graphQLArg 48 | if params != nil { 49 | if params.Where != nil { 50 | args = append(args, graphQLArg{ 51 | Name: "where", 52 | Key: "where", 53 | TypeName: typeNames[0], 54 | Value: params.Where, 55 | }) 56 | } 57 | if params.OrderBy != nil { 58 | args = append(args, graphQLArg{ 59 | Name: "orderBy", 60 | Key: "orderBy", 61 | TypeName: typeNames[1], 62 | Value: *params.OrderBy, 63 | }) 64 | } 65 | if params.Skip != nil { 66 | args = append(args, graphQLArg{ 67 | Name: "skip", 68 | Key: "skip", 69 | TypeName: "Int", 70 | Value: *params.Skip, 71 | }) 72 | } 73 | if params.After != nil { 74 | args = append(args, graphQLArg{ 75 | Name: "after", 76 | Key: "after", 77 | TypeName: "String", 78 | Value: *params.After, 79 | }) 80 | } 81 | if params.Before != nil { 82 | args = append(args, graphQLArg{ 83 | Name: "before", 84 | Key: "before", 85 | TypeName: "String", 86 | Value: *params.Before, 87 | }) 88 | } 89 | if params.First != nil { 90 | args = append(args, graphQLArg{ 91 | Name: "first", 92 | Key: "first", 93 | TypeName: "Int", 94 | Value: *params.First, 95 | }) 96 | } 97 | if params.Last != nil { 98 | args = append(args, graphQLArg{ 99 | Name: "last", 100 | Key: "last", 101 | TypeName: "Int", 102 | Value: *params.Last, 103 | }) 104 | } 105 | } 106 | 107 | var stack []instruction 108 | if base != nil { 109 | stack = make([]instruction, len(base.Stack), len(base.Stack)+1) 110 | copy(stack, base.Stack) 111 | } 112 | stack = append(stack, instruction{ 113 | Name: instrName, 114 | Field: graphQLField{ 115 | Name: instrName, 116 | TypeName: typeNames[2], 117 | TypeFields: typeFields, 118 | }, 119 | Operation: "query", 120 | Args: args, 121 | }) 122 | 123 | return &Exec{ 124 | Client: client, 125 | Stack: stack, 126 | } 127 | } 128 | -------------------------------------------------------------------------------- /read_test.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | import ( 4 | "testing" 5 | ) 6 | 7 | func TestGetOne(t *testing.T) { 8 | tests := []struct { 9 | arg1 interface{} 10 | arg2 [2]string 11 | arg3 string 12 | arg4 []string 13 | query string 14 | numParams int 15 | }{ 16 | { 17 | nil, [2]string{"UserWhereUniqueInput!", "User"}, "user", []string{"field1", "field2"}, 18 | "query user {\nuser {\nfield1\nfield2\n}\n}", 19 | 0, 20 | }, 21 | { 22 | struct{}{}, [2]string{"UserWhereUniqueInput!", "User"}, "user", []string{"field1", "field2"}, 23 | "query user($where: UserWhereUniqueInput!) {\nuser(where: $where) {\nfield1\nfield2\n}\n}", 24 | 1, 25 | }, 26 | } 27 | 28 | client := New("") 29 | for _, tt := range tests { 30 | exec := client.GetOne(nil, tt.arg1, tt.arg2, tt.arg3, tt.arg4) 31 | q, params := exec.buildQuery() 32 | if len(params) != tt.numParams { 33 | t.Errorf("got %d variables, want %d", len(params), tt.numParams) 34 | } 35 | if q != tt.query { 36 | t.Errorf("got %q, want %q", q, tt.query) 37 | } 38 | } 39 | } 40 | 41 | func newInt32(v int32) *int32 { return &v } 42 | func newString(v string) *string { return &v } 43 | 44 | func TestGetMany(t *testing.T) { 45 | tests := []struct { 46 | arg1 *WhereParams 47 | arg2 [3]string 48 | arg3 string 49 | arg4 []string 50 | query string 51 | numParams int 52 | }{ 53 | { 54 | &WhereParams{ 55 | Where: struct{}{}, 56 | OrderBy: newString("ORDERBY"), 57 | Skip: newInt32(5), 58 | After: newString("AFTER"), 59 | Before: newString("BEFORE"), 60 | First: newInt32(23), 61 | Last: newInt32(42), 62 | }, 63 | [3]string{"UserWhereInput", "UserOrderByInput", "User"}, 64 | "users", 65 | []string{"field1"}, 66 | "query users($where: UserWhereInput, $orderBy: UserOrderByInput, $skip: Int, $after: String, $before: String, $first: Int, $last: Int) {\nusers(where: $where, orderBy: $orderBy, skip: $skip, after: $after, before: $before, first: $first, last: $last) {\nfield1\n}\n}", 67 | 7, 68 | }, 69 | { 70 | &WhereParams{ 71 | Where: struct{}{}, 72 | OrderBy: nil, 73 | Skip: newInt32(5), 74 | After: nil, 75 | Before: newString("BEFORE"), 76 | First: newInt32(23), 77 | Last: nil, 78 | }, 79 | [3]string{"UserWhereInput", "UserOrderByInput", "User"}, 80 | "users", 81 | []string{"field1"}, 82 | "query users($where: UserWhereInput, $skip: Int, $before: String, $first: Int) {\nusers(where: $where, skip: $skip, before: $before, first: $first) {\nfield1\n}\n}", 83 | 4, 84 | }, 85 | } 86 | 87 | client := New("") 88 | for _, tt := range tests { 89 | exec := client.GetMany(nil, tt.arg1, tt.arg2, tt.arg3, tt.arg4) 90 | q, params := exec.buildQuery() 91 | if len(params) != tt.numParams { 92 | t.Errorf("got %d variables, want %d", len(params), tt.numParams) 93 | } 94 | if q != tt.query { 95 | t.Errorf("got %q, want %q", q, tt.query) 96 | } 97 | } 98 | } 99 | -------------------------------------------------------------------------------- /write.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | type BatchPayloadExec struct { 4 | client *Client 5 | stack []instruction 6 | } 7 | 8 | type BatchPayload struct { 9 | Count int64 `json:"count"` 10 | } 11 | 12 | type UpdateParams struct { 13 | Data interface{} 14 | Where interface{} 15 | } 16 | 17 | func (client *Client) UpdateMany(params UpdateParams, typeNames [2]string, instrName string) *BatchPayloadExec { 18 | var args []graphQLArg 19 | args = append(args, graphQLArg{ 20 | Name: "data", 21 | Key: "data", 22 | TypeName: typeNames[0], 23 | Value: params.Data, 24 | }) 25 | if params.Where != nil { 26 | args = append(args, graphQLArg{ 27 | Name: "where", 28 | Key: "where", 29 | TypeName: typeNames[1], 30 | Value: params.Where, 31 | }) 32 | } 33 | 34 | stack := []instruction{{ 35 | Name: instrName, 36 | Field: graphQLField{ 37 | Name: instrName, 38 | TypeName: "BatchPayload", 39 | TypeFields: []string{"count"}, 40 | }, 41 | Operation: "mutation", 42 | Args: args, 43 | }} 44 | 45 | return &BatchPayloadExec{ 46 | client: client, 47 | stack: stack, 48 | } 49 | } 50 | 51 | func (client *Client) Update(params UpdateParams, typeNames [3]string, instrName string, typeFields []string) *Exec { 52 | var args []graphQLArg 53 | args = append(args, graphQLArg{ 54 | Name: "data", 55 | Key: "data", 56 | TypeName: typeNames[0], 57 | Value: params.Data, 58 | }) 59 | args = append(args, graphQLArg{ 60 | Name: "where", 61 | Key: "where", 62 | TypeName: typeNames[1], 63 | Value: params.Where, 64 | }) 65 | 66 | stack := []instruction{{ 67 | Name: instrName, 68 | Field: graphQLField{ 69 | Name: instrName, 70 | TypeName: typeNames[2], 71 | TypeFields: typeFields, 72 | }, 73 | Operation: "mutation", 74 | Args: args, 75 | }} 76 | 77 | return &Exec{ 78 | Client: client, 79 | Stack: stack, 80 | } 81 | } 82 | 83 | func (client *Client) DeleteMany(params interface{}, typeName string, instrName string) *BatchPayloadExec { 84 | args := []graphQLArg{{ 85 | Name: "where", 86 | Key: "where", 87 | TypeName: typeName, 88 | Value: params, 89 | }} 90 | 91 | stack := []instruction{{ 92 | Name: instrName, 93 | Field: graphQLField{ 94 | Name: instrName, 95 | TypeName: "BatchPayload", 96 | TypeFields: []string{"count"}, 97 | }, 98 | Operation: "mutation", 99 | Args: args, 100 | }} 101 | 102 | return &BatchPayloadExec{ 103 | client: client, 104 | stack: stack, 105 | } 106 | } 107 | 108 | func (client *Client) Delete(params interface{}, typeNames [2]string, instrName string, typeFields []string) *Exec { 109 | var args []graphQLArg 110 | if params != nil { 111 | args = []graphQLArg{{ 112 | Name: "where", 113 | Key: "where", 114 | TypeName: typeNames[0], 115 | Value: params, 116 | }} 117 | } 118 | 119 | stack := []instruction{{ 120 | Name: instrName, 121 | Field: graphQLField{ 122 | Name: instrName, 123 | TypeName: typeNames[1], 124 | TypeFields: typeFields, 125 | }, 126 | Operation: "mutation", 127 | Args: args, 128 | }} 129 | 130 | return &Exec{ 131 | Client: client, 132 | Stack: stack, 133 | } 134 | } 135 | 136 | func (client *Client) Create(params interface{}, typeNames [2]string, instrName string, typeFields []string) *Exec { 137 | var args []graphQLArg 138 | if params != nil { 139 | args = append(args, graphQLArg{ 140 | Name: "data", 141 | Key: "data", 142 | TypeName: typeNames[0], 143 | Value: params, 144 | }) 145 | } 146 | 147 | stack := []instruction{{ 148 | Name: instrName, 149 | Field: graphQLField{ 150 | Name: instrName, 151 | TypeName: typeNames[1], 152 | TypeFields: typeFields, 153 | }, 154 | Operation: "mutation", 155 | Args: args, 156 | }} 157 | 158 | return &Exec{ 159 | Client: client, 160 | Stack: stack, 161 | } 162 | } 163 | 164 | type UpsertParams struct { 165 | Where interface{} 166 | Create interface{} 167 | Update interface{} 168 | } 169 | 170 | func (client *Client) Upsert(params *UpsertParams, typeNames [4]string, instrName string, typeFields []string) *Exec { 171 | var args []graphQLArg 172 | if params != nil { 173 | args = append(args, graphQLArg{ 174 | Name: "where", 175 | Key: "where", 176 | TypeName: typeNames[0], 177 | Value: params.Where, 178 | }) 179 | args = append(args, graphQLArg{ 180 | Name: "create", 181 | Key: "create", 182 | TypeName: typeNames[1], 183 | Value: params.Create, 184 | }) 185 | args = append(args, graphQLArg{ 186 | Name: "update", 187 | Key: "update", 188 | TypeName: typeNames[2], 189 | Value: params.Update, 190 | }) 191 | } 192 | 193 | stack := []instruction{{ 194 | Name: instrName, 195 | Field: graphQLField{ 196 | Name: instrName, 197 | TypeName: typeNames[3], 198 | TypeFields: typeFields, 199 | }, 200 | Operation: "mutation", 201 | Args: args, 202 | }} 203 | 204 | return &Exec{ 205 | Client: client, 206 | Stack: stack, 207 | } 208 | } 209 | -------------------------------------------------------------------------------- /write_test.go: -------------------------------------------------------------------------------- 1 | package prisma 2 | 3 | import "testing" 4 | 5 | func TestUpdate(t *testing.T) { 6 | tests := []struct { 7 | arg0 UpdateParams 8 | arg1 [3]string 9 | arg2 string 10 | arg3 []string 11 | query string 12 | numParams int 13 | }{ 14 | { 15 | UpdateParams{}, 16 | [3]string{"UserUpdateInput!", "UserWhereUniqueInput!", "User"}, 17 | "updateUser", 18 | []string{"field1", "field2"}, 19 | "mutation updateUser($data: UserUpdateInput!, $where: UserWhereUniqueInput!) {\nupdateUser(data: $data, where: $where) {\nfield1\nfield2\n}\n}", 20 | 2, 21 | }, 22 | { 23 | UpdateParams{ 24 | Data: struct{}{}, 25 | Where: struct{}{}, 26 | }, 27 | [3]string{"UserUpdateInput!", "UserWhereUniqueInput!", "User"}, 28 | "updateUser", 29 | []string{"field1", "field2"}, 30 | "mutation updateUser($data: UserUpdateInput!, $where: UserWhereUniqueInput!) {\nupdateUser(data: $data, where: $where) {\nfield1\nfield2\n}\n}", 31 | 2, 32 | }, 33 | } 34 | 35 | client := New("") 36 | for _, tt := range tests { 37 | exec := client.Update(tt.arg0, tt.arg1, tt.arg2, tt.arg3) 38 | q, params := exec.buildQuery() 39 | if len(params) != tt.numParams { 40 | t.Errorf("got %d variables, want %d", len(params), tt.numParams) 41 | } 42 | if q != tt.query { 43 | t.Errorf("got %q, want %q", q, tt.query) 44 | } 45 | } 46 | } 47 | --------------------------------------------------------------------------------