繁体   English   中英

在Foxx中使用GraphiQL

[英]Using GraphiQL with Foxx

使用graphQLHTTPexpress-graphql ,可以通过以下方式进行传递:

  const {Schema} = require('./data/schema');
  const graphQLApp = express();
  graphQLApp.use('/', graphQLHTTP({
    graphiql: true,
    pretty: true,
    schema: Schema,
  }));

通过这种配置,我们可以使用GraphiQL。 如何用Foxx实现? 这个 graphql-sync ,我可以看到Foxx正在使用graphql-sync 我浏览了源代码,并在这里找到了它:

controller.js

'use strict';
const Foxx = require('org/arangodb/foxx');
const schema = require('./schema');
const graphql = require('graphql-sync').graphql;
const formatError = require('graphql-sync').formatError;

const ctrl = new Foxx.Controller(applicationContext);

// This is a regular Foxx HTTP API endpoint.
ctrl.post('/graphql', function (req, res) {
  // By just passing the raw body string to graphql
  // we let the GraphQL library take care of making
  // sure the query is well-formed and valid.
  // Our HTTP API doesn't have to know anything about
  // GraphQL to handle it.
  const result = graphql(schema, req.rawBody(), null, req.parameters);
  console.log(req.parameters);
  if (result.errors) {
    res.status(400);
    res.json({
      errors: result.errors.map(function (error) {
        return formatError(error);
      })
    });
  } else {
    res.json(result);
  }
})
.summary('GraphQL endpoint')
.notes('GraphQL endpoint for the Star Wars GraphQL example.');

可以将GraphiQL与Foxx一起使用吗? 如果 ,我该如何实现? 有什么想法吗?

谢谢。

您可以将GraphiQL与任何GraphQL API一起使用,即使它不是内置在服务器中。 您可以通过几种不同的方式自己运行GraphiQL:

  1. 作为独立的应用程序: https : //github.com/skevy/graphiql-app
  2. 作为应用程序内部的React组件: https : //github.com/graphql/graphiql

如果您将其用作npm中的React组件(选项2),则实际上可以使用其他选项来自定义它,处理发送给服务器的请求等。

有一个名为ChromeiQL的Chrome扩展程序 ,该扩展程序在工具栏中添加了一个用于打开GraphiQL窗口的按钮。 简单易用,无需运行任何服务器

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM