如何在节点应用

时间:2018-11-08 19:55:20

标签: typescript rxjs graphql prisma async-iterator

我正在关注this guide。我试图在我的节点应用程序中收听graphQL订阅。我在执行该订阅时遇到很多麻烦。我尝试了几种不同的方法,下面列出了这些方法,但没有一种起作用。

// converted to rxjs observables
from(PrismaClient.$subscribe.priority({
    mutation_in: "CREATED"
})).subscribe(
    (next) => {
        console.log("next", next);
    },
    () => {
        console.log("error");
    },
    () => {
        console.log("complete");
    }
);

// as promise
PrismaClient.$subscribe.priority({
    mutation_in: "CREATED"
}).then(
    (next) => {
        console.log("next", next);
    }
);

这两个都在下一个中返回:

{ next: [Function: next],
  return: [Function: return],
  throw: [Function: throw],
  '@@asyncIterator': [Function] }

我希望它会在每次创建新priority时返回创建的Promise<AsyncIterator<...>>。但是,它只返回一次,并立即完成并返回上面列出的对象。

我知道这与返回prisma-client有关,但是我不确定如何解决此问题。

如何使用rxjs在节点应用中创建pyramida graphql订阅?我也想将其转换为promises而不是使用Observables。我只喜欢ProcessBuilder gradlewProcessBuilder = new ProcessBuilder(mainDirPath.concat("\\android\\gradlew.bat"), "assembleDebug"); gradlewProcessBuilder.directory(new File(mainDirPath.concat("/android"))); gradlewProcessBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT); //This is the line Process gradlewProcess = gradlewProcessBuilder.start(); gradlewProcess.waitFor();

1 个答案:

答案 0 :(得分:1)

以下是关于Promises和async / await的建议:

const main = async () => {
  const subscription = await PrismaClient.$subscribe.priority({
    mutation_in: 'CREATED',
  })
  handleSubscription(subscription)
}

const handleSubscription = async (subscription) => {
  const result = await subscription.next()
  console.log('do stuff with', result.value)

  if (!result.done) {
    console.log('handle again')
    handleSubscription(subscription)
  }
  else {
    console.log('done')
  }
}

main().catch(e => console.error(e))