Closed MedAzizHaddad closed 4 months ago
While writing this issue ticket, I figured it out... The subscriptions configuration (wsLink ..) should be applied in ApolloWrapper.js
rather than ApolloClient.js
, as they are being used from a client-side component. (I can't believe that I missed that and I wasted like 5 hours trying to "debug" this error ...)
I’ve decided to submit this ticket and document the solution before closing it, in case it proves helpful to someone in the future. (This is my first time submitting an issue ticket, so I’m not entirely sure if this is ok)
so the ApolloWrapper.js
should look like this
"use client";
import {ApolloLink, HttpLink, split} from "@apollo/client";
import {
ApolloNextAppProvider,
NextSSRInMemoryCache,
NextSSRApolloClient,
SSRMultipartLink,
} from "@apollo/experimental-nextjs-app-support/ssr";
import React from "react";
import {GraphQLWsLink} from "@apollo/client/link/subscriptions";
import {createClient} from "graphql-ws";
import {getMainDefinition} from "@apollo/client/utilities";
function makeClient() {
const httpLink = new HttpLink({
uri: "http://localhost:8000/graphql",
});
const wsLink = new GraphQLWsLink(createClient({
url: 'ws://localhost:8000/graphql',
connectionParams: {
authToken: "Bearer I_AM_A_VALID_AUTH_TOKEN",
},
}));
const splitLink = split(
({ query }) => {
const definition = getMainDefinition(query);
return (
definition.kind === 'OperationDefinition' &&
definition.operation === 'subscription'
);
},
wsLink,
httpLink,
);
return new NextSSRApolloClient({
cache: new NextSSRInMemoryCache(),
link:
typeof window === "undefined"
? ApolloLink.from([
new SSRMultipartLink({
stripDefer: true,
}),
splitLink,
])
: splitLink,
});
}
export function ApolloWrapper({ children }) {
return (
<ApolloNextAppProvider makeClient={makeClient}>
{children}
</ApolloNextAppProvider>
);
}
Do you have any feedback for the maintainers? Please tell us by taking a one-minute survey. Your responses will help us understand Apollo Client usage and allow us to serve you better.
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. For general questions, we recommend using StackOverflow or our discord server.
Environment
Description
I'm experiencing a
TypeError
when attempting to use a Strawberry GraphQL subscription from my Next.js frontend using Apollo Client. The subscription functions correctly when tested via the GraphQL interface, but it fails when invoked from the frontend, resulting in anull
value for a non-nullable field.FastAPI backend Code
Strawberry GraphQL Endpoint Configuration
Next.js Frontend Code
Why am I thinking the bug is in frontend ? :
Because when I execute the following subscription query via the backend GraphQL interface
The expected result is received:
When invoking the same subscription from the frontend, the following error is encountered (backend console)