how to use .graphql in a typescript node project without webpack
AFAIK, there are two ways to import schema files, either 1) by reading the file directly as you describe above, or 2) by wrapping the queries in exported variables.
// bookSchema.ts <- note the file extension is .ts instead of .graphql
export default `
type Book {
message: String
}
`
// anotherSchema.ts <- note the file extension is .ts instead of .graphql
export default `
type User {
name: String
}
`
// main.ts
import bookSchema from 'bookSchema';
import anotherSchema from 'anotherSchema';
const schema = makeExecutableSchema({ typeDefs: [
bookSchema,
anotherSchema,
] });
You can use https://github.com/ardatan/graphql-import-node to solve this without webpack.
Install with yarn add graphql-import-node
or npm install --save graphql-import-node
and then either use the graphql-import-node/register
hook (if you're using ts-node):
ts-node -r graphql-import-node/register index.ts
Or import it in your file right at the top like this:
import "graphql-import-node";
I chose the later in my case because I already used ts-node/register
with mocha -r
for my tests.
You also may need to add "esModuleInterop": true
to your compilerOptions in tsconfig.json
.