Graphql-tools: Mocking a schema returned by server introspection

Created on 21 Mar 2017  Â·  5Comments  Â·  Source: ardatan/graphql-tools

I am using graphene-django as my GraphQL server and when it's introspected (via its own command or using apollo-codegen I get a JSON file that is quite similar in structure to a GraphQLSchema however it doesn't have the methods found in GraphQLSchema.

If I try passing the file to mockServer I get errors that it's not compatible. This is the schema: https://gist.github.com/abdulhaq-e/8351f4b6c6ddc0b6fa13e57273de6a13 . I wonder how can I use this schema in mockServer.

P.S graphene-django says that the introspected schema is formatted to be compatible with babel-relay-plugin. If perhaps there is a more standard way for formatting schemas in the awesome GraphQL world, then please include it and I will submit a PR for graphene-django to allow that formatting. Whatever to increase the graphql awesomeness :+1:

Most helpful comment

@abdulhaq-e you should be able to mock a schema via introspection really easily:

  1. Run the introspection query to get a JSON result, you can get that here: https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js
  2. Create a GraphQL.js schema object using buildClientSchema from GraphQL.js: https://github.com/graphql/graphql-js/blob/master/src/utilities/buildClientSchema.js
  3. Pass that into addMockFunctionsToSchema: http://dev.apollodata.com/tools/graphql-tools/mocking.html

The GraphQL schema tool you linked is not necessary because those tools are built right into the GraphQL.js reference implementation as I linked above!

All 5 comments

hey @abdulhaq-e :)

hmm, i think that you should try convert the introspection result into a textual scheme,
then you can use buildSchema to convert it into GraphQLSchema type :)

please keep us updated im carious now ;)

I just found out the query that gets executed for introspection. It's actually the introspectionQuery defined in the reference graphQl JS implementation. https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js

I'll try to come up with a query that generates a structure similar to ITypeDefinitions

So I found this https://github.com/gabrielf/graphql-schema-from-introspection . It doesn't work for me because I use Relay specification. I think it would be a good idea to merge the linked tool with this repository if @gabrielf and maintainers of this repo agree.

@abdulhaq-e you should be able to mock a schema via introspection really easily:

  1. Run the introspection query to get a JSON result, you can get that here: https://github.com/graphql/graphql-js/blob/master/src/utilities/introspectionQuery.js
  2. Create a GraphQL.js schema object using buildClientSchema from GraphQL.js: https://github.com/graphql/graphql-js/blob/master/src/utilities/buildClientSchema.js
  3. Pass that into addMockFunctionsToSchema: http://dev.apollodata.com/tools/graphql-tools/mocking.html

The GraphQL schema tool you linked is not necessary because those tools are built right into the GraphQL.js reference implementation as I linked above!

@stubailo Thanks a lot. I just tried it and it works perfectly.

I also submitted a PR to document this. https://github.com/apollographql/tools-docs/pull/69

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Adherentman picture Adherentman  Â·  4Comments

alfaproject picture alfaproject  Â·  4Comments

ghost picture ghost  Â·  3Comments

flippidippi picture flippidippi  Â·  3Comments

adamkl picture adamkl  Â·  3Comments