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?
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 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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With