I'm studying nodejs w/ graphql and I would like to make sure I'm going in the right direction here.
My study project is based on a simple ERP with entries, products, orders and users.
I have one mutation that creates products indiviadually but I thought that allowing add products while creating a new entry (same mutation and same query as well) was a good aproach but now the code seems weird.
It's all working fine but I would like to have a code very well organized that follows the most common aproaches and best practices.
entry.graphql:
type EntryItems {
id:ID!
quantity: Int
created_at: DateTime
modified_at: DateTime
Entry:Entry
Product:Product
}
type Entry {
id:ID!
User: User
total: Float
created_at: DateTime
modified_at: DateTime
items:[EntryItems]
}
input EntryInput {
total: Float
user_id:Int
items:[EntryItemsInput!]!
}
input EntryItemsInput {
id:ID
quantity: Int
Product:ProductInput!
}
type Query {
entries: [Entry]
entry(id: ID!): Entry!
}
type Mutation {
addEntry(Entry: EntryInput): Entry!
updateEntry(id: ID!, Entry: EntryInput): updateEntryResposta!
deleteEntry(id: ID!): deleteEntryResposta!
}
datasource:
async addEntry (Entry) {
const Entry_id = await this.db
.insert({
user_id: Entry.user_id,
total: Entry.total,
created_at: new Date(),
modified_at: new Date()
})
.into('entry_details')
const Items = Entry.items ?? []
Items.map(async item => {
if (item.Product.id) {
this.addEntryItem({
product_id: item.Product.id,
quantity: item.quantity,
entry_id: Entry_id
})
} else {
const new_product = await this.context.dataSources.ProductsAPI.addProduct(
item.Product
)
this.addEntryItem({
product_id: new_product.id,
quantity: item.quantity,
entry_id: Entry_id
})
}
})
return await this.getEntry(Entry_id)
}
async addEntryItem (EntryItem) {
const Entry_id = await this.db
.insert(EntryItem)
.returning('id')
.into('entry_items')
return {
...EntryItem,
id: Entry_id
}
}
```
I decided to allow creating products while creating a new entry (same mutation and same query as well)
not a code review issue. What I can tell you is that sometimes a customer will ask for this specific feature, and then yes you include it. I don't know node.js so I can review the code. \$\endgroup\$