Graphql-tools: Feature request: allow .json schema files in makeExecutableSchema

Created on 28 Jul 2017  路  8Comments  路  Source: ardatan/graphql-tools

It looks like makeExecutableSchema only accepts a JavaScript string; could it also accept a .json schema file so that it would work with the output from https://github.com/apollographql/apollo-codegen?

Most helpful comment

Not sure about the official version, but I followed the comment from @sidd here to create a client-sided executable GraphQL schema from a schema.json. Just for the sake of completeness:

import { addResolveFunctionsToSchema } from 'graphql-tools';
import { buildClientSchema } from 'graphql/utilities';

import schema from './schema.json';
import { resolvers } from './MockResolvers';

const executableSchema = buildClientSchema(schema.data);

addResolveFunctionsToSchema({
  schema: executableSchema,
  resolvers,
});

export { executableSchema };

Would be great to know whether there is a more official way to this :)

Edit: Perhaps this writeup is useful as well.

All 8 comments

I was looking to achieve the same thing, it would be a nice feature.

For now I've settled on a workaround that achieves the same thing (at least for my use case). Instead of makeExecutableSchema, use graphql-js's buildClientSchema, which accepts an introspection query result: http://dev.apollodata.com/tools/graphql-tools/mocking.html#Mocking-a-schema-using-introspection

To add resolvers, use addResolveFunctionsToSchema: http://dev.apollodata.com/tools/graphql-tools/resolvers.html#addResolveFunctionsToSchema

I'm happy to share a code snippet if there's any confusion.

I'm now using and recommending the new graphql-cli and graphql-config standards: https://www.graph.cool/blog/2017-08-01-graphql-config-and-cli-aeghee3di9. Would be great if this repo gets updated to use them as well :)

Closing as https://github.com/apollographql/graphql-tools/issues/388 would be better :)

BTW - #382 has a makeRemoteExecutableSchema which accepts a URL

Would be great if this repo gets updated to use them as well :)

I think that standard is more for things like CLI tools, I'm not sure where that would fit in here - but would be open to a new issue to discuss!

Not sure about the official version, but I followed the comment from @sidd here to create a client-sided executable GraphQL schema from a schema.json. Just for the sake of completeness:

import { addResolveFunctionsToSchema } from 'graphql-tools';
import { buildClientSchema } from 'graphql/utilities';

import schema from './schema.json';
import { resolvers } from './MockResolvers';

const executableSchema = buildClientSchema(schema.data);

addResolveFunctionsToSchema({
  schema: executableSchema,
  resolvers,
});

export { executableSchema };

Would be great to know whether there is a more official way to this :)

Edit: Perhaps this writeup is useful as well.

There's an even simpler way, which I discovered from the graphql JS repo's test suite, that allows you to use the introspection query _and_ makeExecutableSchema, meaning you have access to all the options that it gives e.g. resolverValidationOptions

This assumes you used apollo-cli to download your schema i.e. apollo schema:download

import { makeExecutableSchema } from "graphql-tools"
import schemaFile from "./schema.json"
import { printSchema, buildClientSchema } from "graphql/utilities"

const schemaSDL = printSchema(buildClientSchema({ __schema: schemaFile }))
export const schema = makeExecutableSchema({
  typeDefs: schemaSDL,
})
Was this page helpful?
0 / 5 - 0 ratings