From 458f371285f921714fd73700186b3a5a3d3982ba Mon Sep 17 00:00:00 2001 From: Siddharth Gupta Date: Mon, 1 Jun 2026 17:04:11 -0700 Subject: [PATCH] fix(schema): support absolute schema paths Schema file paths were resolved with path.join(servicePath, schema), which prefixes absolute paths with the service directory and makes them resolve to a non-existent file (producing an empty schema). Use path.resolve instead: relative paths still resolve against the service directory, while absolute paths are honored as-is. Glob expansion and schema stitching are unaffected. Adds tests for absolute-path resolution and a relative-path regression guard. Closes #382 --- src/__tests__/schema.test.ts | 24 ++++++++++++++++++++++++ src/resources/Schema.ts | 5 ++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/__tests__/schema.test.ts b/src/__tests__/schema.test.ts index 74ec1995..98a6285e 100644 --- a/src/__tests__/schema.test.ts +++ b/src/__tests__/schema.test.ts @@ -1,3 +1,4 @@ +import * as path from 'path'; import { Api } from '../resources/Api'; import { Schema } from '../resources/Schema'; import * as given from './given'; @@ -229,6 +230,29 @@ describe('schema', () => { expect(output).toContain('AWSJSON'); }); + it('should support absolute schema paths regardless of servicePath', () => { + const plugin = given.plugin(); + // servicePath deliberately points somewhere the schema is NOT; + // an absolute schema path must still be read correctly. + plugin.serverless.config.servicePath = path.resolve('does', 'not', 'exist'); + const api = new Api(given.appSyncConfig(), plugin); + const absolutePath = path.resolve( + 'src/__tests__/fixtures/schemas/single/schema.graphql', + ); + const schema = new Schema(api, [absolutePath]); + expect(schema.generateSchema()).toContain('type Query'); + }); + + it('should resolve relative schema paths against servicePath', () => { + const plugin = given.plugin(); + plugin.serverless.config.servicePath = process.cwd(); + const api = new Api(given.appSyncConfig(), plugin); + const schema = new Schema(api, [ + 'src/__tests__/fixtures/schemas/single/schema.graphql', + ]); + expect(schema.generateSchema()).toContain('type Query'); + }); + it('should return single files schemas as-is', () => { const api = new Api(given.appSyncConfig(), plugin); const schema = new Schema(api, [ diff --git a/src/resources/Schema.ts b/src/resources/Schema.ts index fdbba11d..551e9343 100644 --- a/src/resources/Schema.ts +++ b/src/resources/Schema.ts @@ -85,7 +85,10 @@ export class Schema { globby.sync( this.schemas.map((schema) => path - .join(this.api.plugin.serverless.config.servicePath, schema) + // `path.resolve` keeps relative paths resolved against the + // service directory while leaving absolute paths untouched + // (`path.join` would incorrectly prefix them with servicePath). + .resolve(this.api.plugin.serverless.config.servicePath, schema) .replace(/\\/g, '/'), ), ),