Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(handler): Custom request params parser #100

Merged
merged 7 commits into from
Jul 8, 2023
Merged

Conversation

enisdenjo
Copy link
Member

@enisdenjo enisdenjo commented Jul 7, 2023

Closes #36, closes #12

The spec explicitly allows other media types and request methods to be used. Meaning, it should be possible to have it working in graphql-http without breaching spec compliance.

Examples

Start the server with http

import http from 'http';
import { createHandler } from 'graphql-http/lib/use/http';
import processRequest from 'graphql-upload/processRequest.mjs'; // yarn add graphql-upload
import { schema } from './my-graphql';

const handler = createHandler({
  schema,
  async parseRequestParams(req) {
    const params = await processRequest(req.raw, req.context.res);
    if (Array.isArray(params)) {
      throw new Error('Batching is not supported');
    }
    return {
      ...params,
      // variables must be an object as per the GraphQL over HTTP spec
      variables: Object(params.variables),
    };
  },
});

const server = http.createServer((req, res) => {
  if (req.url.startsWith('/graphql')) {
    handler(req, res);
  } else {
    res.writeHead(404).end();
  }
});

server.listen(4000);
console.log('Listening to port 4000');

Start the server with express

import express from 'express'; // yarn add express
import { createHandler } from 'graphql-http/lib/use/express';
import processRequest from 'graphql-upload/processRequest.mjs'; // yarn add graphql-upload
import { schema } from './my-graphql';

const app = express();
app.all(
  '/graphql',
  createHandler({
    schema,
    async parseRequestParams(req) {
      const params = await processRequest(req.raw, req.context.res);
      if (Array.isArray(params)) {
        throw new Error('Batching is not supported');
      }
      return {
        ...params,
        // variables must be an object as per the GraphQL over HTTP spec
        variables: Object(params.variables),
      };
    },
  }),
);

app.listen({ port: 4000 });
console.log('Listening to port 4000');

@enisdenjo enisdenjo force-pushed the parse-request-params branch from e507b87 to d80a86a Compare July 8, 2023 07:52
@enisdenjo enisdenjo marked this pull request as ready for review July 8, 2023 08:59
@enisdenjo enisdenjo force-pushed the parse-request-params branch from 41c2de6 to 05c3daf Compare July 8, 2023 09:32
@enisdenjo enisdenjo merged commit b919d7e into main Jul 8, 2023
@enisdenjo enisdenjo deleted the parse-request-params branch July 8, 2023 09:44
enisdenjo pushed a commit that referenced this pull request Jul 8, 2023
# [1.20.0](v1.19.0...v1.20.0) (2023-07-08)

### Bug Fixes

* **handler:** Don't export `makeResponse`, `getAcceptableMediaType` or `isResponse` ([#98](#98)) ([a638cb4](a638cb4))
* **handler:** Request params optional properties can also be null ([10a6f06](10a6f06))

### Features

* **handler:** Custom request params parser ([#100](#100)) ([b919d7e](b919d7e))
@enisdenjo
Copy link
Member Author

🎉 This PR is included in version 1.20.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@enisdenjo enisdenjo added the released Has been released and published label Jul 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
released Has been released and published
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant