How to use graphql file loader with Next.js? #4397
-
I would much rather put my queries and mutations in import { loadDocuments } from '@graphql-tools/load'
import { GraphQLFileLoader } from '@graphql-tools/graphql-file-loader'
const documents = await loadDocuments('graphql/**/*.graphql', {
// load files and merge them into a single schema object
loaders: [new GraphQLFileLoader()]
}) However, I've come across the next-plugin-graphql which actually loads the files in the webpack loader. This doesn't quite work for me. I'm guessing I need a babel plugin to recognize it's ok to import graphql files (though Next.js is moving away from babel to SWC). config.module.rules.push({
test: /\.(graphql|gql)$/,
include: [dir],
exclude: /node_modules/,
use: [
{
loader: 'graphql-tag/loader',
},
],
}); I'm not sure if there's a solution as easy as what I did to use import { loadFilesSync } from '@graphql-tools/load-files';
import { mergeTypeDefs } from '@graphql-tools/merge';
import { makeExecutableSchema } from '@graphql-tools/schema';
import { join } from 'path';
import resolvers from './resolvers';
const typesArray = loadFilesSync(join(process.cwd(), '**/*.graphqls'), { extensions: ['graphqls'] });
export const typeDefs = mergeTypeDefs(typesArray);
export const schema = makeExecutableSchema({
typeDefs,
resolvers,
}); |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
GraphQL File Loader and |
Beta Was this translation helpful? Give feedback.
GraphQL File Loader and
load-files
are using File System directly but Next.js is bundled so they are not compatible. You should import files explicitly in the environments which are using bundlers.