0
votes

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?

1
If there's a resolver for meHello, that's not entirely clear from the code you've shown. If there's no resolver, then that's the problem.Daniel Rearden
I wrote google Docs with the question, just let me Translate it :P docs.google.com/document/d/…Daniil Shushpanov

1 Answers

0
votes

Namespaced mutations are against GraphQL spec as they are not guarranted to run sequentially - more info in this discussion in GitHub issue related to your problem: https://github.com/MichalLytek/type-graphql/issues/64