Skip to content

Commit

Permalink
Fix routing with search params (#700)
Browse files Browse the repository at this point in the history
I missed trouter expects only path to be provided without search params.
Fixed and added test.
  • Loading branch information
TrySound authored Feb 11, 2021
1 parent f3ac5eb commit 7e5f352
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/express.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
43 changes: 43 additions & 0 deletions tests/router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
);
});

0 comments on commit 7e5f352

Please sign in to comment.