diff --git a/src/express.ts b/src/express.ts index 4ab8be0c..90b00649 100644 --- a/src/express.ts +++ b/src/express.ts @@ -157,7 +157,8 @@ export function createRouter(sofa: Sofa): Router { if (!url.startsWith(sofa.basePath)) { return null; } - const slicedUrl = url.slice(sofa.basePath.length); + // trim base path and search + const [slicedUrl] = url.slice(sofa.basePath.length).split('?'); const trouterMethod = method.toUpperCase() as Trouter.HTTPMethod; const obj = router.find(trouterMethod, slicedUrl); for (const handler of obj.handlers) { diff --git a/tests/router.spec.ts b/tests/router.spec.ts index f4988621..e7011e84 100644 --- a/tests/router.spec.ts +++ b/tests/router.spec.ts @@ -214,3 +214,46 @@ test('should work with scalars', (done) => { } }); }); + +test('should support search params in url', async () => { + const users = [ + { + id: 'user:foo', + name: 'Foo', + }, + ]; + const spy = jest.fn(() => users); + + const sofa = useSofa({ + basePath: '/api', + schema: makeExecutableSchema({ + typeDefs: /* GraphQL */ ` + type User { + id: ID + name: String + } + type Query { + users(count: Int!): [User] + } + `, + resolvers: { + Query: { + users: spy, + }, + }, + }), + }); + + const app = express(); + app.use(bodyParser.json()); + app.use('/api', sofa); + + const res = await supertest(app).get('/api/users?count=5').expect(200); + expect(res.body).toEqual(users); + expect(spy).lastCalledWith( + undefined, + { count: 5 }, + expect.anything(), + expect.anything() + ); +});