arackaf / mongo-graphql-starter

Creates a fully functioning, performant, extensible GraphQL endpoint from a Mongo DB. Supports middleware, Mongo 4 transactions.
MIT License
423 stars 29 forks source link

Problem in createTypeResolver.js #81

Closed peebles closed 3 years ago

peebles commented 3 years ago

If a model has "normal" relationships and a resolvedFields/extras structure, then we get a syntax error during code generation:

(node:91945) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token, expected "," (66:5)
  64 |     return context[dataLoaderName].load(obj._id || []);
  65 |   }
> 66 |     ...(OtherExtras1 || {})
     |     ^
  67 | }

The "fix" for this is:

src/codeGen/createTypeResolver.js, line #73, I believe needs a "," between "${TAB2}" and "...(OtherExtras${i + 1} || {})"

However, if the model in question has only a relation, OR only a resolvedFields/extras, then adding that comma above causes an error:

(node:92041) UnhandledPromiseRejectionWarning: SyntaxError: Unexpected token (27:5)
  25 | export const Shoe = {
  26 |
> 27 |     ,...(OtherExtras1 || {})
     |     ^
  28 | }

Here is a testcase you can fool around with that demonstrates the problem:

import { dataTypes } from "mongo-graphql-starter";
const {
  MongoIdType,
  MongoIdArrayType,
  StringType,
  StringArrayType,
  BoolType,
  IntType,
  IntArrayType,
  FloatType,
  FloatArrayType,
  DateType,
  arrayOf,
  objectOf,
  formattedDate,
  JSONType,
  typeLiteral
} = dataTypes;

export const User = {
  table: "User",
  fields: {
    something: StringType,
  },
  relationships: {
    badges: {
      get type() { return Badge },
      fkField: "_id",
      keyField: "userId",
      oneToMany: true
    }
  },
  resolvedFields: {
    shoes: "[Shoe]",
  },
  extras: {
    resolverSources: ["../../graphQL-extras/UserResolverExtras"],
  },
}

export const Shoe = {
  table: "Shoe",
  fields: {
    sn: StringType
  },
  resolvedFields: {
    users: "[User]",
  },
  extras: {
    resolverSources: ["../../graphQL-extras/ShoeResolverExtras"],
  },
}

export const Badge = {
  table: "Badge",
  fields: {
    code: StringType,
    userId: MongoIdType,
  },
  relationships: {
    user: {
      get type() { return User },
      fkField: "userId",
      keyField: "_id",
      oneToOne: true
    }
  }
}

Remove the "resolvedFields"/"extras" structure in User and the error will go away. Or leave that in and remove the "badges" relation. I dunno, maybe I am doing something wrong.

arackaf commented 3 years ago

omg thank you so much for this. Will have a look.

arackaf commented 3 years ago

Great find - thanks again! This should be fixed in 0.11.8