有一个graphql端点,我不拥有它,但它提供了一个公共端点。我希望用石墨粉来反省它。我对graphql完全陌生,所以我甚至不知道这种事情是否可能发生。
我让石墨化实例在本地运行,并且正在修改server.js以使其工作。四处窥探其他线索让我走了这么远..。
var introspectionQuery = require('graphql/utilities').introspectionQuery;
var request = require('sync-request');
var url = 'http://endpoint.com/graphql';
var response = request('POST', url, { qs: { query: introspectionQuery } } );
var schema = JSON.parse(response.body.toString('utf-8'));
// herein lies the rub
schema = new GraphQLSchema(schema.data.__schema);
var app = express();
app.use(express.static(__dirname));
app.use('/graphql', graphqlHTTP(() => ({
schema: schema,
})));
app.listen(8080);这段代码在GraphQLSchema构造函数中崩溃,试图利用这个内省查询创建一个模式。很明显这不是正确的方法?
发布于 2016-11-06 21:16:12
您想要在内省结果之外构建模式的是buildClientSchema。
var buildClientSchema = require('graphql/utilities').buildClientSchema;
var introspectionQuery = require('graphql/utilities').introspectionQuery;
var request = require('sync-request');
var response = request('POST', url, { qs: { query: introspectionQuery } });
// Assuming we're waiting for the above request to finish (await maybe)
var introspectionResult = JSON.parse(response.body.toString('utf-8'));
var schema = buildClientSchema(introspectionResult);您可以用另外两种方式构建模式:buildASTSchema和直接实例化GraphQLSchema,这就是您正在尝试的。GraphQLSchema构造函数接受具有GraphQLSchemaConfig类型的对象:
type GraphQLSchemaConfig = {
query: GraphQLObjectType;
mutation?: ?GraphQLObjectType;
subscription?: ?GraphQLObjectType;
types?: ?Array<GraphQLNamedType>;
directives?: ?Array<GraphQLDirective>;
};这两个实用程序模块分别通过使用buildClientSchema或buildASTSchema提供了从内省查询结果或解析IDL类型定义构建模式的更简单的方法。有关更多信息,请参阅graphql/src/实用程序目录中的这些模块。
发布于 2017-05-31 18:25:54
我是用PHP库来尝试这个的。我遇到了很多问题,尝试以上围绕CORS (跨源安全的东西)。
然后我发现GraphIQL可以作为Chrome应用程序使用。这解决了我的需要,所以在此指出,对于任何遇到这个问题的人来说都是有用的。您不需要进行任何编码就可以让GraphIQL与远程端点一起工作。
https://stackoverflow.com/questions/40451636
复制相似问题