In Neo4j Graphql Apollo server type definition how to add names with spaces?

461 views Asked by At

My type definition is like this

type COMPANY {
    "AIRLINE SEGMENT": String             
    AIRLINE_TYPE: String
}

I get an error saying

apollo server GraphQLError: Syntax Error: Expected Name, found :

If I remove the space and quotes then I don't get an error. I have searched everywhere but I haven't a way to add names with spaces in them.

Any help will be highly appreciated. Thanks.

1

There are 1 answers

7
Daniel Rearden On BEST ANSWER

GraphQL does not support spaces within names. According to the spec, names must match the following regular expression:

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

GraphQL actually ignores all white space within a document, with the exception of white space within Strings and Comments:

White space is used to improve legibility of source text and act as separation between tokens, and any amount of white space may appear before or after any token. White space between tokens is not significant to the semantic meaning of a GraphQL Document, however white space characters may appear within a String or Comment token.

You probably shouldn't be using spaces within node names in the first place, even if they are technically supported. This is the recommended naming convention:

Camel case, beginning with an upper-case character

If your data source returns an object with fields whose names include spaces, you can convert these to "legal" field names inside your resolver map. For example, given a type named Company with a field called airlineSegment:

const resolvers = {
  Company: {
    airlineSegment: company => company['AIRLINE SEGMENT']
  }
}