import { ApolloClient, InMemoryCache, HttpLink, gql } from '@apollo/client';
const GET_CART_ITEMS = gql`
query GetCartItems {
cartItems @client
}
`;
const cache = new InMemoryCache();
cache.writeQuery({
query: GET_CART_ITEMS,
data: {
cartItems: [],
},
});
const client = new ApolloClient({
cache,
link: new HttpLink({
uri: 'http://localhost:4000/graphql',
}),
resolvers: {
Launch: {
isInCart: (launch, _args, { cache }) => {
const { cartItems } = cache.readQuery({ query: GET_CART_ITEMS });
return cartItems.includes(launch.id);
},
},
},
});
const GET_LAUNCH_DETAILS = gql`
query LaunchDetails($launchId: ID!) {
launch(id: $launchId) {
isInCart @client
site
rocket {
type
}
}
}
`;
// ... run the query using client.query, a <Query /> component, etc.
import { InMemoryCache } from '@apollo/client';
const cache = new InMemoryCache();
cache.writeQuery({
query: gql`query MyQuery {
isLoggedIn,
cartItems
}`,
data: {
isLoggedIn: !!localStorage.getItem('token'),
cartItems: [],
},
});
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
cache: new InMemoryCache({
typePolicies: {
Query: {
fields: {
book: {
read(_, { args, toReference }) {
return toReference({
__typename: 'Book',
id: args.id,
});
}
}
}
}
}
})
});
import { ApolloClient, InMemoryCache, gql } from '@apollo/client';
let nextTodoId = 0;
const cache = new InMemoryCache();
cache.writeQuery({
query: gql`query GetTodos { todos { ... } }`,
data: { todos: [] },
});
const client = new ApolloClient({
resolvers: {
Mutation: {
addTodo: (_, { text }, { cache }) => {
const query = gql`
query GetTodos {
todos @client {
id
text
completed
}
}
`;
const previous = cache.readQuery({ query });
const newTodo = { id: nextTodoId++, text, completed: false, __typename: 'TodoItem' };
const data = {
todos: [...previous.todos, newTodo],
};
cache.writeQuery({ query, data });
return newTodo;
},
},
},
});
import { ApolloClient, InMemoryCache } from '@apollo/client';
const client = new ApolloClient({
cache: new InMemoryCache(),
resolvers: {
Mutation: {
updateVisibilityFilter: (_, { visibilityFilter }, { cache }) => {
cache.writeQuery({
query: gql`query GetVisibilityFilter { visibilityFilter }`,
data: {
__typename: 'Filter',
visibilityFilter,
},
});
},
},
},
};