Skip to content

feat: Implement server handler #2

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

Merged
merged 65 commits into from
Aug 4, 2022
Merged

feat: Implement server handler #2

merged 65 commits into from
Aug 4, 2022

Conversation

enisdenjo
Copy link
Member

@enisdenjo enisdenjo commented Aug 2, 2022

Complying with the GraphQL over HTTP Specification Working Draft (Jul 28, 2022).

Basic implementation with Node:

import http from 'http';
import { createHandler } from 'graphql-http';
import { schema } from './my-graphql';

const handler = createHandler({ schema });

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

  try {
    const [body, init] = await handler({
      url: req.url,
      method: req.method,
      headers: req.headers,
      body: await new Promise((resolve) => {
        let body = '';
        req.on('data', (chunk) => (body += chunk));
        req.on('end', () => resolve(body));
      }),
      raw: req,
    });
    res.writeHead(init.status, init.statusText, init.headers).end(body);
  } catch (err) {
    res.writeHead(500).end(err.message);
  }
});

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

@enisdenjo enisdenjo changed the title feat: Implement server feat: Implement server handler Aug 4, 2022
@enisdenjo enisdenjo merged commit 99b888b into master Aug 4, 2022
@enisdenjo enisdenjo deleted the server branch August 4, 2022 12:46
enisdenjo pushed a commit that referenced this pull request Aug 4, 2022
# 1.0.0 (2022-08-04)

### Features

* Implement client ([#3](#3)) ([c1b4c79](c1b4c79))
* Implement server handler ([#2](#2)) ([99b888b](99b888b))
@enisdenjo
Copy link
Member Author

🎉 This PR is included in version 1.0.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

@enisdenjo enisdenjo added the released Has been released and published label Aug 4, 2022
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