#✅ - ✅ Subscription is not doing anything

7 messages · Page 1 of 1 (latest)

sinful aspen
#

Hi! I am using GraphQL subscriptions.

I have this table:

type UserViewState @model {
id: String! @primaryKey
userId: ID!
viewStateId: ID!
enabled: Boolean
}

And automatically amplify generated the following subscription:

export const onUpdateUserViewState = /* GraphQL */ subscription OnUpdateUserViewState( $filter: ModelSubscriptionUserViewStateFilterInput ) { onUpdateUserViewState(filter: $filter) { id userId viewStateId enabled createdAt updatedAt userViewStatesId __typename } };

The idea is that when I update something for a specific id on UserViewState that it gets detected by the subscription.
However, when I run the code below to initialized the subscription event and then I use do a mutation on the table using graphQL playground, the subscription doesn't return anything, either data or an error. What could possible be doing wrong?
For the sake of testing I am using a .js script.

Code (graph_test.js)

import { generateClient } from 'aws-amplify/api';
import * as subscriptions from './src/graphql/subscriptions.js';
import { Amplify } from "aws-amplify";
import amplifyconfig from './src/amplifyconfiguration.json' assert { type: 'json' };
Amplify.configure(amplifyconfig);
const client = generateClient();

export const onUpdateUserViewState = /* GraphQL */ subscription OnUpdateUserViewState( $filter: ModelSubscriptionUserViewStateFilterInput ) { onUpdateUserViewState(filter: $filter) { id userId viewStateId enabled createdAt updatedAt userViewStatesId __typename } };

const createSub = client
.graphql({ query: onUpdateUserViewState, variables: { id: '123123123211323'} })
.subscribe({ next: ({ data }) => console.log(data), error: (error) => console.warn(error) });

I am following this documentation: https://docs.amplify.aws/gen1/react/build-a-backend/graphqlapi/subscribe-data/

Thanks in advance!

#

hey there @azure wyvern Nice to meet you! I just saw that you have a similar thread, do you know what could be the issue here?

sinful aspen
#

UPDATE: It works perfectly inside the application, just doesn't work on a test.js file!

sinful aspen
#

✅ Subscription is not doing anything

light vine
#

Hi there, I got subscriptions working after a while - I don't know if this is the best way to do it - but it's working as expected. I am working in a React app. This is what mine looks like - I hope it helps you try some things perhaps:
useEffect(() => {
let counter = 0
// Need to stipulate the session is in progress otherwise this will keep triggering as athletes are entered into the competition
if (eventRightNow.sessionstatus === 'In progress') {
const updateSub = client.graphql({ query: onUpdateAthleteEntry, authMode: 'apiKey', filter: {sessionSessionEntriesId: {eq: eventRightNow.sessioninprogressdbid}} }).subscribe({
next: ({ provider, value }) => {
if (counter === 0) {
counter = counter++
liveScoreUpdate()
} else {
return () => updateSub.unsubscribe()
}
},
error: (error) => {
console.log(error)
}
})
return () => updateSub.unsubscribe()
} // Do nothing event is not in progress
},[])

Note: This is not a live app yet hence the auth on the API

light vine
#

Note: I realised I hadn't tested my subs after migrating from V5 to V6 - it returns one value 'data' now rather than 'provider' and 'value' - FYI

lofty peakBOT
#

✅ - ✅ Subscription is not doing anything