GraphQL [Apollo-Android]: two `.watcher().enqueueAndWatch` creating infinite refresh loop - kotlin

I'm requesting a list of 'posts' from my server using enqueueAndWatch and it is causing an infinite refresh loop.
The two queries are below:
query GetOrganizationPosts {
user {
organization {
posts {
...post
}
}
}
}
query GetUserPosts {
user {
posts {
...post
}
}
}
fragment post on Post {
id
title
messsage
}
The view model is initialized and the infinite loop occurs when I call refreshPosts() from the posts fragment:
init {
viewModelScope.launch {
networkClient.apolloClient.
query(GetOrganizationPostsQuery())
.watcher().enqueueAndWatch(callback)
networkClient.apolloClient
.query(GetUserPostsQuery())
.watcher().enqueueAndWatch(callback2)
}
}
fun refreshPosts() {
networkClient.apolloClient
.query(GetOrganizationPostsQuery())
.enqueue(null)
networkClient.apolloClient
.query(GetUserPostsQuery())
.enqueue(null)
}
Thanks in advance!

Workaround as described in Apollo-Android pull request: https://github.com/apollographql/apollo-android/issues/2226
Add .responseFetcher(ApolloResponseFetchers.CACHE_ONLY) to any enqueueAndWatch calls. This makes sure watchers don't perform network requests when the cache is updated.

Related

BigCommerce Stencil - GraphQL query using front matter not returning anything

I'm not sure if it's a bug, but I'm not able to make GraphQL work in the Cornerstone template. I'm expecting an error or something getting returned at least, but nothing is being rendered at all from gql.
I am on the pages/product.html template, and I even tried this example from the docs:
---
product:
videos:
limit: {{theme_settings.productpage_videos_count}}
reviews:
limit: {{theme_settings.productpage_reviews_count}}
related_products:
limit: {{theme_settings.productpage_related_products_count}}
similar_by_views:
limit: {{theme_settings.productpage_similar_by_views_count}}
gql: "query productById($productId: Int!) {
site {
product(entityId: $productId) {
variants(first: 25) {
edges {
node {
sku
defaultImage {
url(width: 1000)
}
}
}
}
}
}
}
"
My goal is to have access to the paths/URL on each of the product's category because product.category is just an array of category names. Here's the query I am able to make work on the GraphQL playground (86 to be replaced by $productId in the front matter GraphQL query, I think?):
query getProductCategories {
site {
product(entityId: 86) {
categories {
edges {
node {
name
path
}
}
}
}
}
}
If there's no way around this, maybe I'll just try to do the fetching in the client side.
This now works correctly, as of 20-Sep-2021.
There was a bug, tracked as an issue here: https://github.com/bigcommerce/stencil-cli/issues/732 which has been resolved and closed.

How to fetch all repositories with specific topic, using GitHub GraphQL API?

I am trying to fetch all my repositories that contain topic "portfolio" using Github GraphQL API.
For now, I found only how to fetch all repos on github with a specific topic, like so :
{
search(type: REPOSITORY, query: "topic: portfolio", last: 50) {
repos: edges {
repo: node {
... on Repository {
url
}
}
}
}
}
And I know how to fetch all repos for a specific user, like so:
{
user(login: "VitFL") {
repositories(last: 50) {
repos: nodes {
url
}
}
}
}
But I have no idea how to combine those queries, so I could receive all repos with topic "portfolio" for a specific user.
Does anyone know how to achieve this result?
Using search(), a user argument can be added to the query value.
Also, the whitespace after the colon should be removed, i.e. topic: portfolio -> topic:portfolio. You will notice far more results with this change.
{
search(type: REPOSITORY, query: "user:VitFL topic:portfolio", last: 50) {
repos: edges {
repo: node {
... on Repository {
url
}
}
}
}
}

How to run a function continuously in RN

I am trying to create a sound metering app with React Native, but it seems that if I start the metering, which I have as a infinite loop until the user stops it, it hangs the app.
Basically the user taps a button, invokes startMonitor(), then the runMonitor starts and checkSound() does a check for the current decibel level.
The relevant code section here
startMonitor() {
KeepAwake.activate();
this.setState({ monitorStatus: true
}, this.runMonitor );
}
runMonitor() {
while(this.state.monitorStatus) {
if(this.state.beginWait) {
this.wait(5000);
}
if(!this.state.isTransmitting) {
this.checkSound();
}
}
}
Any ideas on how to run a function continuously/infinite until the user cancels it?
You can use setInterval:
startMonitor() {
this.runMonitorIV = setInterval(runMonitor, 1000);
}
runMonitor() {
while(this.state.monitorStatus) {
if(this.state.beginWait) {
this.wait(5000);
}
if(!this.state.isTransmitting) {
this.checkSound();
}
}
}
//called on user event
stopMonitor() {
clearInterval(this.runMonitorIV);
}
(Not sure about your context or scope, but that's the basic idea)
There's also the TimerMixin that clears those timers triggers when a component unmounts.

Graphql query only not null objects

im trying to perform a query like this:
{
people{
pet{
name
}
}
}
result:
{
"people": {
"pet": null
}
},
{
"people": {
"pet": {
name: "steve"
}
}
}
What i want is to get only people that contains a pet, is there any way to achieve this not coding on my resolver ?
Actually, it is possible with the filter: { pet: {ne: null} } filtering:
query allPeople(filter: { people: { pet: {ne: null} } }) {
people {
pet
}
}
This is not possible the way you describe. GraphQL will call resolve functions to fetch the data. If you don't want certain data in your response, you have to filter it somewhere on the server. The only thing you have control over is the query, the schema and the resolve functions.
There is no way to express your requirement purely in the query. If you put it in the schema, you would no longer be able to query for people without pets. So the only way to do it is to write it in your resolve function. You could for example add a boolean argument called hasPet to your people field, and do this in the resolver:
people(root, { hasPet }){
// get allPeople
if (typeof hasPet === 'undefined'){
return allPeople
}
return allPeople.filter((person) => person.hasPet() === hasPet)
}
The unfortunate thing is that this will require you to 'look ahead' and figure out if a person has a pet in the first place, but if you cache backend or DB requests with something like DataLoader, this isn't actually costly, because you would have to fetch the pet anyway. This way you just fetch it a bit earlier.
If you're fetching your people from a database, it would of course make sense to already filter them there, and not in the resolve function.

Vows: Testing Asynchronous Interleaving

Is there a methodology to test (potential) interleaving of asynchronous functions with vows?
For example:
// Topic portion
var user = new User('jacob')
user.set('email,'foo#bar.com')
user.save() // a
user.set('email',derp#cherp.com')
user.save() // b
user.refresh(this.callback) // Reload from database
// Callback
assert.equals(user.email,'derp#cherp.com')
There is could be a race condition between the two saves. When writing my tests I want to ensure that my API is ensuring that b finishes last (and that we have the correct final value for the email). With the way that's it written, the test will pass coincidentally some of the time.
Heres the example from the vows docs:
The nested contexts act as nested callbacks and pass the return arguments to the next context.
Docs: http://vowsjs.org/
{ topic: function () {
fs.stat('~/FILE', this.callback);
},
'after a successful `fs.stat`': {
topic: function (stat) {
fs.open('~/FILE', "r", stat.mode, this.callback);
},
'after a successful `fs.open`': {
topic: function (fd, stat) {
fs.read(fd, stat.size, 0, "utf8", this.callback);
},
'we can `fs.read` to get the file contents': function (data) {
assert.isString (data);
}
}
}
}