[英]Using GraphiQL with Foxx
使用graphQLHTTP
, express-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:
如果您将其用作npm中的React组件(选项2),则实际上可以使用其他选项来自定义它,处理发送给服务器的请求等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.