0

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.

sushmit sarmah
  • 7,508
  • 6
  • 21
  • 24

1 Answers1

3

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']
  }
}
Daniel Rearden
  • 80,636
  • 11
  • 185
  • 183
  • I tried this and this doesn't work. My neo4j database is not in my control. There are columns with spaces in them and I need to access them. In your resolver the company variable does not have the required field. – sushmit sarmah Nov 16 '18 at 12:39
  • Is there a way to provide my own cypher query in the resolver? – sushmit sarmah Nov 16 '18 at 12:42
  • The resolver was just an example. The value of `company` would depend on whatever the resolving field actually returned. Hard to make any specific recommendations without actually seeing your code. Are you not writing your own queries already? How else are you populating the data in your server? Are you using `neo4j-graphql-js`? – Daniel Rearden Nov 16 '18 at 13:10
  • I was writing my own queries till now in a REST api. I just thought of using neo4j-graphql-js and thats when I faced this problem. I thought for simple stuff like fetching columns I will use neo4jgraphql function but I guess I will write my own cypher queries in the resolver. – sushmit sarmah Nov 16 '18 at 16:58
  • Looks like `neo4j-graphql-js` is a bit limited in that sense. If you want to continue using it, you might be able to use the `@cypher` directive to run a cypher for a specific field. If you only have a handful of fields that have this issue, that may be the way to go. – Daniel Rearden Nov 16 '18 at 17:39
  • So here is what I did for neo4j-graphql-js. In the resolver function where I fetch all the nodes I replaced ```neo4jgraphql``` function with my own cypher query and fetched the data from the db. Converted that into an array of nodes that contains all the fields and returned that. After that your resolver function works. – sushmit sarmah Nov 17 '18 at 07:24
  • I will mark this answer as correct as I derived my answer from your comments. Thanks a lot – sushmit sarmah Nov 17 '18 at 07:25