Closed frankcollins3 closed 12 months ago
proposed approach: 0: keep the GraphQL POST route as a query rather than a mutation which it was in the original react-express app.
postSettings: {
type: SettingsType,
description: 'Post Settings',
args: {
weight: { type: GraphQLInt },
height: { type: GraphQLInt },
age: { type: GraphQLInt },
start_time: { type: GraphQLInt },
end_time: { type: GraphQLInt },
reminder: { type: GraphQLInt },
activity: { type: GraphQLInt },
users_id: { type: GraphQLInt },
},
resolve: async (parent, args) => {
let { weight, height, age, start_time, end_time, reminder, users_id } = args
// let { weight, height, age, start_time, end_time, reminder, activity, users_id } = args
let meAsUser = await prisma.users.findUnique({ where: { id: users_id }})
let allSettings = await prisma.settings.findMany()
let mySettings = allSettings.filter(settings => settings.users_id === users_id)
mySettings = mySettings[0]
if (mySettings) {
const deleteUser = await prisma.settings.delete({
where: {
id: mySettings.id
},
})
}
let newSettings = await prisma.settings.findMany()
let newLength = newSettings.length + 1
return NewSettings = await prisma.settings.create({
data: {
id: newLength,
weight: weight,
height: height,
age: age,
start_time: start_time,
end_time: end_time,
reminder: reminder,
activity: 0,
users_id: users_id,
}
}).then(async(data) => {
return { id, weight, height, age, start_time, end_time, reminder, activity, users_id } = data
}).catch( () => {
return { id: newLength || 0, weight: weight || 0, height: height || 0, age: age || 0, start_time: start_time || 0, end_time: end_time || 0, reminder: reminder || 0, activity: activity || 0, users_id: users_id || 0 }
})
}
},
[11:41pm]
pages/api/graphql.ts (server already running and returning data):
import { ApolloServer } from "apollo-server-micro"; import { typeDefs } from "graphql/schema" import { resolvers } from "graphql/resolvers" import Cors from "micro-cors"
const cors = Cors() const apolloServer = new ApolloServer( { typeDefs, resolvers } )
const startServer = apolloServer.start()
export default cors(async function handler(req, res) { res.setHeader('Access-Control-Allow-Credentials', 'true'); res.setHeader('Access-Control-Allow-Origin', '*'); res.setHeader('Access-Control-Allow-Methods', 'POST'); res.setHeader('Access-Control-Allow-Headers', 'Content-Type');
if (req.method === "OPTIONS") {
res.end();
return false;
}
await startServer;
await apolloServer.createHandler({
path: '/api/graphql' // I think if the path were pages/api/index.ts it would just be: /api/
})(req, res);
});
export const config = { api: { bodyParser: false } } [11:48am]
data can be seen going through the pipeline but there is a rejection somewhere. 400 error
[12:05am]
attempting to do: set-up routes
error: $ iterm2 error logs: wait - compiling /api/graphql (client and server)... event - compiled successfully in 668 ms (59 modules) error - Error: Query.addUserSettings defined in resolvers, but not in schema at addResolversToSchema (/Users/me/Desktop/next-water-app/node_modules/@graphql-tools/schema/cjs/addResolversToSchema.js:73:35)
const postSettings = axios.post('/api/graphql', { query:
query AddUserSettings( $id: Int! $age: Int! $height: Int! $weight: Int! $start_time: Int! $end_time: Int! $reminder: Int! $activity: Int! $users_id: Int! ) { addUserSettings( id: $id age: $age height: $height weight: $weight start_time: $start_time end_time: $end_time reminder: $reminder activity: $activity users_id: $users_id ) { id age height weight start_time end_time reminder activity users_id } }
, variables: { id: 3, age: 25, height: 90, weight: 190, start_time: 2, end_time: 10, reminder: 1, activity: 0, users_id: 3 } }) .then((response) => { console.log('Response:', response.data); // Process the response data }) .catch((error) => { console.log('Error:', error); // Handle the error });main-root/graphql/schema.ts:
main-root/graphql/resolvers.ts:
// note in the graphql/resolvers.ts -> this below code returns the settings.findMany() which is GraphQLList(settings) from GraphQLHTTP 👍 allDBsettings: async () => { return await prisma.settings.findMany() } // this .findMany() returns list of all Data