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>
);
};
import { ApolloClient, InMemoryCache, HttpLink, gql } from '@apollo/client';
const query = gql`
query CurrentAuthorPostCount($authorId: Int!) {
currentAuthor @client {
name
authorId @export(as: "authorId")
}
postCount(authorId: $authorId)
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:4000/graphql' }),
cache,
resolvers: {},
});
cache.writeQuery({
query: gql`
query GetCurrentAuthor {
currentAuthor {
name
authorId
}
}
`,
data: {
currentAuthor: {
__typename: 'Author',
name: 'John Smith',
authorId: 12345,
},
},
});
// ... run the query using client.query, the <Query /> component, etc.
import { ApolloClient, InMemoryCache } from "@apollo/client";
const client = new ApolloClient({
cache: new InMemoryCache(),
uri: "/graphql"
});
client.query({
query: MY_QUERY,
context: {
// example of setting the headers with context per operation
headers: {
special: "Special header value"
}
}
});
import { ApolloClient, InMemoryCache, HttpLink, gql } from '@apollo/client';
const query = gql`
query CurrentAuthorPostCount($authorId: Int!) {
currentAuthorId @client @export(as: "authorId")
postCount(authorId: $authorId)
}
`;
const cache = new InMemoryCache();
const client = new ApolloClient({
link: new HttpLink({ uri: 'http://localhost:4000/graphql' }),
cache,
resolvers: {},
});
cache.writeQuery({
query: gql`query GetCurrentAuthorId { currentAuthorId }`,
data: {
currentAuthorId: 12345,
},
});
// ... run the query using client.query, the <Query /> component, etc.
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.