【问题标题】:How to set CORs using Lacinia Pedestal?如何使用 Lacinia Pedestal 设置 COR?
【发布时间】:2019-08-14 11:37:43
【问题描述】:

我正在使用 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]))})

客户端代码超级简单(使用 Apollo):

const client = new ApolloClient({
  uri: "http://localhost:8888/graphql"
});

【问题讨论】:

  • 浏览器在 devtools 控制台中记录的确切错误消息是什么?
  • 错误消息是“404 not found”,虽然我设法修复它(见下面的答案)。

标签: server clojure cors graphql pedestal


【解决方案1】:

更新:我设法通过将我的 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)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-09
    • 1970-01-01
    相关资源
    最近更新 更多