Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Allow custom graphql params extraction from request #730

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 0 additions & 72 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions src/__tests__/http-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1029,6 +1029,36 @@ function runTests(server: Server) {
});
});

it('allow custom graphql params extraction from request', async () => {
const app = server();

app.get(
urlString(),
graphqlHTTP(
{
schema: TestSchema,
},
(request) => {
const searchParams = new URLSearchParams(request.url.split('?')[1]);
return {
query: searchParams.get('graphqlQuery'),
variables: null,
operationName: null,
raw: false,
};
},
),
);

const response = await app.request().get(
urlString({
graphqlQuery: '{test}',
}),
);

expect(response.text).to.equal('{"data":{"test":"Hello World"}}');
});

describe('Pretty printing', () => {
it('supports pretty printing', async () => {
const app = server();
Expand Down
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,16 @@ type Middleware = (request: Request, response: Response) => Promise<void>;
* Middleware for express; takes an options object or function as input to
* configure behavior, and returns an express middleware.
*/
export function graphqlHTTP(options: Options): Middleware {
export function graphqlHTTP(
options: Options,
/**
* An optional function which will get params from request instead of
* default getGraphQLParams
*/
customGraphQLParamsFn?: (
Copy link
Member

@acao acao Jan 3, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@TrySound is there a reason you didn't want to add this to the Options interface?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I cannot because of options getter which depends on params. If we could get rid of it Im good to move into options

It would look better in options though options getter depends on parsed
options.

request: Request,
) => GraphQLParams | Promise<GraphQLParams>,
): Middleware {
devAssert(options != null, 'GraphQL middleware requires options.');

return async function graphqlMiddleware(
Expand All @@ -210,11 +219,12 @@ export function graphqlHTTP(options: Options): Middleware {
let pretty = false;
let result: ExecutionResult;
let optionsData: OptionsData | undefined;
const graphQLParamsFn = customGraphQLParamsFn ?? getGraphQLParams;

try {
// Parse the Request to get GraphQL request parameters.
try {
params = await getGraphQLParams(request);
params = await graphQLParamsFn(request);
} catch (error: unknown) {
// When we failed to parse the GraphQL parameters, we still need to get
// the options object, so make an options call to resolve just that.
Expand Down