简体   繁体   中英

Nested query and mutation in type-Graphql

I found a feature in graphql to write nested query and mutation, I tried it but got null. I found the best practices of building graphqL schema on Meetup HolyJs and the speaker told that one of the best ways is building "Namespaced" mutations/queries nested, in this way you can write some middlewares inside the "Namespaced" mutations/queries and for get the Child mutation you should return an empty array because if you return an empty array, Graphql understand it and go one level deep.

Please check the example code.

Example in 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
      };
    },
  }
};

This is my Code

types

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;
  }
}

Result of code

query {
  Me{
    hello
    meCheck{
      meHello
    }
  }
}

--RESULT--
{
  "data": {
    "Me": {
      "hello": "world",
      "meCheck": {
        "meHello": null
      }
    }
  }
}

I got a null instead a meHello resolver. Where am I wrong?

命名空间突变违反了GraphQL规范,因为它们不保证可以按顺序运行-有关此问题的GitHub问题中的此讨论中的更多信息: https : //github.com/MichalLytek/type-graphql/issues/64

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM