【问题标题】:Hypen is not supporting GraphQL连字符不支持 GraphQL
【发布时间】:2018-08-27 14:40:24
【问题描述】:

我是 graphQL 的新手,当我尝试使用连字符/破折号定义架构时,它会显示错误。但是下划线没有任何问题。

# Type of Hello
enum HowAreYou{
  Hello-Hello
  Hai-Hai
}



throw (0, _error.syntaxError)(source, position, 'Invalid number,         expected digit but got: ' + printCharCode(code) + '.');
 ^
GraphQLError: Syntax Error GraphQL request (176:9) Invalid number,     expected digit but got: "H".

175: enum HowAreYou{
176:   Hello-Hello
         ^
177:   Hai-Hai

【问题讨论】:

    标签: node.js schema naming-conventions graphql


    【解决方案1】:

    这是故意的——per the specification,在 GraphQL 中命名实体时,连字符不是有效字符。名称应该符合这种模式:

    /[_A-Za-z][_0-9A-Za-z]*/
    

    这意味着只允许使用字母、数字和下划线,并且名称不能以数字开头。

    【讨论】:

      【解决方案2】:

      我们可以在我们的架构部分指定所需的枚举,并在我们的解析器部分编写相应的自定义。

      const typeDefs = `
        enum Color {
          RED
        };
      `
      
      const resolvers = {
        Color: {
          RED: '#FF0000',
        }
      };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-03-16
        • 1970-01-01
        • 2022-08-16
        • 2021-11-09
        • 2018-07-10
        • 2018-02-02
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多