2016-11-06 52 views
3

有一个我没有的graphql端点,但它提供了一个公共端点。我希望用graphiql来反思它。我对graphql完全陌生,所以我甚至不知道这种事情是否可能。用graphiql检查远程graphql端点

我有本地运行的graphiql example,并且正在修改server.js以尝试使其工作。在其他SO线程闲逛已经得到了我到这地步?

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构造炸毁,试图使一个模式指出,自省查询。显然这不是正确的方法?

回答

3

你想建立的模式进行反省的结果是什么buildClientSchemabuildASTSchema和直接实例GraphQLSchema,这是你想要什么:

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); 

你可以在另外两种方式建立的模式。 GraphQLSchema构造函数采用一个对象与GraphQLSchemaConfig类型:

type GraphQLSchemaConfig = { 
    query: GraphQLObjectType; 
    mutation?: ?GraphQLObjectType; 
    subscription?: ?GraphQLObjectType; 
    types?: ?Array<GraphQLNamedType>; 
    directives?: ?Array<GraphQLDirective>; 
}; 

而那些两个实用模块提供更容易的方法从无论是从内省查询结果或解析的IDL类型定义,分别通过使用buildClientSchemabuildASTSchema构建的模式。请参阅graphql-js/src/utilities目录中的那些模块以获取更多信息。

+0

工作正常!我只需要做'buildClientSchema(introspectionResult.data)'。 –

+0

那么接下来的问题就是如何使用graphiql将查询发送回该端点...... –

+0

为此,您可以查看我们在此处的示例index.html代码:https://github.com/graphql/ graphiql/blob/master/example/index.html#L95 –

0

我正在尝试这与PHP GraphQL库。我在CORS(跨域安全的东西)方面遇到了很多与上述有关的问题。

然后我发现GraphIQL可作为Chrome应用程序使用。这解决了我的需要,所以在此注意以防遇到此问题的其他人有用。您不需要执行任何编码就可以使GraphIQL与远程端点一起工作。