-
|
For example, a graphql schema is associated with other schemas. After being generated by server-preset, both the operation resolvers and the object type resolvers will require the corresponding schema to be returned. However, I can clearly put it into the corresponding object type resolvers for processing. If I want to maintain type safety and hope that when the object type resolvers is implemented, parant resolvers can ignore the check of the corresponding fields. What is the best practice? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 7 replies
-
|
Hello, In this case, the recommended way is to use mappers to let Codegen know your intention. Let's use this : # src/app/schema.graphql
type Query {
user: User
}
type User {
id: ID!
name: String!
favouriteBook: Book
}
type Book {
id: ID!
title: String!
}And you may store a "User" this in your database: Then, you may use the Database User interface as your "mapper": // src/app/schema.mappers.ts
export type UserMapper = {
id: string;
name: string;
favouriteBookId?: string;
}Now, you can return the database User in // src/app/Query/user.ts
export const user = () => {
const dbUser = db.user.find("1");
return dbUser;
}And access // src/app/User.ts
export const User = {
favouriteBook: (dbUser) => {
if(!dbUser.favouriteBookId) {
return null;
}
const book = db.book.find(dbUser.favouriteBookId);
return book;
}
} |
Beta Was this translation helpful? Give feedback.
Hello,
Hopefully I understanding your question right: you are asking how to avoid having to resolve an object in the parent, because you want to resolve it at the field level using server preset.
In this case, the recommended way is to use mappers to let Codegen know your intention.
Let's use this :
And you may store a "User" this in your database:
Then, you may use the Database User interface a…