Hey guys,
tutorial is clear on how to integrate w/ a sql database,
how would we go about integrating w/ an api?
Hey guys,
tutorial is clear on how to integrate w/ a sql database,
how would we go about integrating w/ an api?
I guess you’d have to replace the usage of Prisma within your services, and call your API instead.
If you look at the code generated for a model scaffold, you get something like this in the api/src
directory:
In posts.js
are the actual handlers for your different model accessors. For example:
export const post = ({ id }) => {
return db.post.findOne({
where: { id },
})
}
As far as I can tell, that’s where the transition from the backend to the DB lies.
If you replace db.post.findOne
with whatever you need to call your API, that should work
Note that it’s OK to return a Promise (that’s what this code does)!
For Redwood to find your services functions, you’ll have to declare a GraphQL schema manually in an SDL file, such as the api/src/graphql/posts.sdl.js
one generated by the scaffold generator (if I’m not mistaken).
You could of course create your own. Its content gives information about your model and how to manipulate it:
export const schema = gql`
type Post {
id: Int!
title: String!
body: String!
createdAt: DateTime!
}
type Query {
posts: [Post]
post(id: Int!): Post
}
input PostInput {
title: String
body: String
}
type Mutation {
createPost(input: PostInput!): Post
updatePost(id: Int!, input: PostInput!): Post
deletePost(id: Int!): Post
}
`
I guess the findMany
and findOne
services functions found in api/services/posts/posts.js
are inferred from the type Query
entry in this SDL file.
Hopefully, if I’m missing something, Rob or David will come to the rescue
@arjunv27 so actually, @thedavid talked about that approach in another post so I guess that’s indeed the way to go!