[英]How to create a GraphQL query in type-graphql with a custom return type
[英]Nested query and mutation in type-Graphql
我在graphql中發現了一個可編寫嵌套查詢和變異的功能,我嘗試了一下,但得到了null。 我發現了在Meetup HolyJs上構建graphqL模式的最佳實踐,演講者告訴我們,最好的方法之一是構建嵌套的“命名空間”變異/查詢,這樣,您可以在“命名空間”變異/查詢中編寫一些中間件,並且獲得Child突變,您應該返回一個空數組,因為如果您返回一個空數組,Graphql會理解它並深入一層。
請檢查示例代碼。
graphql-tools中的示例
const typeDefs = gql`
type Query { ...}
type Post { ... }
type Mutation {
likePost(id: Int!): LikePostPayload
}
type LikePostPayload {
recordId: Int
record: Post
# ✨✨✨ magic – add 'query' field with 'Query' root-type
query: Query!
}
`;
const resolvers = {
Mutation: {
likePost: async (_, { id }, context) => {
const post = await context.DB.Post.find(id);
post.like();
return {
record: post,
recordId: post.id,
query: {}, // ✨✨✨ magic - just return empty Object
};
},
}
};
這是我的代碼
類型
import { ObjectType, Field } from "type-graphql";
import { MeTypes } from "../User/Me/Me.types";
@ObjectType()
export class MeNameSpaceTypes {
@Field()
hello: string;
@Field({ nullable: true })
meCheck: MeTypes;
}
import { Resolver, Query } from "type-graphql";
import { MeNameSpaceTypes } from "./MeNamespace.types";
@Resolver()
export class MeResolver {
@Query(() => MeNameSpaceTypes)
async Me() {
const response = {
hello: "world",
meCheck:{}
};
return response;
}
}
代碼結果
query {
Me{
hello
meCheck{
meHello
}
}
}
--RESULT--
{
"data": {
"Me": {
"hello": "world",
"meCheck": {
"meHello": null
}
}
}
}
我得到的是null而不是meHello解析器。 我哪里錯了?
命名空間突變違反了GraphQL規范,因為它們不保證可以按順序運行-有關此問題的GitHub問題中的此討論中的更多信息: https : //github.com/MichalLytek/type-graphql/issues/64
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.