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

[graphql] add authorization bearer to /gql queries

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