我正在使用Clojure设置一个lacinia-pedestal graphql服务器,并尝试使用apollo通过客户端javascript代码访问它。但是,我无法访问localhost上的/graphql端点,因为我试图从CORs不允许的localhost源(localhost:3000)访问它。如何使用lacinia-pedestal设置CORs?
以下是服务器端代码(使用lacinia tutorial 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]))})客户端代码非常简单(使用Apollo):
const client = new ApolloClient({
uri: "http://localhost:8888/graphql"
});发布于 2019-03-24 11:59:25
更新:通过将我的lacinia底座服务地图与标准底座服务地图合并,我设法解决了这个问题。
(start [this]
(assoc this :server (-> schema-provider
:schema
(lp/service-map {:graphiql true})
(merge {::http/allowed-origins (constantly true)})
http/create-server
http/start)))https://stackoverflow.com/questions/55317305
复制相似问题