frankcollins3 / Next-Water-App

Happy, Healthy Water Cycling App that tracks user/human fluid intake.
https://next-water-app.vercel.app
1 stars 0 forks source link

apollo-server-micro convention on query | mutation ? [12:10am] #30

Open frankcollins3 opened 12 months ago

frankcollins3 commented 12 months ago

attempting to do: decide whether this prisma hitting data query with graphQL will be within the query or mutation object

error: no error really.. it works already.

    getDailyData: async (parent, args) => {
          const { users_id } = args
          const allusers = await allusersDB()
          const alldata = await alldataDB()
          const dataLength = alldata.length
          let me = allusers.filter(users => users.id === users_id)
          const date = new Date()
          const dayName = date.toLocaleDateString('en-US', { weekday: 'long' } )
          const dateString = `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()}`        
          const dateAndUserCheck = alldata.find(data => data.date === dateString && data.users_id === users_id)
          if (dateAndUserCheck) {
            // Data already exists for the given date and user
            return dateAndUserCheck;
          }

          **if (!me) return
          return prisma.data.create({
            data: {
              id: dataLength + 1,
              google_id: me.google_id || 'no google-id',
              date: dateString,
              progress: 0,
              weekday: dayName,
              status: [],
              users_id: users_id
            }
          }).then( (newdate) => {
            let d = newdate;
            return { id: d.id, google_id: d.google_id, date: d.date, progress: d.progress, weekday: d.weekday, status: d.status, users_id: d.users_id }
          })
      },**

the emboldened code which is a prisma Create Promise runs in case there is no data

proposed approach: Enclose this function with the query Mutation object