Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
393 views
in Technique[技术] by (71.8m points)

node.js - How can I put special characters in an enum value in apollo graphQL server?

I have a problem with the enum type in apollo-graphQL, I'm using apollo-server in nodejs. The problem is that I can't use strings with a value like image/jpeg or svg+xml. It's giving me errors that these values can't be parsed as enums.

Enum values

enter image description here

Can somebody tell me how to fix this ?

question from:https://stackoverflow.com/questions/65864564/how-can-i-put-special-characters-in-an-enum-value-in-apollo-graphql-server

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Unfortunately, the answer is that you cannot. According to the GraphQL spec, an enum is defined as

Name but not true or false or null

"Name" here refers to this definition, which defines a "Name" with the regular expression

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

This means that enums (which are defined as "names values") can only have letters, numbers, or underscores, and the first character can't be a number. Additionally, the spec recommends that you use "all caps", which I read to mean "CONSTANT_CASE".

If an enum really is what you want, to follow the recommendation of the spec, you "should" use

enum ImageMimeTypes {
  IMAGE_APNG
  IMAGE_AVIF
  IMAGE_GIF
  IMAGE_JPEG
  IMAGE_PNG
  IMAGE_SVG_XML
  IMAGE_WEBP
}

though I personally like to name my enums as singular constant case, as well -- just so it's less a surprise of what it represents -- so I'd probably call it IMAGE_MIME_TYPE.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...