Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import .graphql files with webpack

Trying to import this file:

subscription onNewNotification {
    notification {
        id
        content
        seen
    }
}

I get this error when running babel-node to compile the whole thing: SyntaxError: .../graphql/NotificationSubscription.graphql: Unexpected token, expected ;

This is how my webpack loaders look like:

module: {
        loaders: [
            {
                test: /\.js?$/,
                include: [
                    path.join(__dirname, 'client'),
                    path.join(__dirname, 'server/shared'),
                ],
                exclude: /node_modules/,
                loader: 'babel-loader',
                query: {
                    presets: ['react-hmre'],
                },
            },
            {
                test: /\.json?$/,
                loader: 'json-loader',
            },
            {
                test: /\.scss$/,
                loaders: ['style-loader', 'css-loader', 'sass-loader'],
            },
            { test: /\.css$/, loader: 'style-loader!css-loader' },

            { test: /\.svg(\?v=\d+\.\d+\.\d+)?$/, loader: 'file-loader?mimetype=image/svg+xml' },
            {
                test: /\.(graphql|gql)$/,
                exclude: /node_modules/,
                loader: 'graphql-tag/loader',
            },
        ],


    },

No idea why this is happening, I'm following the Apollo docs and I don't see anything about this on google: http://dev.apollodata.com/react/webpack.html

like image 421
Vlady Veselinov Avatar asked May 21 '17 01:05

Vlady Veselinov


People also ask

What is Gql file?

gql A JavaScript template literal tag that parses GraphQL query strings into the standard GraphQL AST. /loader A webpack loader to preprocess queries.

Why is Server side caching difficult in GraphQL?

Being a globally unique identifier means that the URL is unique and cannot have duplicates. In GraphQL, the POST HTTP method is used to perform queries and there are no URL-like endpoints like we have in RESTful APIs. This makes caching GraphQL queries difficult.

What is makeExecutableSchema?

makeExecutableSchema combines schema and resolvers to make executable schema. It's part of a graphql-tools package that makes it easier to use the schema language while also writing resolvers. So you define types and resolvers and pass them to makeExecutableSchema .

Is GraphQL cacheable?

HTTP caches will not caches POST requests, which means GraphQL is simply not cacheable at the HTTP level. However, GET is indeed a valid way to query a GraphQL server over HTTP. This means that caches could indeed cache GraphQL responses.


1 Answers

Well, I am having the same problem too. After talk with on guy on apollo slack channel we start to debug and discover what is the problem. We discover that the problem was the 'babel-register' that in some kind of way trying to compile the .graphql files even with the rule to ignore. So there are two paths you can go:

    1. Easier to do: Instead of creating a graphql file you can just create a js file to save your queries and create them using the gql function on react-apollo package and export it as a variable.
    1. More sofisticated: If you use webpack you can create a configuration that will compile all your server code in one bundle and run the bundle using node (no babel-node or babel-register) like it is in GitHunt-React example (https://github.com/apollographql/GitHunt-React). It's not so simple like the first one but it's more elegant.
like image 160
matheus.rocha89 Avatar answered Sep 29 '22 05:09

matheus.rocha89