我正在使用反應原生和中繼的Express Graphql服務器。我的設備連接到訂閱,但它不訂閱它。這裏的服務器中繼訂閱不能與反應原生
const subscriptionServer = SubscriptionServer.create(
{
execute,
subscribe,
schema,
onOperation: (message, params, webSocket) => {
console.log(params)
return params;
},
onConnect:() => {
// My device does connects
console.log("client connected")
}
},
{
server,
path: '/subscriptions'
},
);
app.use('/graphql', graphqlHTTP({
schema,
graphiql: true
}));
app.use('/graphiql', graphiqlExpress({
endpointURL: '/graphql',
subscriptionsEndpoint: `ws://127.0.0.1:8080/subscriptions`
}));
server.listen(PORT,()=> {
console.log("Groceries running on port " + PORT)
console.log(
`subscriptions is now running on ws://localhost:${PORT}/subscriptions'}`
);
});
服務器上訂閱解析器我index.js,這是很麻煩搞清楚,因爲每個人都使用可執行模式從apolloGraphql。
export default {
type: OrderEdges,
args: {
ShopId: {type: GraphQLID},
},
subscribe: withFilter(() => pubsub.asyncIterator('orderConfirmed'), (payload, variables) => {
console.log(payload)
console.log(variables)
return payload.orderConfirmed.node.ShopId == variables.ShopId;
}),
}
現在反應原生客戶端。我的訂閱設置與中繼環境。
const setupSubscriptions = (config, variables, cacheConfig, observer) => {
const query = config.text; //does console logs the query
const subscriptionClient = new SubscriptionClient(`ws://192.168.0.100:8080/subscriptions`, {reconnect:true});
subscriptionClient.request({query, variables}, (err, result) => {
console.log(err) // doesn't get call inside the request method
observer.onNext(data:result)
})
}
我的訂閱方法,
export default function() {
const variables = {
ShopId: shop.getShop()[0].id
}
requestSubscription(
environment,
{
subscription,
variables,
onCompleted: (res, err) => {
console.log(res)
console.log(err)
},
updater: (store) => {...},
onError: error => console.error(error),
onNext: (response) => {console.log(response)}
});
}
在那裏我打電話來訂購部件,
import subscription from '../../GraphQLQueries/subscriptions/orderConfirmed';
class OrdersBox extends React.Component {
constructor(props) {
super(props);
}
componentDidMount() {
//initializing subscription
orderSubscriptions();
}
當裝置啓動的應用程序,我的設備連接到網絡插座因爲我可以在SubscriptionServer中的onConnect方法內看到console.log語句。但是當有效載荷在變化之後發佈時,訂閱方法不會被調用。我似乎無法弄清楚我做錯了什麼。也許這是一些反應原生特定的配置,我錯過cuz一切似乎工作正常,當我在graphiql上測試它。 我無法找到與express graphql一起使用的react-native和relay訂閱的任何示例。
注意:當我使用與graphiql的訂閱時,一切正常。但與反應原生和中繼不兼容。
在此先感謝球員 ....
要退subscriptionClient.request({查詢,變量})或subscriptionClient.request({查詢,變量})。訂閱({.....}); –
const setupSubscriptions =(config,variables,cacheConfig,observer)=> {0} const query = config.text; const subscriptionClient = new SubscriptionClient('ws://192.168.10.3:8080/subscriptions',{reconnect:true}); 返回subscriptionClient.request({查詢,變量},(ERR,結果)=> { observer.onNext(數據:結果) }) } –
我認爲.subscribe現在被移除。你只需要返回subscriptionClient.request –