Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inspecting a remote graphql endpoint with graphiql

There is a graphql endpoint which I don't own but which provides a public endpoint. I'm hoping to introspect it using graphiql. I'm totally new to graphql, so I don't even know if this sort of thing is possible.

I have the graphiql example running locally and am modifying server.js to try to make it work. Poking around at other SO threads has gotten me this far...

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

This code blows up in the GraphQLSchema constructor, trying to make a schema out of that introspection query. Clearly that's not quite the right approach?

like image 225
Josh Santangelo Avatar asked Nov 06 '16 16:11

Josh Santangelo


People also ask

How do you check the endpoint of a GraphQL?

We saw why not testing your app is not an option and how you can test your GraphQL endpoint: test your schema with eslint-plugin-graphql and schema-graphql-linter. test your queries and mutations with EasyGraphQL Tester. test your resolvers like you would test any other Javascript function.

How do you query in GraphiQL?

The GraphiQL client allows you to create variables for use in your queries. To add a query variable, click the Query Variables pane and enter a JSON object that defines your variable. To use a variable in your query, prepend the $ character to your variable name and use it to replace the desired value.

Does GraphQL have endpoint?

GraphQL is typically served over HTTP via a single endpoint which expresses the full set of capabilities of the service. This is in contrast to REST APIs which expose a suite of URLs each of which expose a single resource.


1 Answers

What you want to build schema out of the introspection result is 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);

You could build the schema in two other ways: buildASTSchema and instantiating GraphQLSchema directly, which is what you're trying out. GraphQLSchema constructor takes in an object with GraphQLSchemaConfig type:

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

And those two utility modules provide easier ways to build the schema from either from introspection query result or parsed IDL type definitions, respectively by using buildClientSchema or buildASTSchema. Refer to those modules in graphql-js/src/utilities directory for more information please.

like image 152
Hyohyeon Jeong Avatar answered Oct 25 '22 19:10

Hyohyeon Jeong