Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modularize GraphQL schema when using `buildSchema`?

Tags:

graphql

It has been about a year since I updated my graphql-js dependency. I see now that there is a utility that simplifies schema generation: buildSchema. This function takes, as an arg, your entire schema, as a string, in the GraphQL language. That's awesome, but is there a way to modularize this? My schema is not super small, and would suck to cram into a single .graphql file. Is there some sort of utility or pattern for storing each type definition in its own file, for example?

like image 820
Ryan Avatar asked Apr 30 '17 22:04

Ryan


People also ask

How do you protect a GraphQL schema?

Other approaches that developers can leverage to protect the GraphQL layer include: Use a whitelist for allowed characters. Define GraphQL schemas for mutations input. Use a single internal character encoding format to properly handle Unicode input.

How do you query a GraphQL schema?

How To Get The Schema — Introspection Queries. Some GraphQL servers don't provide a convenient GraphQL API explorer. Instead, to get the schema we need to send a HTTP request to the GraphQL server endpoint asking for the GraphQL schema. This type of HTTP request is called a GraphQL introspection query.

How do I change schema in GraphQL?

Select the database you created in previous articles and go to the GRAPHQL section from the left menu bar. Click on the UPDATE SCHEMA button and select the file containing the updated schema. At the database layer, the update process creates any missing collections, indexes, and functions.


1 Answers

If you have the graphql-tools package, you can use makeExecutableSchema to modularize your schema like so:

const schema = makeExecutableSchema({
    typeDefs: [schema1, schema2, schema3, ...],
    resolvers: resolvers,
});

That way each type can be defined in its own file.

like image 192
mstorkson Avatar answered Oct 23 '22 05:10

mstorkson