Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AppSync Support #98

Open
lowecg opened this issue Nov 11, 2024 · 0 comments
Open

AppSync Support #98

lowecg opened this issue Nov 11, 2024 · 0 comments

Comments

@lowecg
Copy link

lowecg commented Nov 11, 2024

Hi Oli,

I have re-graph working with AppSync and Cognito (Amplify), but I needed to make some changes to support subscription. I'll raise a PR very shortly to cast your eye over.

I needed to carefully configure re-graph using the JWT token from my Cognito authorised session. Might be worth getting this into the README?

(ns app.core
  (:require
    ...
    ["/aws-exports.js" :default aws-exports])) ;; generated by Amplify

(defn clj->json-str [obj]
  (.stringify js/JSON (clj->js obj)))

(defn str->base64 [s]
  (.btoa js/window s))

(defn extract-host [url]
  (-> url
      (string/replace #"^https?://" "")
      (string/split #"/")
      (first)))

(defn re-graph-appsync-init [{:keys [token] :as data}]
    (let [graphql-endpoint-url (.-aws_appsync_graphqlEndpoint aws-exports)
          ;; https://docs.aws.amazon.com/appsync/latest/devguide/real-time-websocket-client.html
          graphql-endpoint-ws-url (-> graphql-endpoint-url
                                      (string/replace #"^https:" "wss:")
                                      (string/replace #"\.appsync-api\." ".appsync-realtime-api."))

          appsync-host (extract-host graphql-endpoint-url)
          header-b64 (str->base64 (clj->json-str {"Host" appsync-host, "Authorization" token}))
          payload-b64 (str->base64 (clj->json-str {})) ;; documentation states that an empty payload is required here
          graphql-endpoint-ws-url-with-auth (str graphql-endpoint-ws-url "?header=" header-b64 "&payload=" payload-b64)]
      (rf/dispatch
        [:re-graph.core/init
         {:ws   {:url                  graphql-endpoint-ws-url-with-auth
                 :supported-operations #{:subscribe}
                 :impl                 {:subscription-query-as-data? true ;; this also will cause extensions to be included in the subscription query payload
                                        :extensions                  {"authorization" {"Authorization" token
                                                                                       "host"          appsync-host}}}}

          :http {:url                  graphql-endpoint-url
                 :supported-operations #{:query :mutation}
                 :impl                 {:with-credentials? false
                                        :headers           {"Authorization" token}}}}])))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant