Giter VIP home page Giter VIP logo

Comments (4)

d-exclaimation avatar d-exclaimation commented on July 22, 2024 1

Update on this, the package does technically work although it's very bare-bone at the moment and is missing GraphQL over WebSocket

from pioneer.

d-exclaimation avatar d-exclaimation commented on July 22, 2024

Hi, having a server serving both gRPC and GraphQL seemed possible. I haven't tried this personally but it would reckon it would not be too different from running gRPC with regular REST.

I have taken a look at the article but I am missing a lot context and information because the material (which I assumed is the starting point codebase) is not available unless I pay for it.

From my understanding, there is about 2 ways of having this setup:

  1. The easiest approach is to just have 2 servers with a different port running on 1 Swift application, with one for gRPC and HTTP/2 and the other is serving GraphQL which can be HTTP/1.1 or HTTP/2. Swift version 5 or higher have Task to help achieve this by running each server on their on Task. This is definitely not optimal but this is by far the easiest and simplest approach to it.
import Pioneer
import Vapor
import GRPCVapor
import NIOSSL

let gRPCTask = Task {
    let gApp = try Application(.detect())
    let certificates = try NIOSSLCertificate.fromPEMBytes(Array(sampleCert.utf8))
    let privateKey = try NIOSSLPrivateKey.init(bytes: Array(sampleKey.utf8), format: .pem)

    app.server.configuration.supportVersions = [.two]
    app.server.configuration.tlsConfiguration = .forServer(certificateChain: certificates.map { .certificate($0) }, privateKey: .privateKey(privateKey))

    app.middleware.use(GRPCMiddleware(services: [SomeGRPCService()]))
}

let gqlTask = Task {
    let server = try Pioneer(
        schema: schema(),
        resolver: Resolver(),
        httpStrategy: .csrfPrevention,
        introspection: true,
        playground: .sandbox
    )

    try server.standaloneServer(
         at: "graphql",
         context: { req, res in
             Context(req, res)
         }
    )
}
  1. The later is to run them on a single server. Taking a look at this example of setting up Vapor with gRPC and also serving REST endpoints, the only part that is of interest is that gRPC adapter is basically just a middleware. Pioneer is also just a Vapor middleware but it can also be apply on the route like a normal endpoint, so you can have the gRPC as a middleware and either also apply Pioneer as middleware (as far as I can tell, it won't cause conflict) or apply it on individual routes like a normal REST endpoint. This is more optimal because it have them running on 1 Vapor application but it will force your app to be on HTTP/2 because gRPC require HTTP/2. REST and GraphQL is usually using HTTP/1.1 for normal requests.
import Pioneer
import Vapor
import GRPCVapor
import NIOSSL

// Called before your application initializes.
public func configure(_ app: Application) throws {
    let server = try Pioneer(
        schema: schema(),
        resolver: Resolver(),
        httpStrategy: .csrfPrevention,
        introspection: true,
        playground: .sandbox
    )

    let certificates = try NIOSSLCertificate.fromPEMBytes(Array(sampleCert.utf8))
    let privateKey = try NIOSSLPrivateKey.init(bytes: Array(sampleKey.utf8), format: .pem)

    // Setting up HTTP/2 which is probably the cause of issues since normal REST and GraphQL is HTTP/1.1
    app.server.configuration.supportVersions = [.two]
    app.server.configuration.tlsConfiguration = .forServer(certificateChain: certificates.map { .certificate($0) }, privateKey: .privateKey(privateKey))

    app.middleware.use(GRPCMiddleware(services: [SomeGRPCService()]))
   
    app.group("graphql") { group in
        group.post { req in 
            try await server.httpHandler(req: req, context: { ... }) 
        }
    }
}

I haven't tried either approach but those 2 will be the approaches I would give a shot. I think the best issue in general is that gRPC is using HTTP/2 and making REST and GraphQL work with HTTP/2 is going to be difficult

from pioneer.

ZirgVoice avatar ZirgVoice commented on July 22, 2024

For Hummingbird there is a library that adds grpc support along with rest and rest seems to even work over HTTP/1.1. Is there a pioneer planned for the Hummingbird? I might be thinking about switching from Vapor to Hummingbird then 🙂

from pioneer.

d-exclaimation avatar d-exclaimation commented on July 22, 2024

It was on my todo a while back. I stopped only due to that I ran into a roadblock with how I would be able to handle both HTTP and WebSocket requests using 1 "/graphql" endpoint, mainly on upgrade WebSocket requests manually.

I made a repo for the integration/adapter which should at some point hold the library itself while I am still experimenting with it, and before I added into this package a

https://github.com/d-exclaimation/pioneer-hummingbird

I can't make a promise that I would be able to get something within the next 1-2 weeks as I am still relatively occupied. After 2 weeks or so, I might be able to get a working adapter but it would pretty experimental with bugs and not so polish DX yet.

If you need this urgently, you can also go have a shot on creating a simple integration for it. I made a guide on this here, and if you do, feel free to ask me questions about it and I'll try my best helping.

from pioneer.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.