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,
},
});
},
},
},
};
import { ApolloClient, InMemoryCache } from '@apollo/client';
const cache = new InMemoryCache();
const client = new ApolloClient({
cache,
resolvers: { /* ... */ },
});
cache.writeQuery({
query: gql`
query GetTodosNetworkStatusAndFilter {
todos
visibilityFilter
networkStatus {
isConnected
}
}
`,
data: {
todos: [],
visibilityFilter: 'SHOW_ALL',
networkStatus: {
__typename: 'NetworkStatus',
isConnected: false,
},
},
});
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.
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.