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

Use custom link, cache and cache options #96

Merged
merged 4 commits into from Apr 19, 2018

Conversation

manueliglesias
Copy link
Contributor

@manueliglesias manueliglesias commented Apr 16, 2018

Use custom link, cache and cache options

Issue #, if available:
Fixes #3
Closes #21
Fixes #24
Closes #25
Fixes #36
Fixes #52
Closes #62
Fixes #70
Fixes #87
Fixes #90
Closes #92

Description of changes:
Allows the usage of:

  • Custom link
  • Custom cache
  • Custom cache options

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Allows the usage of:

- Custom link
- Custom cache
- Custom cache options

Fixes awslabs#3
Fixes awslabs#21
Fixes awslabs#24
Fixes awslabs#36
Fixes awslabs#52
Fixes awslabs#70
Fixes awslabs#87
Closes awslabs#62
@reggie3
Copy link

reggie3 commented Apr 17, 2018

@manueliglesias Can you give an example of how to add a custom cache using your PR?

@reggie3
Copy link

reggie3 commented Apr 18, 2018

Also, I tried to use the code from this pull request and received a error related to line 103 of client.js stating that super can't be used prior to this.
By the way, great work on adding these features. Thank you for working on it.

@manueliglesias
Copy link
Contributor Author

Hi @reggie3

This is how you would use a custom cache, please note that offline support needs to be disabled (the custom cache won't know how to write to the offline store):

import { Hermes } from 'apollo-cache-hermes';
import AWSAppSyncClient from "aws-appsync";

const cache = new Hermes({ verbose: true });

const client = new AWSAppSyncClient({
  url: appSyncConfig.graphqlEndpoint,
  region: appSyncConfig.region,
  auth: {
    type: appSyncConfig.authenticationType,
    apiKey: appSyncConfig.apiKey
  },
  disableOffline: true
}, { cache });

@manueliglesias
Copy link
Contributor Author

@reggie3 I also added a couple commits to address eslint rules (Hopefully that takes care of the webpack errors, let me know if it works for you)

Thanks for trying this SDK!

Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very nice, but lets talk tomorrow. Probably I didn't understand some stuff

@@ -217,7 +217,7 @@ var sign = function (request, access_info, service_info = null) {

// datetime string and date string
var dt = new Date(),
dt_str = dt.toISOString().replace(/[:\-]|\.\d{3}/g, ''),
dt_str = dt.toISOString().replace(/[:-]|\.\d{3}/g, ''),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is dt_str defined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is part of the same var statement

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lol thanks!

let theLink;

return new ApolloLink((op, forward) => {
if (!theLink) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you checking this theLink?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I want to create the link only once and re-use it on subsequent link invocations. For this I am creating a closure

} = {}, options = {}) {
const { cache: customCache, link: customLink } = options;

if (!customLink && (!url || !region || !auth)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add on the error message or customLink

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good!

});
let resolveClient;

const store = disableOffline ? null : createStore(() => this, () => resolveClient(this), conflictResolver);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to talk tomorrow the use of resolveClient

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure!

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manueliglesias I don't have reproducible steps yet, but I can sometimes trigger an error in my app around this new area of the appsync sdk code:
TypeError: resolveClient is not a function. (In 'resolveClient(_this)', 'resolveClient' is undefined)")
It seems to be thrown when I invoke a lambda outside the context of appsync, while the app is starting up. Sorry I don't have more actionable information yet.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When disableOffline=false, I applied this workaround to prevent the "resolveClient is not a function" error: apollographql/apollo-link#493 (comment)

let resolveClient;

const store = disableOffline ? null : createStore(() => this, () => resolveClient(this), conflictResolver);
const cache = disableOffline ? (customCache || new InMemoryCache(cacheOptions)) : new OfflineCache(store, cacheOptions);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happen if someone use customCache with cacheOptions

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case, cacheOptions would be ignored. It is inferred that your customCache is already initialized with whatever options you chose.

handle = passthrough(op, forward).subscribe(observer);
}).catch(observer.error);

return () => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this return will run before waiting hydratedPromise?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, an Observable "Returns a cleanup function which will cancel the event stream". For reference, see: https://github.com/tc39/proposal-observable#example-observing-keyboard-events

@reggie3
Copy link

reggie3 commented Apr 19, 2018

@manueliglesias Thank you for working on it. I'm really excited about incorporating your work into my project.
Also, I enjoyed reading the code review between you and @elorzafe . It was informative.

Copy link
Contributor

@elorzafe elorzafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 Nice!

@manueliglesias manueliglesias merged commit 8557e0a into awslabs:master Apr 19, 2018
@reggie3
Copy link

reggie3 commented Apr 20, 2018

@manueliglesias Would the correct combination of custom cache and link-state be a combination of your example above and the one from #24? Would it be something like this?

const cache = new InMemoryCache();

const defaultState = {
  AppState:{
    __typename: 'AppState',
    geolocation: 'null'
  }
};

const stateLink = createLinkWithCache(cache => withClientState({
  cache,
  defaults: defaultState
}));

const appSyncLink = new createAppSyncLink({
  url: AppSyncConfig.graphqlEndpoint,
  region: AppSyncConfig.region,
  auth: {
    type: AppSyncConfig.authType,
    apiKey: AppSyncConfig.apiKey
  },
  disableOffline: true
}, {cache});

const link = ApolloLink.from([
  stateLink,
  appSyncLink
]);

const client = new AWSAppSyncClient({}, { link });

This is what I'm trying to us now, but it isn't working yet. I'm trying to figure out if I'm on the right track.

EDIT: Just want to add, that auth includes a variable for type which most tutorials pull from your AppSync configuration file as "authType". The relevant key is actually called "authenticationType"in the JSON file that AppSync will export for you when building your GraphQL schema.

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

Successfully merging this pull request may close these issues.

None yet

4 participants