-
Notifications
You must be signed in to change notification settings - Fork 2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Subscriptions using built-in SubscriptionServer error: client gets "Cannot read property 'headers' of undefined" #1537
Comments
Code showing import ApolloClient from 'apollo-client'
import { ApolloLink } from 'apollo-link'
import { MeteorAccountsLink } from 'meteor/apollo'
import { HttpLink } from 'apollo-link-http'
import { WebSocketLink } from 'apollo-link-ws'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { Meteor } from "meteor/meteor"
const apolloClient = new ApolloClient({
link: ApolloLink.split( // split based on operation type
({ query }) => {
import { getMainDefinition } from 'apollo-utilities'
const { kind, operation } = getMainDefinition(query)
return kind === 'OperationDefinition' && operation === 'subscription'
},
new WebSocketLink({
uri: `${Meteor.absoluteUrl("/subscriptions").replace("http", "ws")}`,
options: { reconnect: true }
}),
ApolloLink.from([
new MeteorAccountsLink(),
new HttpLink({ uri: '/graphql' })
]),
),
cache: new InMemoryCache()
}) |
Both |
hey @tab00 had the same exception, you could start looking here. I didn't have any headers on the websockets (subscriptions): context: async ({ req }) => ({ user: await getUser(req.headers.authorization) }) async context(val) {
console.log(val && val.req)
} |
This problem still exists. Please fix it. |
Should you use |
I still get this problem. Has anyone tried to fix this? |
@almostprogrammer is correct. I structured my ApolloServer code below and it works. for subscription, you don't use req. u need to use connection
|
@myhendry, why isn't there |
They are all in my resolvers file https://github.com/myhendry/template/blob/master/server/graphql/resolvers.js |
It looks like returning Though I still needed to add subscriptions: {
path: "/subscriptions" in the So my code is now: import { ApolloServer } from 'apollo-server-express'
import { typeDefs } from './schema.js'
import { resolvers } from './resolvers.js'
import { DSBooks } from "./DSBooks.js"
import { getUser } from 'meteor/apollo'
const server = new ApolloServer({
typeDefs,
resolvers,
dataSources: () => ({
dsBooks: new DSBooks()
}),
context: async ({ req, connection }) => {
if (connection) {
return {
...connection.context
};
}
uploads: false,
subscriptions: {
path: "/subscriptions",
onConnect: async (connectionParams, webSocket, context) => {
console.log(`Subscription client connected using Apollo server's built-in SubscriptionServer.`)
},
onDisconnect: async (webSocket, context) => {
console.log(`Subscription client disconnected.`)
}
}
})
import { WebApp } from 'meteor/webapp'
server.applyMiddleware({ app: WebApp.connectHandlers }) //path option defaults to /graphql
WebApp.connectHandlers.use('/graphql', (req, res) => { if (req.method === 'GET') res.end() }) // To prevent server-side exception "Can't set headers after they are sent" because GraphQL Playground (accessible in browser via /graphql) sets headers and WebApp also sets headers
server.installSubscriptionHandlers(WebApp.httpServer) |
I am not using meteor. I am using node js. I haven't tried using meteor
with graphql
…On Sat, Feb 9, 2019, 10:04 PM tab00 ***@***.***> wrote:
Closed #1537 <#1537>.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1537 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ATr8G64YYZCcvQmeVkO5UNeYo2rGU75eks5vLtWFgaJpZM4V-Pgx>
.
|
@helfer the problem here is that SubscriptionServer calls |
@helfer to make matters worse, https://github.com/apollographql/apollo-server/blob/master/packages/apollo-server-core/src/ApolloServer.ts#L480 overwrites the context returned by This is a major issue. |
I found a solution Context must be used with connection.context check here. |
Thanks for reporting this issue originally! I'm going to close this issue since it hasn't received a lot of traction and could have been resolved already. If this is still a problem, would someone who's experiencing the problem (or anyone who comes across this issue and is able to assist) mind building a reproduction of the problem in to a runnable CodeSandbox reproduction using the latest version of Apollo Server and sharing the link to that CodeSandbox in this issue? I'm happy to re-open if this is still occurring and someone can provide a reproduction. Thanks again! |
This is still an issue for most people who setup auth with subscriptions. This is a running sandbox showing the problem: https://codesandbox.io/s/apollo-server-qthfh?fontsize=14 |
@StefanFeederle that seems like the wrong issue number, mind checking it? |
@jedwards1211 Thanks, fixed it |
Is this dead? I'm having issues with receiving the connection param from context. I'm using websockets for auth If that's relevent. |
Apollo Server no longer has a built-in SubscriptionServer. |
The server sees client connecting successfully, but the client gets the error, even if nothing is published from server.
Meteor's WebApp is the HTTP server.
If I start a new
SubscriptionServer
instead, there's no problem, and the client receives subscription data.The text was updated successfully, but these errors were encountered: