将 GraphiQL 与 Foxx 结合使用
Using GraphiQL with Foxx
对于 NodeJS,有
graphQLHTTP
from express-graphql
可以像下面这样传递:
const {Schema} = require('./data/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));
有了这样的配置,我们就可以使用GraphiQL了。如何通过 Foxx 实现这一目标?从 this repo,我可以看出 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 一起使用?如果 YES,我该如何实现?有什么想法吗?
谢谢。
您可以将 GraphiQL 与任何 GraphQL API 一起使用,即使它没有内置到服务器中。您可以通过几种不同的方式 运行 自己使用 GraphiQL:
- 作为独立应用程序:https://github.com/skevy/graphiql-app
- 作为应用程序中的 React 组件:https://github.com/graphql/graphiql
如果您将它用作来自 npm 的 React 组件(选项 2),您实际上可以使用额外的选项对其进行自定义,操纵它发送到服务器的请求,等等。
有一个名为 ChromeiQL 的 Chrome 扩展,它在工具栏上添加了一个按钮,用于打开 GraphiQL window。简单易行,无需运行任何服务器
对于 NodeJS,有
graphQLHTTP
from express-graphql
可以像下面这样传递:
const {Schema} = require('./data/schema');
const graphQLApp = express();
graphQLApp.use('/', graphQLHTTP({
graphiql: true,
pretty: true,
schema: Schema,
}));
有了这样的配置,我们就可以使用GraphiQL了。如何通过 Foxx 实现这一目标?从 this repo,我可以看出 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 一起使用?如果 YES,我该如何实现?有什么想法吗?
谢谢。
您可以将 GraphiQL 与任何 GraphQL API 一起使用,即使它没有内置到服务器中。您可以通过几种不同的方式 运行 自己使用 GraphiQL:
- 作为独立应用程序:https://github.com/skevy/graphiql-app
- 作为应用程序中的 React 组件:https://github.com/graphql/graphiql
如果您将它用作来自 npm 的 React 组件(选项 2),您实际上可以使用额外的选项对其进行自定义,操纵它发送到服务器的请求,等等。
有一个名为 ChromeiQL 的 Chrome 扩展,它在工具栏上添加了一个按钮,用于打开 GraphiQL window。简单易行,无需运行任何服务器