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, '/'), ), ),