|
1 | 1 | -- | Creates GraphQL Apollo clients |
2 | 2 | module GraphQL.Client.BaseClients.Apollo |
3 | 3 | ( ApolloClientOptions |
| 4 | + , ApolloClientOptions' |
4 | 5 | , ApolloSubClientOptions |
| 6 | + , ApolloSubClientOptions' |
5 | 7 | , ApolloClient |
6 | 8 | , ApolloSubClient |
7 | 9 | , MutationOpts |
8 | 10 | , QueryOpts |
9 | 11 | , createClient |
| 12 | + , createClient' |
10 | 13 | , createSubscriptionClient |
| 14 | + , createSubscriptionClient' |
11 | 15 | , class IsApollo |
12 | 16 | , updateCacheJson |
13 | 17 | , updateCache |
@@ -45,13 +49,37 @@ type ApolloClientOptions = |
45 | 49 | , headers :: Array RequestHeader |
46 | 50 | } |
47 | 51 |
|
| 52 | +-- | As `ApolloClientOptions`, plus `operationTypeHeader`: when set, HTTP |
| 53 | +-- | requests include the GraphQL operation type ("query" or "mutation") as |
| 54 | +-- | the value of the header with this name, so eg. a load balancer can route |
| 55 | +-- | queries to a read replica. |
| 56 | +type ApolloClientOptions' = |
| 57 | + { url :: URL |
| 58 | + , authToken :: Maybe String |
| 59 | + , headers :: Array RequestHeader |
| 60 | + , operationTypeHeader :: Maybe String |
| 61 | + } |
| 62 | + |
48 | 63 | type ApolloSubClientOptions = |
49 | 64 | { url :: URL |
50 | 65 | , websocketUrl :: URL |
51 | 66 | , authToken :: Maybe String |
52 | 67 | , headers :: Array RequestHeader |
53 | 68 | } |
54 | 69 |
|
| 70 | +-- | As `ApolloSubClientOptions`, plus `operationTypeHeader`: when set, HTTP |
| 71 | +-- | requests include the GraphQL operation type ("query" or "mutation") as |
| 72 | +-- | the value of the header with this name, so eg. a load balancer can route |
| 73 | +-- | queries to a read replica. Subscriptions go over the websocket and are |
| 74 | +-- | unaffected (custom headers are not supported in websockets). |
| 75 | +type ApolloSubClientOptions' = |
| 76 | + { url :: URL |
| 77 | + , websocketUrl :: URL |
| 78 | + , authToken :: Maybe String |
| 79 | + , headers :: Array RequestHeader |
| 80 | + , operationTypeHeader :: Maybe String |
| 81 | + } |
| 82 | + |
55 | 83 | -- | Apollo client to make graphQL queries and mutations. |
56 | 84 | -- | From the @apollo/client npm module |
57 | 85 | foreign import data ApolloClient :: Type |
@@ -91,41 +119,60 @@ createClient |
91 | 119 | :: forall schema |
92 | 120 | . ApolloClientOptions |
93 | 121 | -> Effect (Client ApolloClient schema) |
94 | | -createClient = clientOptsToForeign >>> createClientImpl >>> map Client |
| 122 | +createClient { url, authToken, headers } = |
| 123 | + createClient' { url, authToken, headers, operationTypeHeader: Nothing } |
| 124 | + |
| 125 | +createClient' |
| 126 | + :: forall schema |
| 127 | + . ApolloClientOptions' |
| 128 | + -> Effect (Client ApolloClient schema) |
| 129 | +createClient' = clientOptsToForeign >>> createClientImpl >>> map Client |
95 | 130 |
|
96 | 131 | createSubscriptionClient |
97 | 132 | :: forall schema |
98 | 133 | . ApolloSubClientOptions |
99 | 134 | -> Effect (Client ApolloSubClient schema) |
100 | | -createSubscriptionClient = clientOptsToForeign >>> createSubscriptionClientImpl >>> map Client |
| 135 | +createSubscriptionClient { url, websocketUrl, authToken, headers } = |
| 136 | + createSubscriptionClient' { url, websocketUrl, authToken, headers, operationTypeHeader: Nothing } |
| 137 | + |
| 138 | +createSubscriptionClient' |
| 139 | + :: forall schema |
| 140 | + . ApolloSubClientOptions' |
| 141 | + -> Effect (Client ApolloSubClient schema) |
| 142 | +createSubscriptionClient' = clientOptsToForeign >>> createSubscriptionClientImpl >>> map Client |
101 | 143 |
|
102 | 144 | clientOptsToForeign |
103 | 145 | :: forall r |
104 | 146 | . { authToken :: Maybe String |
105 | 147 | , headers :: Array RequestHeader |
| 148 | + , operationTypeHeader :: Maybe String |
106 | 149 | | r |
107 | 150 | } |
108 | 151 | -> { authToken :: Nullable String |
109 | 152 | , headers :: Object String |
| 153 | + , operationTypeHeader :: Nullable String |
110 | 154 | | r |
111 | 155 | } |
112 | 156 | clientOptsToForeign opts = |
113 | 157 | opts |
114 | 158 | { authToken = toNullable opts.authToken |
115 | 159 | , headers = Object.fromFoldable $ opts.headers <#> \h -> Tuple (name h) (value h) |
| 160 | + , operationTypeHeader = toNullable opts.operationTypeHeader |
116 | 161 | } |
117 | 162 |
|
118 | 163 | type ApolloClientOptionsForeign = |
119 | 164 | { url :: URL |
120 | 165 | , authToken :: Nullable String |
121 | 166 | , headers :: Object String |
| 167 | + , operationTypeHeader :: Nullable String |
122 | 168 | } |
123 | 169 |
|
124 | 170 | type ApolloSubApolloClientOptionsForeign = |
125 | 171 | { url :: URL |
126 | 172 | , websocketUrl :: URL |
127 | 173 | , authToken :: Nullable String |
128 | 174 | , headers :: Object String |
| 175 | + , operationTypeHeader :: Nullable String |
129 | 176 | } |
130 | 177 |
|
131 | 178 | instance queryClient :: |
|
0 commit comments