对于NodeJS,有来自express-graphql的graphQLHTTP,可以像下面这样传递:
const {Schema} = require('./data/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));有了这种配置,我们可以使用GraphiQL。如何通过Foxx实现这一目标?从这个回购中,我可以看到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.');在Foxx中可以使用GraphiQL吗?如果是,我如何实现这一点?有什么想法吗?
谢谢。
发布于 2016-06-24 03:58:09
您可以在任何GraphiQL API中使用GraphQL,即使它不是内置到服务器上的。您可以通过几种不同的方式运行GraphiQL:
如果您使用它作为npm的一个React组件(选项2),您实际上可以使用额外的选项来定制它,操纵它发送给服务器的请求,等等。
发布于 2016-06-29 22:56:30
有一个名为ChromeiQL的Chrome扩展,它在工具栏上添加了一个按钮,用于打开GraphiQL窗口。简单易用,无需运行任何服务器。
https://stackoverflow.com/questions/38005255
复制相似问题