首页 > 解决方案 > 如何使用“flask-graphql”将资源管理器添加到 GraphiQL?

问题描述

我正在使用 GraphiQL 服务,flask-graphql但没有看到任何方法可以在不使用 Node.JS 的情况下启用 GraphiQL 的“资源管理器”。有没有办法为 Python ( flask-graphql) 后端启用资源管理器?

标签: python-3.xflaskgraphql

解决方案


Node.js 不是必需的,但 Reactgraphiql必需的,因为两者graphiql-explorer都是 React 组件。flask-graphql只需使用 React 呈现 GraphiQL 界面的脚本来呈现 HTML 页面,如下所示

在通过此处graphiql_template显示的选项创建 GraphQLView 时,您应该能够提供自己的模板来呈现此页面。

复制并粘贴现有模板,然后为库添加脚本标签:

<script src="//cdn.jsdelivr.net/npm/graphiql-explorer@0.4.6/graphiqlExplorer.min.js"></script>

并按照此处所示实现组件。由于您没有转换此代码,因此您将无法使用 JSX,因此您需要执行类似...

React.createElement(
  "div",
  { className: "graphiql-container" },
  React.createElement(
    GraphiQLExplorer,
    { /* props here */ },
  ),
  React.createElement(
    GraphiQL,
    { /* props here */ },
  )
)

推荐阅读