sikanhe / gqtx

Code-first Typescript GraphQL Server without codegen or metaprogramming
458 stars 13 forks source link

Lazy fields declaration #51

Open FedericoBiccheddu opened 3 years ago

FedericoBiccheddu commented 3 years ago

Problem: while defining some fields, is not possible to use fields defined later in the file.

Possible solution: make field declaration lazy using a function and not a plain array

Example:

const Query = t.queryType({
  fields: [
    t.field('myType', {
      type: myType, // ts(2454): Variable 'event' is used before being assigned.
      // ...
    })
  ]
})

const myType = t.objectType({ ... })
const Query = t.queryType({
  fields: () => [
    t.field('myType', {
      type: myType, // It works
      // ...
    })
  ]
})

const myType = t.objectType({ ... })

I can submit a PR for this.

PS: in case this would be accepted, we should revert #46

Jomik commented 3 years ago

I guess this becomes an issue when you have a Book with an Author and the Author has a list of Book? Your example above doesn't really showcase an issue, as you can simply lift myType up above Query