Empty Result for query with GraphQL


#1

hi, I always get a empty result with following code:

const credentials = Credentials.jwt(this.$store.state.authToken)
    User.authenticate(credentials, 'https://samay.de1a.cloud.realm.io').then(user => {
      GraphQLConfig.create(user, '/samay', null, true).then(config => {
        const httpLink = concat(config.authLink, new HttpLink({ uri: config.httpEndpoint }))
        const webSocketLink = new WebSocketLink({
          uri: config.webSocketEndpoint,
          options: {
            connectionParams: config.connectionParams
          }
        })
        // link
        const link = split(
          ({ query }) => {
            const { kind, operation } = getMainDefinition(query)
            return kind === 'OperationDefinition' && operation === 'subscription'
          },
          webSocketLink,
          httpLink
        )
        // create client
        const client = new ApolloClient({
          link: link,
          cache: new InMemoryCache()
        })
        const query = gql`
        query {
          teams {
            id
            name
          }
        }
        `
        client.query({
          query: query
        }).then(function (result) {
          console.log(result.data)
        }).catch(({ networkError, graphQLErrors }) => {
          console.log('graphQLErrors', graphQLErrors)
          console.log('networkError', networkError)
        })
      })
    })

If I check with the explorer I can see the results:
https://samay.de1a.cloud.realm.io/graphql/explore/%2Fsamay

Has someone and idea what is wrong?

thank you


#2

It appears you’re trying to use query-based sync. You can read more about it in the docs but essentially, your Realm will be empty unless you specifically ask for data. So in your case, you’ll need something like:

mutation {
  createTeamSubscription {
    id
    name
  }
}

This will synchronize all team objects into your partial view. You only need to do that once - once a subscription is created, the Realm sync engine will keep the reference Realm and the partial view in sync with each other. You can see all existing subscriptions by querying queryBasedSubscriptions and you can unsubscribe by issuing the following mutation deleteQueryBasedSubscription(subscriptionName). You can read a bit more in the release notes.


#3

perfect thank you! it works on the realm /samay

but if i try to connect to a different realm i get following CORS error:

Access to fetch at ‘https://samay.de1a.cloud.realm.io/graphql/%2Fsamay%2F909AF16C-F7BC-4FDC-AE62-CA6E100BB6B9%2F__partial%2FB3897412-4976-4821-9634-97AE078B5D68%2Fgraphql-client’ from origin ‘http://localhost:8080’ has been blocked by CORS policy: No ‘Access-Control-Allow-Origin’ header is present on the requested resource. If an opaque response serves your needs, set the request’s mode to ‘no-cors’ to fetch the resource with CORS disabled.


#4

This is quite surprising. My guess is that the CORS error masks another exception that occurs on the server. Can you try executing the query from a node-js client (or any other client that is not subject to CORS policies)?


#5

I dont know what you mean with other client that is not subject to CORS policies?

I just saw that the Problem is quite the same like this post here:
I also get the 502 Bad Gateway error


#6

If you haven’t already please open a support ticket for that 502 error. A number of others are getting it and the Realm team is having a difficult time reproducing the issue.