| 182 | } |
| 183 | |
| 184 | func Example_insertWithPresets() { |
| 185 | gql := `mutation { |
| 186 | products(insert: $data) { |
| 187 | id |
| 188 | name |
| 189 | owner { |
| 190 | id |
| 191 | email |
| 192 | } |
| 193 | } |
| 194 | }` |
| 195 | |
| 196 | vars := json.RawMessage(`{ |
| 197 | "data": { |
| 198 | "id": 2001, |
| 199 | "name": "Product 2001", |
| 200 | "description": "Description for product 2001", |
| 201 | "price": 2011.5, |
| 202 | "tags": ["Tag 1", "Tag 2"], |
| 203 | "category_ids": [1, 2, 3, 4, 5] |
| 204 | } |
| 205 | }`) |
| 206 | |
| 207 | conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true}) |
| 208 | err := conf.AddRoleTable("user", "products", core.Insert{ |
| 209 | Presets: map[string]string{"owner_id": "$user_id"}, |
| 210 | }) |
| 211 | if err != nil { |
| 212 | panic(err) |
| 213 | } |
| 214 | |
| 215 | gj, err := core.NewGraphJin(conf, db) |
| 216 | if err != nil { |
| 217 | panic(err) |
| 218 | } |
| 219 | defer gj.Close() |
| 220 | |
| 221 | ctx := context.WithValue(context.Background(), core.UserIDKey, 3) |
| 222 | res, err := gj.GraphQL(ctx, gql, vars, nil) |
| 223 | if err != nil { |
| 224 | fmt.Println(err) |
| 225 | } else { |
| 226 | printJSON(res.Data) |
| 227 | } |
| 228 | |
| 229 | // Cleanup: delete the inserted product to avoid polluting other tests |
| 230 | _, _ = db.Exec("DELETE FROM products WHERE id = 2001") |
| 231 | |
| 232 | // Output: {"products":[{"id":2001,"name":"Product 2001","owner":{"email":"user3@test.com","id":3}}]} |
| 233 | } |
| 234 | |
| 235 | func Example_insertBulk() { |
| 236 | |