Consuming a mercure graphql subscription with react / apollo-client

Viewed 132

has anybody tried and succeeded connecting a react appollo-client component to a mercure graphql subscription in api-platform ? i’ve tried to create a splitLink (https://www.apollographql.com/docs/link/composition/) with a dedicated httpLink for mercure, but i can’t manage to connect cleanly. My mercure server is clean and i can successfully connect to a topic in browser. When i try to connect thru httpLink with an auth bearer, i receive a 401 from mercure. Maybe someone have an advice ?

I've tried this:

'''

const httpLink = new HttpLink({
  uri: 'https://localhost/graphql'
});

const mercureLink = new HttpLink({
  uri: 'https://localhost/.well-known/mercure',
  headers: {
    'Authorization': 'Bearer MyBearer'
  }
});

const splitLink = split(
  ({ query }) => {
    const definition = getMainDefinition(query);
    return (
      definition.kind === 'OperationDefinition' &&
      definition.operation === 'subscription'
    );
  },
  mercureLink,
  httpLink,
);

const client = new ApolloClient({
  link: ApolloLink.from([splitLink]),
  cache: new InMemoryCache()
});

function App() {
  return (
    <ApolloProvider client={client}>
      <Main />
    </ApolloProvider>
  );
}

'''

Thanks for any advice

0 Answers
Related