【问题标题】:Module build failed: GraphQLError模块构建失败:GraphQLError
【发布时间】:2017-10-29 18:20:34
【问题描述】:

我无法理解错误,我添加了graphq-tag来解析webpack配置文件中的.graphql,如下所示

{
  test: /\.(graphql|gql)$/,
  exclude: /node_modules/,
  loader: 'graphql-tag/loader'
}

以下是我的.graphql 文件

import {gql} from 'react-apollo';

const query = gql`
  query {
    userCurrent{
      profile {
      name
    }
    appRoles,
    username,
    authEmail {
      address
    }
    orgs {
      userOrgRoles
      orgId
      name
      orgRoles
    }
    currentOrg {
      orgId
      userOrgRoles
      name
      orgRoles
    }
  }
}`;

export default query;

错误

ERROR in **./src/modules/goin_users/client/graphql_queries/user_graphql_queries.graphql
[2] Module build failed: GraphQLError**
[2]     at syntaxError (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/bundledParser.js:1349:16)
[2]     at unexpected (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/bundledParser.js:1043:34)
[2]     at parseDefinition (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/bundledParser.js:206:10)
[2]     at parseDocument (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/bundledParser.js:160:23)
[2]     at parse (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/bundledParser.js:97:11)
[2]     at parseDocument (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/index.js:125:16)
[2]     at gql (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/index.js:158:10)
[2]     at Object.module.exports (/home/sairam/Downloads/goin/goin/node_modules/graphql-tag/loader.js:27:18)

【问题讨论】:

    标签: reactjs graphql graphql-js


    【解决方案1】:

    您的 .graphql 文件应该只包含普通查询,而不是 javascript 代码:

    query {
        userCurrent{
          profile {
          name
        }
        appRoles,
        username,
        authEmail {
          address
        }
        orgs {
          userOrgRoles
          orgId
          name
          orgRoles
        }
        currentOrg {
          orgId
          userOrgRoles
          name
          orgRoles
        }
    }
    

    然后直接导入:

    import query from './query.graphql';
    
    console.log(query);
    // {
    //   "kind": "Document",
    // ...
    

    graphql-tag loader 将处理其余部分。

    在单独的文件中只使用普通查询可以让您直接在您使用的代码编辑器中获得查询突出显示和自动建议的优势:

    【讨论】:

    • 谢谢,我在看https://github.com/apollographql/GitHunt-React时也猜到了
    猜你喜欢
    • 2019-04-08
    • 2021-04-27
    • 2019-12-26
    • 2019-11-06
    • 2019-03-06
    • 2019-12-15
    • 2018-11-27
    • 2016-02-22
    • 1970-01-01
    相关资源
    最近更新 更多