#How can I resolve the issue with nested queries?

1 messages · Page 1 of 1 (latest)

muted oyster
#

I have two resolvers, each with a @ResolveField that is related to each other, following is the code-first approach I tried to implement:

// post.resolver.ts
@Resolver(() => Post)
export class PostResolver {
  constructor(
    private authorService: AuthorService
  ) {}

  ...
  
  @ResolveField(() => Author)
  author(@Parent() post: Post) {
    return this.authorService.getAuthor(post.id);
  }
}
// author.resolver.ts
@Resolver(() => Author)
export class AuthorResolver {
  constructor(
    private readonly postService: PostService
  ) {}

  ...
  
  @ResolveField(() => [Post])
  posts(@Parent() author: Author) {
    console.log(author);

    return this.postService.getPostByAuthor(author.username);
  }
}

This implementation is causing my graphql query to have nested query problems like the example below:

query ShowAllPost {
  showAllPost {
    author {
      username
      posts {
        title
        author {
          email
        }
      }
    }
  }
}

How to avoid this problem?

steady turret
#

You can restrict queries based on the total complexity. It won't stop anyone to issuing such query, but it would return an error if it exceed a certain complexity level or maximum query depth. I don't have the links at hand, but google is your friend.

prime knot
muted oyster
#

Thanks a lot @prime knot @steady turret !

muted oyster
muted oyster
# muted oyster i tried what you suggested, but it seems did not work with ResolveField?

refere to the example on https://docs.nestjs.com/graphql/complexity#getting-started

in the post resolver:

  @ResolveField(() => Author, { complexity: 1 })
  author(@Parent() post: Post) {
    return this.authorService.getAuthor(post.id);
  }

and the example query:

query ShowAllPost {
  showAllPost {
    author {
      posts {
        author {
          username
        }
      }
    }
  }
}

and still not thrown an error :"

#

is it normal that after activating the complexity plugin, the logs from the plugin continue to appear after the application is running?

prime knot
solar nimbus
#

You can also use something like envelop to restrict query depth for the server