如何在GraphQL模式定义中处理连字符 [英] How to handle hyphens in GraphQL Schema definitions

查看:119
本文介绍了如何在GraphQL模式定义中处理连字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的猫鼬模式如下

var ImageFormats = new Schema({
     svg        : String,
     png-xlarge : String,
     png-small  : String
});

当我将其转换为GraphQL模式时,这就是我尝试的方式

When I translate this into a GraphQL Schema, this is what I try

export var GQImageFormatsType: ObjectType = new ObjectType({
     name: 'ImageFormats',

     fields: {
          svg        : { type: GraphQLString },
         'png-xlarge': { type: GraphQLString },
         'png-small' : { type: GraphQLString }
 }
});

GraphQL返回以下错误:Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.

GraphQL Returns the following error: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.

如果我尝试在Mongoose模型之后对GraphQL建模,如何协调字段?我有办法创建别名吗?

If I am trying to model the GraphQL after my Mongoose model, how can I reconcile the fields? Is there a way for me to create an alias?

(我在涂鸦和stackoverflow论坛上进行了搜索,但找不到类似的问题)

(I have searched for this on the graffiti and stackoverflow forums but could not find a similar question)

推荐答案

GraphQL返回以下错误:错误:名称必须匹配/^ [_ a-zA-Z] [_ a-zA-Z0-9] * $/,但"png-xlarge"不匹配.

GraphQL Returns the following error: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.

GraphQL抱怨字段名称'png-xlarge'无效.错误消息中的正则表达式表示第一个字符可以是字母,无论大小写或下划线.其余字符也可以有数字.因此,很明显,对于字段名,连字符-和单引号'都不可接受.这些规则基本上遵循您在几乎每种编程语言中都可以找到的变量命名规则.您可以检查 GraphQL命名规则.

GraphQL complains that field name 'png-xlarge' is invalid. The regular expression in error message says that the first character can be a letter irrespective of case or underscore. The remaining characters can also have digit. Therefore, it is clear that neither hyphen - nor single quote ' is acceptable for a field name. The rules basically follow the variable naming rules that you find in almost every programming language. You can check the GraphQL naming rules.

如果我尝试在Mongoose模型之后对GraphQL建模,如何协调字段?我有办法创建别名吗?

If I am trying to model the GraphQL after my Mongoose model, how can I reconcile the fields? Is there a way for me to create an alias?

借助resolve功能,您可以按照以下步骤进行操作:

With the help of resolve function, you can do this as follows:

pngXLarge: { 
    type: GraphQLString,
    resolve: (imageFormats) => {
        // get the value `xlarge` from the passed mongoose object 'imageFormats'
        const xlarge = imageFormats['png-xlarge'];
        return xlarge;
    },
},

这篇关于如何在GraphQL模式定义中处理连字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆