-
Notifications
You must be signed in to change notification settings - Fork 46
feat(cloud-railway): implement real GraphQL API for provision/list/de… #440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -13,6 +13,45 @@ interface Config { | |||||||||||||
|
|
||||||||||||||
| const API = 'https://backboard.railway.app/graphql/v2'; | ||||||||||||||
|
|
||||||||||||||
| async function railwayGql<T = unknown>( | ||||||||||||||
| token: string, | ||||||||||||||
| query: string, | ||||||||||||||
| variables?: Record<string, unknown>, | ||||||||||||||
| ): Promise<T> { | ||||||||||||||
| const res = await fetch(API, { | ||||||||||||||
| method: 'POST', | ||||||||||||||
| headers: { | ||||||||||||||
| Authorization: `Bearer ${token}`, | ||||||||||||||
| 'Content-Type': 'application/json', | ||||||||||||||
| }, | ||||||||||||||
| body: JSON.stringify({ query, variables }), | ||||||||||||||
| }); | ||||||||||||||
| const json = (await res.json()) as { data?: T; errors?: Array<{ message: string }> }; | ||||||||||||||
| if (json.errors?.length) throw new Error(`Railway: ${json.errors![0]!.message}`); | ||||||||||||||
| if (!res.ok) throw new Error(`Railway HTTP ${res.status}`); | ||||||||||||||
| return json.data as T; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| function serviceToInstance(s: { | ||||||||||||||
| id: string; | ||||||||||||||
| createdAt?: string; | ||||||||||||||
| serviceInstances?: { edges: Array<{ node: { status?: string } }> }; | ||||||||||||||
| }, kind: Instance['kind'] = 'cpu-vps'): Instance { | ||||||||||||||
| const rawStatus = s.serviceInstances?.edges[0]?.node?.status ?? 'ACTIVE'; | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When
Suggested change
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time! |
||||||||||||||
| const status: Instance['status'] = | ||||||||||||||
| rawStatus === 'ACTIVE' || rawStatus === 'SUCCESS' ? 'running' : | ||||||||||||||
| rawStatus === 'BUILDING' || rawStatus === 'DEPLOYING' ? 'provisioning' : | ||||||||||||||
| rawStatus === 'REMOVED' ? 'destroyed' : 'failed'; | ||||||||||||||
| return { | ||||||||||||||
| id: s.id, | ||||||||||||||
| kind, | ||||||||||||||
| status, | ||||||||||||||
| createdAt: s.createdAt ?? new Date().toISOString(), | ||||||||||||||
| hourlyRate: 0, | ||||||||||||||
| currency: 'USD', | ||||||||||||||
| }; | ||||||||||||||
| } | ||||||||||||||
|
|
||||||||||||||
| export default defineCloud<Config>({ | ||||||||||||||
| id: 'cloud-railway', | ||||||||||||||
| label: 'Railway (scalable services)', | ||||||||||||||
|
|
@@ -25,22 +64,87 @@ export default defineCloud<Config>({ | |||||||||||||
|
|
||||||||||||||
| async quote(ctx, spec) { | ||||||||||||||
| ctx.log(`railway quote · kind=${spec.kind}`); | ||||||||||||||
| // Railway pricing is per-minute vCPU + memory (Hobby/Pro plan). Quote | ||||||||||||||
| // is a projection from Railway's resource-usage API. | ||||||||||||||
| return { hourly: 0, monthly: 0, currency: 'USD', provider: 'railway', sku: 'stub', spot: false }; | ||||||||||||||
| // Railway pricing is per-minute vCPU + memory (Hobby/Pro plan). | ||||||||||||||
| return { hourly: 0, monthly: 0, currency: 'USD', provider: 'railway', sku: 'usage', spot: false }; | ||||||||||||||
| }, | ||||||||||||||
|
|
||||||||||||||
| async provision(ctx, spec, config) { | ||||||||||||||
| ctx.log(`railway add/up · project=${config.projectId}`); | ||||||||||||||
| if (ctx.dryRun) return stub('dry-run', 'provisioning', spec.kind); | ||||||||||||||
| // TODO: GraphQL mutation serviceCreate({ projectId, name, source: { repo / image } }) | ||||||||||||||
| // For GPU workloads, Railway isn't viable — flag to user and suggest cloud-runpod. | ||||||||||||||
| return stub(`rw_${Date.now()}`, 'provisioning', spec.kind); | ||||||||||||||
| ctx.log(`railway serviceCreate · project=${config.projectId}`); | ||||||||||||||
| if (!config.projectId) throw new Error('config.projectId is required for Railway provisioning'); | ||||||||||||||
| if (spec.kind === 'gpu') throw new Error('GPU workloads not supported on Railway — use cloud-runpod instead'); | ||||||||||||||
| if (ctx.dryRun) return { id: 'dry-run', kind: spec.kind, status: 'provisioning', createdAt: new Date().toISOString(), hourlyRate: 0, currency: 'USD' }; | ||||||||||||||
|
Comment on lines
+73
to
+75
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
A caller that passes
Suggested change
|
||||||||||||||
|
|
||||||||||||||
| const token = ctx.secret('RAILWAY_TOKEN')!; | ||||||||||||||
| const data = await railwayGql<{ serviceCreate: { id: string; createdAt: string } }>( | ||||||||||||||
| token, | ||||||||||||||
| `mutation ServiceCreate($input: ServiceCreateInput!) { | ||||||||||||||
| serviceCreate(input: $input) { id createdAt } | ||||||||||||||
| }`, | ||||||||||||||
| { | ||||||||||||||
| input: { | ||||||||||||||
| name: `sh1pt-${spec.kind}-${Date.now()}`, | ||||||||||||||
| projectId: config.projectId, | ||||||||||||||
| ...(config.region ? { region: config.region } : {}), | ||||||||||||||
| }, | ||||||||||||||
| }, | ||||||||||||||
| ); | ||||||||||||||
| return serviceToInstance({ id: data.serviceCreate.id, createdAt: data.serviceCreate.createdAt }, spec.kind); | ||||||||||||||
| }, | ||||||||||||||
|
|
||||||||||||||
| async list(ctx, config) { | ||||||||||||||
| ctx.log(`railway services · project=${config?.projectId}`); | ||||||||||||||
| if (!config?.projectId) return []; | ||||||||||||||
| const token = ctx.secret('RAILWAY_TOKEN')!; | ||||||||||||||
| const data = await railwayGql<{ | ||||||||||||||
| project: { services: { edges: Array<{ node: { id: string; createdAt: string; serviceInstances: { edges: Array<{ node: { status: string } }> } } }> } }; | ||||||||||||||
| }>( | ||||||||||||||
| token, | ||||||||||||||
| `query ProjectServices($id: String!) { | ||||||||||||||
| project(id: $id) { | ||||||||||||||
| services { | ||||||||||||||
| edges { | ||||||||||||||
| node { | ||||||||||||||
| id | ||||||||||||||
| createdAt | ||||||||||||||
| serviceInstances { edges { node { status } } } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| } | ||||||||||||||
| }`, | ||||||||||||||
| { id: config.projectId }, | ||||||||||||||
| ); | ||||||||||||||
| return data.project.services.edges.map(e => serviceToInstance(e.node)); | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Railway's GraphQL API returns
Suggested change
|
||||||||||||||
| }, | ||||||||||||||
|
|
||||||||||||||
| async destroy(ctx, id) { | ||||||||||||||
| ctx.log(`railway serviceDelete · id=${id}`); | ||||||||||||||
| const token = ctx.secret('RAILWAY_TOKEN')!; | ||||||||||||||
| await railwayGql( | ||||||||||||||
| token, | ||||||||||||||
| `mutation ServiceDelete($id: ID!) { serviceDelete(id: $id) }`, | ||||||||||||||
| { id }, | ||||||||||||||
| ); | ||||||||||||||
| }, | ||||||||||||||
|
|
||||||||||||||
| async list(ctx) { ctx.log('railway service status --all --json'); return []; }, | ||||||||||||||
| async destroy(ctx, id) { ctx.log(`railway service delete ${id}`); }, | ||||||||||||||
| async status(ctx, id) { ctx.log(`railway service status --service ${id} --json`); return stub(id, 'running', 'cpu-vps'); }, | ||||||||||||||
| async status(ctx, id) { | ||||||||||||||
| ctx.log(`railway service status · id=${id}`); | ||||||||||||||
| const token = ctx.secret('RAILWAY_TOKEN')!; | ||||||||||||||
| const data = await railwayGql<{ | ||||||||||||||
| service: { id: string; createdAt: string; serviceInstances: { edges: Array<{ node: { status: string } }> } }; | ||||||||||||||
| }>( | ||||||||||||||
| token, | ||||||||||||||
| `query ServiceStatus($id: ID!) { | ||||||||||||||
| service(id: $id) { | ||||||||||||||
| id | ||||||||||||||
| createdAt | ||||||||||||||
| serviceInstances { edges { node { status } } } | ||||||||||||||
| } | ||||||||||||||
| }`, | ||||||||||||||
| { id }, | ||||||||||||||
| ); | ||||||||||||||
| return serviceToInstance(data.service); | ||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Same GraphQL null-data pattern as in
Suggested change
|
||||||||||||||
| }, | ||||||||||||||
|
|
||||||||||||||
| setup: tokenSetup({ | ||||||||||||||
| secretKey: 'RAILWAY_TOKEN', | ||||||||||||||
|
|
@@ -55,7 +159,3 @@ export default defineCloud<Config>({ | |||||||||||||
| ], | ||||||||||||||
| }), | ||||||||||||||
| }); | ||||||||||||||
|
|
||||||||||||||
| function stub(id: string, status: Instance['status'], kind: Instance['kind']): Instance { | ||||||||||||||
| return { id, kind, status, createdAt: new Date().toISOString(), hourlyRate: 0, currency: 'USD' }; | ||||||||||||||
| } | ||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
res.json()evaluated before HTTP error checkres.json()is called unconditionally on line 29, before!res.okis checked. When Railway returns a non-JSON error body — e.g. an HTML 429 rate-limit page or a 503 with a plain-text message — theawait res.json()call throws aSyntaxErrorthat propagates to the caller as an unintelligible parse failure instead of a meaningful "Railway HTTP 429". Swap the order: check!res.okwithawait res.text()first, then parse JSON only when the response looks valid.