// The `client` variable refers to your `ApolloClient` instance.
// It would be imported in your template,
// or passed via props thanks to `withApollo` in React for example.
Meteor.logout(function() {
return client.resetStore(); // make all active queries re-run when the log-out process completed
});
import React from "react";
import { ApolloConsumer, useApolloClient, useQuery, gql } from "@apollo/client";
const GET_MESSAGE_COUNT = gql`
query GetMessageCount {
messageCount @client {
total
}
}
`;
const resolvers = {
Query: {
messageCount: (_, args, { cache }) => {
// ... calculate and return the number of messages in
// the cache ...
return {
total: 123,
__typename: "MessageCount",
};
},
},
};
export function MessageCount() {
const client = useApolloClient();
client.addResolvers(resolvers);
const { loading, data: { messageCount } } = useQuery(GET_MESSAGE_COUNT);
if (loading) return "Loading ...";
return (
<p>
Total number of messages: {messageCount.total}
</p>
);
};
const client = new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message, locations, path, extensions }) => {
if(extensions.code === 'UNAUTHENTICATED') {
localStorage.removeItem('jwt');
client.resetStore();
}
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
});
if (networkError) {
console.log(`[Network error]: ${networkError}`);
}
}
}),
AuthLink,
createUploadLink({
uri: 'http://localhost:8000/graphql',
credentials: 'same-origin',
}),
]),
cache: new InMemoryCache().restore(window.__APOLLO_STATE__)
});
const client = new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors) {
graphQLErrors.map(({ message, locations, path, extensions }) => {
if(extensions.code === 'UNAUTHENTICATED') {
localStorage.removeItem('jwt');
client.resetStore();
}
console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`);
});
if (networkError) {
console.log(`[Network error]: ${networkError}`);
}
}
}),
InfoLink,
AuthLink,
link
]),
cache: new InMemoryCache().restore(window.__APOLLO_STATE__)
});
import { ApolloClient, InMemoryCache, HttpLink, gql } from '@apollo/client';
const query = gql`
query CurrentAuthorPostCount($authorId: Int!) {
currentAuthorId @client @export(as: "authorId")
postCount(authorId: $authorId) @client
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
resolvers: {
Query: {
postCount(_, { authorId }) {
return authorId === 12345 ? 100 : 0;
},
},
},
});
cache.writeQuery({
query: gql`{ currentAuthorId }`,
data: {
currentAuthorId: 12345,
},
});
// ... run the query using client.query, the <Query /> component, etc.