Type-graphql: Mutations inside the query

Created on 25 Sep 2019  Â·  2Comments  Â·  Source: MichalLytek/type-graphql

Hello guys!
I have question about typegraphql.
I found the example written in graphql-tools about "deep-level mutations". And I found it so useful, but I don't understand how I can write it using typegraphql library.

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

I found it useful because you can structure your API as it structured in GitHub GraphQL API
I just want to someone re-write it and show in comments and explain where I can find more information.

Thank you.

PS: Sorry if is it a easy question, but I didn't find any info about it, and sorry about my English, I'm not a native speaker.

Question Solved

Most helpful comment

I don't see this pattern useful. When your mutation changes something, you should return the mutated resources, not force the users to perform a nested query to read again the mutated resource.

So as your query don't rely on the changed resource, you can just perform the query operation as it doesn't depend on the mutation.

All 2 comments

I don't see this pattern useful. When your mutation changes something, you should return the mutated resources, not force the users to perform a nested query to read again the mutated resource.

So as your query don't rely on the changed resource, you can just perform the query operation as it doesn't depend on the mutation.

Closing for a housekeeping purposes 🔒

Was this page helpful?
0 / 5 - 0 ratings

Related issues

avkonst picture avkonst  Â·  3Comments

limenutt picture limenutt  Â·  3Comments

winuxue picture winuxue  Â·  4Comments

laukaichung picture laukaichung  Â·  3Comments

tongtwist picture tongtwist  Â·  3Comments