server - 如何使用 Lacinia Pedestal 设置 COR?

标签 server clojure cors graphql pedestal

我正在使用 Clojure 设置 lacinia-pedestal graphql 服务器,并尝试使用 apollo 使用客户端 javascript 代码访问它。但是,我无法访问本地主机上的/graphql 端点,因为我试图从 COR 不允许的本地主机源 (localhost:3000) 访问它。如何使用 lacinia-pedestal 设置 COR?

这是服务器端代码(使用 lacinia 教程 https://lacinia.readthedocs.io/en/latest/tutorial/component.html 设置)

(ns project.server
  (:require [com.stuartsierra.component :as component]
            [com.walmartlabs.lacinia.pedestal :as lp]
            [io.pedestal.http :as http]))

(defrecord Server [schema-provider server]

  component/Lifecycle

  (start [this]
    (assoc this :server (-> schema-provider
                            :schema
                            (lp/service-map {:graphiql true})
                            http/create-server
                            http/start)))

  (stop [this]
    (http/stop server)
    (assoc this :server nil)))

(defn new-server
  []
  {:server (-> {}
               map->Server
               (component/using [:schema-provider]))})

客户端代码 super 简单(使用 Apollo):
const client = new ApolloClient({
  uri: "http://localhost:8888/graphql"
});

最佳答案

更新:我设法通过将我的 lacinia 基座服务 map 与标准基座服务 map 合并来解决它。

 (start [this]
    (assoc this :server (-> schema-provider
                            :schema
                            (lp/service-map {:graphiql true})
                            (merge {::http/allowed-origins (constantly true)})
                            http/create-server
                            http/start)))

关于server - 如何使用 Lacinia Pedestal 设置 COR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55317305/

相关文章:

swift - 在 Vapor 中使用临时文件响应请求的正确方法

clojure - leinigen repl 与配置文件

clojure - 为什么 clojure 允许小于 (<) 或大于 (>) 运算符/函数的单个参数

amazon-s3 - 让 S3 CORS Access-Control-Allow-Origin 动态回显请求域

c# - 来自 Vue 应用程序的 Post 请求被 CORS 策略阻止

java - Maven exec 无法识别 src/test 中的我的应用程序

Golang 在下载时停止导航

clojure - 将 Clojure 的数据结构与 MapDB 结合使用

php - Ajax CORS 替代品

node.js - 在真实的实时服务器中运行 socket.io 服务器,而不是本地主机?