首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Foxx中使用GraphiQL

在Foxx中使用GraphiQL
EN

Stack Overflow用户
提问于 2016-06-24 03:42:41
回答 2查看 382关注 0票数 2

对于NodeJS,有来自express-graphqlgraphQLHTTP,可以像下面这样传递:

代码语言:javascript
复制
  const {Schema} = require('./data/schema');
  const graphQLApp = express();
  graphQLApp.use('/', graphQLHTTP({
    graphiql: true,
    pretty: true,
    schema: Schema,
  }));

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

controller.js

代码语言:javascript
复制
'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.');

在Foxx中可以使用GraphiQL吗?如果,我如何实现这一点?有什么想法吗?

谢谢。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-06-24 03:58:09

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

  1. 作为一个独立的应用:https://github.com/skevy/graphiql-app
  2. 作为应用程序中的一个反应性组件:https://github.com/graphql/graphiql

如果您使用它作为npm的一个React组件(选项2),您实际上可以使用额外的选项来定制它,操纵它发送给服务器的请求,等等。

票数 2
EN

Stack Overflow用户

发布于 2016-06-29 22:56:30

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/38005255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档