[GraphQL] Importing schema.graphql file to use as typeDefs

Amandeep Kochhar
2 min readFeb 13, 2019

--

While working with Apollo GraphQL + Prisma + Node + ReactJS stack, I was following an approach to import typeDefs like this, which was not the bad apporach but not the best even. Because i wasn’t getting syntax high-lightening while defining schema.

I created a file named “schema.js” and put the typeDefs there and simply exported it as default.

schema.js file containing typeDefs

In the index.js where i created the ApolloServer, imported this file and then used as typeDefs.

Everything works, but this was not the best approach, as i wasn’t getting syntax high-lightening while writing schema.

So, after research i found this approach which is very suitable:

Created a “schema.graphql” file instead of “schema.js” (Here i was using .graphql file extension instead of normal .js (JavaScript) file), In this file you cannot import and export anything because its not a JavaScript file. So simply write down the schema.

schema.graphql file with only typeDefs without imporing & exporting anything

Now to translate this schema.graphql file to normal typeDefs we will use a npm module named graphql-import like this:

Using graphql-import module to import the schema.graphql file

And that’s it, You are ready to run your graphql server now. This wasn’t the biggest thing in the world to solve but i tried to share what i was doing wrong and what i did to correct it.

Leave some claps, and maybe a follow if this post helped you in any way :) I will continue to post all the things i am experiencing in my Full Stack Journey.

--

--

Responses (2)