Hello everyone.
I’m trying to use https://github.com/awslabs/aws-mobile-appsync-sdk-js.
I used the bindings from idkjs (https://reasonml.chat/t/writing-bindings/598/8) for the client and it works.
But I need to add a auth header to all the requests to add a JWT. This token needs to be passed through Appsync to the REST API used in some of the resolvers.
I’m trying to use a customLink instead of the normal client : https://github.com/awslabs/aws-mobile-appsync-sdk-js/blob/master/packages/aws-appsync/src/client.ts#L225
This is what I have so far.
Client.re
[@bs.module]
external introspectionQueryResultData: Js.t({…}) =
"…/…/fragmentTypes.json";let fragmentMatcher =
ApolloInMemoryCache.createIntrospectionFragmentMatcher(
~data=introspectionQueryResultData,
);let cache = ApolloInMemoryCache.createInMemoryCache(~fragmentMatcher);
let httpLink =
ApolloLinks.createHttpLink(
~uri=
“https://apiurl”,
(),
);let authLink = {
let authHandler = () => {
let token = “jwt”;
{
“headers”: {
“Authorization”: {j|Bearer $token|j},
},
};
};ApolloLinks.createContextLink(authHandler);
};let apolloLinks = ApolloLinks.from([|authLink, httpLink|]);
AppSyncClient.re
module AppSyncConfig = {
type config = {
.
“graphqlEndpoint”: string,
“region”: string,
“authenticationType”: string,
“apiKey”: string,
};
let config = {
“graphqlEndpoint”: “url”,
“region”: “eu-west-1”,
“authenticationType”: “API_KEY”,
“apiKey”: “key”,
};
};module AppSyncClient = {
[@bs.module “aws-appsync”] [@bs.new]
external makeAWSAppSyncClient:
(
{
.
“url”: Js.Nullable.t(string),
“region”: Js.Nullable.t(string),
“auth”:
Js.Nullable.t({
.
“type”: string,
“apiKey”: string,
}),
},
{. “link”: Js.Nullable.t(unit => 'a)}
) =>
'a =
“AWSAppSyncClient”;let createAWSAppSyncClient = (~url=?, ~region=?, ~auth=?, ~link=?, ()) => {
let appSyncClientOptions = {
“url”: Js.Nullable.fromOption(url),
“region”: Js.Nullable.fromOption(region),
“auth”: Js.Nullable.fromOption(auth),
};makeAWSAppSyncClient( appSyncClientOptions, {"link": Js.Nullable.fromOption(link)}, );
};
};module AppSyncLink = {
[@bs.module “aws-appsync”] [@bs.new]
external makeAWSAppSyncLink:
{
.
“url”: Js.Nullable.t(string),
“region”: Js.Nullable.t(string),
“auth”:
Js.Nullable.t({
.
“type”: string,
“apiKey”: string,
}),
“resultsFetcherLink”: Js.Nullable.t(ReasonApolloTypes.apolloLink),
} =>
'b =
“createAppSyncLink”;let createAWSAppSyncLink =
(~url=?, ~region=?, ~auth=?, ~resultsFetcherLink=?, ()) => {
let appSyncLinkOptions = {
“url”: Js.Nullable.fromOption(url),
“region”: Js.Nullable.fromOption(region),
“auth”: Js.Nullable.fromOption(auth),
“resultsFetcherLink”: Js.Nullable.fromOption(resultsFetcherLink),
};
makeAWSAppSyncLink(appSyncLinkOptions);
};
};
let awsLink =
AppSyncLink.createAWSAppSyncLink(
~url=AppSyncConfig.config##graphqlEndpoint,
~region=AppSyncConfig.config##region,
~auth={
“type”: AppSyncConfig.config##authenticationType,
“apiKey”: AppSyncConfig.config##apiKey,
},
~resultsFetcherLink=Client.apolloLinks,
);
let client =
AppSyncClient.createAWSAppSyncClient(
~url=AppSyncConfig.config##graphqlEndpoint,
~region=AppSyncConfig.config##region,
~auth={
“type”: AppSyncConfig.config##authenticationType,
“apiKey”: AppSyncConfig.config##apiKey,
},
// ~link=awsLink,
(),
);
And :
ReactDOMRe.renderToElementWithId(
<ReasonApollo.Provider client=AppSyncClient.client>
</ReasonApollo.Provider>,
“app”,
);
In these bindings I’m troubled with :
{. “link”: Js.Nullable.t(unit => 'a)}
Is this type normal? I it the result of calling createAppsyncLink so Im not sure the return type should be this?
If I comment out the ~link in the “let client” it works.
I get a response from the API saying 401 unauthaurized.
If I uncomment ~link, I dont get a response anymore and get an error in the js console:
Uncaught (in promise) TypeError: Cannot read property ‘_subscription’ of undefined
at error (Observable.js:224)
Im just tryin a simple query (no subscriptions) in the app.
Does anyone use Appsync with Reason and use customLink to create a client?
Maybe the error is from the API but I would like to be sure the client is good.
Thanks for your help.