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

Need a query to initialize cache when using apollo-state-link #330

Open
sfegsetsfe opened this issue Jan 2, 2019 · 4 comments
Open

Need a query to initialize cache when using apollo-state-link #330

sfegsetsfe opened this issue Jan 2, 2019 · 4 comments
Labels
good first issue Used as a label for the community to tackle

Comments

@sfegsetsfe
Copy link

sfegsetsfe commented Jan 2, 2019

Follow instruction of this post
#24 (comment).

If we delete the client.mutate(...) part at the bottom, but do a console.log(client.cache.data.data), which supposed to hold the default value set in withClientState, it will be an empty object. But if we add back the client.mutate(...), the default value set will appear in the `console.log(client.cache.data.data)`` call.

Also, changing client.mutate(...) to

client.query(query: gql`
            query networkStatus @client{
                networkStatus {
                    isConnected
                }
            }
        `,
    })

to query the networkStatus field will result in error Network error: Cannot read property 'networkStatus' of null.

@elorzafe
Copy link
Contributor

elorzafe commented Jan 3, 2019

@sfegsetsfe how did you configured the client (link)?

@elorzafe elorzafe added feature-request Have an idea, share it with us! good first issue Used as a label for the community to tackle and removed feature-request Have an idea, share it with us! labels Jan 3, 2019
@sfegsetsfe
Copy link
Author

I used the exact config from @manueliglesias (except the client.mutation testing part ). Which was:

import { ApolloLink } from 'apollo-link';
import { withClientState } from 'apollo-link-state';
import AWSAppSyncClient, { createAppSyncLink, createLinkWithCache } from "aws-appsync";

const stateLink = createLinkWithCache(cache => withClientState({
  cache,
  resolvers: {
    Mutation: {
      updateNetworkStatus: (_, { isConnected }, { cache }) => {
        const data = {
          networkStatus: {
            __typename: 'NetworkStatus',
            isConnected
          },
        };
        cache.writeData({ data });
        return null
      },
    },
  },
  defaults: {
    networkStatus: {
      __typename: 'NetworkStatus',
      isConnected: false
    }
  }
}));

const appSyncLink = createAppSyncLink({
  url: appSyncConfig.graphqlEndpoint,
  region: appSyncConfig.region,
  auth: {
    type: appSyncConfig.authenticationType,
    apiKey: appSyncConfig.apiKey
  }
});

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

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

const result = await client.mutate({
  mutation: gql`mutation updateNetworkStatus($isConnected: Boolean) {
    updateNetworkStatus(isConnected: $isConnected) @client {
      isConnected
    }
  }`,
  variables: { isConnected: true }
});

console.log(result);

@usmansbk
Copy link

Is this ever going to be fixed?

@undefobj
Copy link
Contributor

@usmansbk we are still researching Apollo Link State, which at this time isn't fully supported in the SDK due to how cache can be manipulated out of the standard process. We have some ideas here and some prototype code but it will still take some time to fully support. In the meantime we recommend using the SDK without Apollo Link State.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Used as a label for the community to tackle
Projects
None yet
Development

No branches or pull requests

4 participants