首页 > 解决方案 > 如何在没有 relay-babel-plugin 和 relay-compiler 的情况下获取 GraphhQLTaggedNode

问题描述

在中继中,用于将字符串转换为 GraphQLTaggedNode 的方法声明如下:

function graphql(strings: Array<string>): GraphQLTaggedNode {
  invariant(
    false,
    'graphql: Unexpected invocation at runtime. Either the Babel transform ' +
      'was not set up, or it failed to identify this call site. Make sure it ' +
      'is being used verbatim as `graphql`. Note also that there cannot be ' +
      'a space between graphql and the backtick that follows.',
  );
}

Relay是这样设计的,强制开发者使用“babel-plugin-relay”和“replay-compiler”,“relay-compiler”将字符串转换为GraphQLTaggedNode并将节点保存到生成的文件中,用“babel-plugin-relay”替换通过引用该节点来调用“graphql()”。这在正常情况下效果很好。

现在,我正在开发一个可以让用户不使用“babel-plugin-relay”和“replay-compiler”的开源框架,我有自己的方式来处理查询、突变和片段,所以我需要在内部直接将字符串转换为 GraphQLTaggedNode,没有“babel-plugin-relay”和“replay-compiler”。

我该怎么做?理论上我可以使用graphql-js甚至ANTLR来开发自己的编译器,但是我不想做重复的工作。可以复用中继编译器已有的功能吗?

标签: javascriptgraphqlcompiler-constructionbabeljsrelay

解决方案


推荐阅读