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.