| 1371 | } |
| 1372 | |
| 1373 | func Example_queryWithRemoteAPIJoin() { |
| 1374 | gql := `query { |
| 1375 | users(order_by: { id: asc }) { |
| 1376 | email |
| 1377 | payments { |
| 1378 | desc |
| 1379 | } |
| 1380 | } |
| 1381 | }` |
| 1382 | |
| 1383 | // fake remote api service |
| 1384 | mux := http.NewServeMux() |
| 1385 | mux.HandleFunc("/payments/", func(w http.ResponseWriter, r *http.Request) { |
| 1386 | id := r.URL.Path[10:] |
| 1387 | w.Header().Set("Content-Type", "application/json") |
| 1388 | fmt.Fprintf(w, `{"data":[{"desc":"Payment 1 for %s"},{"desc": "Payment 2 for %s"}]}`, //nolint:errcheck |
| 1389 | id, id) |
| 1390 | }) |
| 1391 | |
| 1392 | // Use a listener to ensure we get an available port |
| 1393 | listener, err := net.Listen("tcp", "localhost:0") |
| 1394 | if err != nil { |
| 1395 | panic(err) |
| 1396 | } |
| 1397 | |
| 1398 | // Get the actual port that was assigned |
| 1399 | port := listener.Addr().(*net.TCPAddr).Port |
| 1400 | |
| 1401 | server := &http.Server{ |
| 1402 | Handler: mux, |
| 1403 | } |
| 1404 | |
| 1405 | go func() { |
| 1406 | log.Fatal(server.Serve(listener)) //nolint:gosec |
| 1407 | }() |
| 1408 | |
| 1409 | // Wait for server to be ready by polling the actual endpoint |
| 1410 | for i := 0; i < 100; i++ { |
| 1411 | resp, err := http.Get(fmt.Sprintf("http://localhost:%d/payments/test", port)) |
| 1412 | if err == nil { |
| 1413 | resp.Body.Close() //nolint:errcheck |
| 1414 | break |
| 1415 | } |
| 1416 | time.Sleep(50 * time.Millisecond) |
| 1417 | } |
| 1418 | |
| 1419 | conf := newConfig(&core.Config{DBType: dbType, DisableAllowList: true, DefaultLimit: 2}) |
| 1420 | conf.Resolvers = []core.ResolverConfig{{ |
| 1421 | Name: "payments", |
| 1422 | Type: "remote_api", |
| 1423 | Table: "users", |
| 1424 | Column: "stripe_id", |
| 1425 | StripPath: "data", |
| 1426 | Props: core.ResolverProps{"url": fmt.Sprintf("http://localhost:%d/payments/$id", port)}, |
| 1427 | }} |
| 1428 | |
| 1429 | gj, err := core.NewGraphJin(conf, db) |
| 1430 | if err != nil { |