Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

453
Views
How to handle hyphens in GraphQL Schema definitions

My mongoose schema is as follows

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

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 Returns the following error: Error: Names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/ but "png-xlarge" does not.

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?

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

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

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.

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?

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;
    },
},
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!