Commit 8acf12ac authored by Przemyslaw Kaminski's avatar Przemyslaw Kaminski

[graphql] add authorization bearer to /gql queries

parent ba24537c
Pipeline #2043 failed with stage
module Gargantext.Components.GraphQL where
import Gargantext.Prelude
import Affjax.RequestHeader as ARH
import Data.Argonaut.Decode (JsonDecodeError)
import Data.Bifunctor (lmap)
import Data.List.Types (NonEmptyList)
......@@ -8,7 +11,7 @@ import Effect.Aff (Aff)
import Effect.Class (liftEffect)
import Foreign (unsafeToForeign, ForeignError)
import Gargantext.Components.GraphQL.User (User, UserInfo, UserInfoM)
import Gargantext.Prelude
import Gargantext.Sessions (Session(..))
import Gargantext.Utils.Reactix as R2
import GraphQL.Client.Args (type (==>))
import GraphQL.Client.BaseClients.Urql (UrqlClient, createClient)
......@@ -40,17 +43,22 @@ gqlQuery = queryWithDecoder (unsafeToForeign >>> JSON.read >>> lmap toJsonError
toJsonError :: NonEmptyList ForeignError -> JsonDecodeError
toJsonError = unsafeCoerce -- map ForeignErrors to JsonDecodeError as you wish
getClient :: Effect (Client UrqlClient Schema Mutation Void)
getClient = createClient { headers: [], url: "http://localhost:8008/gql" }
getClient :: Session -> Effect (Client UrqlClient Schema Mutation Void)
getClient (Session { token }) = createClient { headers, url: "http://localhost:8008/gql" }
where
headers = [ ARH.RequestHeader "Authorization" $ "Bearer " <> token ]
queryGql ::
forall query returns.
GqlQuery Schema query returns =>
JSON.ReadForeign returns =>
String -> query -> Aff returns
queryGql name q = do
Session
-> String
-> query
-> Aff returns
queryGql session name q = do
--query client name q
client <- liftEffect getClient
client <- liftEffect $ getClient session
gqlQuery (client :: Client UrqlClient Schema Mutation Void) name q
--query_ "http://localhost:8008/gql" (Proxy :: Proxy Schema)
......
......@@ -225,7 +225,7 @@ saveUserInfo :: Session -> Int -> UserInfo -> Aff (Either RESTError Int)
saveUserInfo session id ui = do
-- TODO GraphQL
-- pure $ Left $ CustomError "TODO implement graphql for saveUserInfo"
client <- liftEffect $ getClient
client <- liftEffect $ getClient session
res <- mutationOpts
(\m -> m)
client
......@@ -320,7 +320,7 @@ getUserInfoWithReload {nodeId, session} = getUserInfo session nodeId -- getConta
getUserInfo :: Session -> Int -> Aff (Either RESTError UserInfo)
getUserInfo session id = do
{ user_infos } <- queryGql "get user infos"
{ user_infos } <- queryGql session "get user infos"
{ user_infos: { user_id: id } =>>
{ ui_id: unit
, ui_username: unit
......@@ -383,7 +383,7 @@ getUserInfo session id = do
--
--getUser' :: Session -> Int -> Aff (Either RESTError ContactData)
--getUser' session id = do
-- { users } <- queryGql "get user"
-- { users } <- queryGql session "get user"
-- { users: { user_id: id } =>>
-- { u_id
-- , u_hyperdata:
......
......@@ -152,7 +152,7 @@ postMultipartFormData mtoken url body = do
, ARH.Accept applicationJSON
] <>
foldMap (\token ->
[ ARH.RequestHeader "Authorization" $ "Bearer " <> token ]
[ ARH.RequestHeader "Authorization" $ " " <> token ]
) mtoken
, content = Just $ formData fd
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment