Querying your Redux store with GraphQL

Using Apollo Client’s InMemoryCache and Local Resolvers, it’s possible to have it query your Redux store:

import store from "./Store";

const typeDefs = gql`
    type Query {
        users: [User]
    }

    type User {
        groups: [Group]
    }

    type Group {}
`;

const client = new ApolloClient({
    cache: new InMemoryCache(),
    typeDefs,
    resolvers: {
        Query: {
            users: () => store.getState().users,
        },
        User: {
            groups: user =>
                store
                    .getState()
                    .groups.filter(group => user.group_ids.includes(group.id)),
        },
    },
});

Neat “hack”. A shame the Local Resolvers are deprecated in Apollo 3.0 and will most likely be removed in a future version.

Querying your Redux store with GraphQL →
Apollo Client Local Resolvers →

Published by Bramus!

Bramus is a frontend web developer from Belgium, working as a Chrome Developer Relations Engineer at Google. From the moment he discovered view-source at the age of 14 (way back in 1997), he fell in love with the web and has been tinkering with it ever since (more …)

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.