首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Lacinia底座设置CORs?

如何使用Lacinia底座设置CORs?
EN

Stack Overflow用户
提问于 2019-03-24 02:56:52
回答 1查看 368关注 0票数 4

我正在使用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设置)

代码语言:javascript
复制
(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):

代码语言:javascript
复制
const client = new ApolloClient({
  uri: "http://localhost:8888/graphql"
});
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-03-24 11:59:25

更新:通过将我的lacinia底座服务地图与标准底座服务地图合并,我设法解决了这个问题。

代码语言:javascript
复制
 (start [this]
    (assoc this :server (-> schema-provider
                            :schema
                            (lp/service-map {:graphiql true})
                            (merge {::http/allowed-origins (constantly true)})
                            http/create-server
                            http/start)))
票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55317305

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档