Error: Callback.apply not a function when using local resolvers with subscriptions

Hello.
Currently, I am working on using Apollo Client with an offline database from within an Expo Application. Using offline resolvers I was able to get it working for mutations but with subscriptions, I have been getting an error I can’t find much information on.

The error only occurs when I use the subscription hook, and is only displayed in the UI and not the console, in fact, any console logs I have placed in my resolvers don’t run so I think the resolver does not get triggered.

The error:
callback.apply is not a function. (In 'callback.apply(thisArg, args)', 'callback.apply' is undefined)

My Setup:
App.tsx:

// Initialize Apollo Client
const client = new ApolloClient({
  cache: new InMemoryCache(),
  // uri: "https://48p1r2roz4.sse.codesandbox.io",
  typeDefs: typeDefs,
  resolvers: resolvers,
});

...

return <ApolloProvider client={client}> .... </ApolloProvider> 

TypeDefs:

const typeDefs = `#graphql
    type User {
        username: String!
    }

    type Subscription {
        users: User!
    }
`;

Resolvers:

import { PubSub } from "graphql-subscriptions";
const pubsub = new PubSub();

const resolvers: any = {
  Query: {
    user: async (_: any, { username }: any) => {
      const user = await getUser(usernmae);
      return user;
    },
  },
  Subscription: {
    users: {
      subscribe: (_: any) => {
        console.log("Subscribe users");
        const users = observeUserList();
        //User const is a RXJS observable
        users.subscribe((user) => {
          console.log("USER HIT");
          console.log(user);
          pubsub.publish(`userList`, { user });
        });
        return pubsub.asyncIterator(`userList`);
      },
    },
  },
};

Usage:

import { gql, useSubscription } from "@apollo/client";

export default function Users() {
  const USERS_SUBSCRIPTION = gql`
    subscription Users {
      users @client {
        username
      }
    }
  `;

  const { loading, error, data } = useSubscription(USERS_SUBSCRIPTION);
  const [users, setUsers] = useState([] as any[]);

  useEffect(() => {
    if (data && data.users) {
      setUsers((prevUsers) => [...prevUsers, data.users]);
    }
  }, [data]);

  if (loading) {
    return <Text>Loading...</Text>;
  }

  if (error) {
    return <Text>Error: {error.message}</Text>;
  }

  return <Text>Data: {data}</Text>;
}

I am aware that offline resolvers are being deprecated soon, but I want to keep my local resolvers in sync with my remote ones on other graphQL servers easily as a import of the same resolvers/schema file.

Thanks for the help.

Hi @Jahson :wave: I haven’t used local resolvers with subscriptions so I apologize if this isn’t helpful but I’ll give it a try:

I’m looking at the following unit test in the Apollo Client code:

The syntax used there is different from the snippet you shared. I believe the users.subscribe function is not being reached because there is no subscribe field in your User type. But our docs and unit tests don’t cover this functionality very well so I can’t say this with confidence. I hope that helps!