MCPcopy
hub / github.com/dosco/graphjin / Example_queryWithOpenAPIJoin

Function Example_queryWithOpenAPIJoin

tests/openapi_test.go:31–154  ·  view source on GitHub ↗

Example_queryWithOpenAPIJoin exercises the full OpenAPI integration against a real GraphJin engine, end-to-end: - A temp OpenAPI 3 spec is dropped in a tempdir. - The mock upstream verifies that bearer auth was applied — if the header is missing the server panics, failing the test loudly. - A Graph

()

Source from the content-addressed store, hash-verified

29// package itself is dialect-independent, so one engine-level test gives
30// us proof the wiring works without N copies for N dialects.
31func Example_queryWithOpenAPIJoin() {
32 // Spin up a mock upstream that mimics a single-record lookup
33 // (GET /payments/{paymentId} returning {data: {desc, amount}}). The
34 // shape mirrors what Salesforce MC Personalization, Stripe-style
35 // services, and most CRUD-y REST APIs publish, so the test exercises
36 // the realistic case rather than a contrived one.
37 mux := http.NewServeMux()
38 mux.HandleFunc("/payments/", func(w http.ResponseWriter, r *http.Request) {
39 // Auth check: the only way this header is set is if the auth
40 // provider was constructed and wired into the resolver. A
41 // regression that bypasses auth surfaces here as a panic.
42 if got := r.Header.Get("Authorization"); got != "Bearer test-tok" {
43 panic(fmt.Sprintf("openapi join: missing/wrong auth: %q", got))
44 }
45 id := r.URL.Path[len("/payments/"):]
46 w.Header().Set("Content-Type", "application/json")
47 fmt.Fprintf(w, `{"data":{"desc":"Payment for %s","amount":100}}`, id) //nolint:errcheck
48 })
49
50 server := httptest.NewServer(mux)
51 defer server.Close()
52
53 // Drop a minimal OpenAPI spec into a tempdir. The loader scans this
54 // directory at NewGraphJin time and classifies the single GET as a
55 // row-join candidate (single trailing path param, JSON response).
56 specsDir, err := os.MkdirTemp("", "graphjin-openapi-test-*")
57 if err != nil {
58 panic(err)
59 }
60 defer os.RemoveAll(specsDir) //nolint:errcheck
61
62 specYAML := fmt.Sprintf(`
63openapi: 3.0.0
64info: { title: Payments, version: '1.0' }
65servers:
66 - url: %s
67paths:
68 /payments/{paymentId}:
69 get:
70 operationId: getPaymentById
71 parameters:
72 - { name: paymentId, in: path, required: true, schema: { type: string } }
73 responses:
74 '200':
75 description: ok
76 content:
77 application/json:
78 schema:
79 type: object
80 properties:
81 data:
82 type: object
83 properties:
84 desc: { type: string }
85 amount: { type: integer }
86`, server.URL)
87
88 specPath := filepath.Join(specsDir, "payments.yaml")

Callers

nothing calls this directly

Calls 8

CloseMethod · 0.95
GraphQLMethod · 0.95
newConfigFunction · 0.85
printJSONFunction · 0.85
GetMethod · 0.65
SetMethod · 0.65
CloseMethod · 0.65

Tested by

no test coverage detected