2

I have two types: Todo and Comment.

type Todo {
    id: ID!
    name: String
    description: String
    priority: Int
    status: TodoStatus
    comments: [Comment]
}
type Comment {
    todoid: ID!
    commentid: String!
    content: String
}

Then I have a Query getTodo but the thing is I want to include a content arg that filters the comment (child) if the string passed is in the content of Comment.

getTodo(id: ID!, content: String!): Todo

I have tried attaching a resolver to comments (under Todo). If I add a filter here, how will I be able to get the ctx.args.content which was passed in getTodo(id: ID!, content: String!).

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        ## Provide a query expression. **
        "expression": "todoid = :id",
        "expressionValues" : {
            ":id" : {
                "S" : "${ctx.args.id}"
            }
        }
    },
    "filter": {
        "expression": "contains(content, :content)",
        "expressionValues" : {
            ":content": {
                "S": "${ctx.args.content}"
            }
        }
    }
}

Or if I remove this filter and leave it like this:

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        ## Provide a query expression. **
        "expression": "todoid = :id",
        "expressionValues" : {
            ":id" : {
                "S" : "${ctx.args.id}"
            }
        }
    }
}

How will I modify getTodo Resolver to fetch comments (with content that contains the string passed)? Can I do it like this (accessing if comments.content contains the content string args passed):

{
    "version" : "2017-02-28",
    "operation" : "Query",
    "query" : {
        ## Provide a query expression. **
        "expression": "id = :id",
        "expressionValues" : {
            ":id" : {
                "S" : "${ctx.args.id}"
            }
        }
    },
    "filter": {
        "expression": "contains(comments.content, :content)",
        "expressionValues" : {
            ":content": {
                "S": "${ctx.args.content}"
            }
        }
    }
}
  • Have you tried `${context.source.content}`? That should give you access to the parent args from with in a nested resolver. – Maurice Jul 19 '19 at 16:28

1 Answers1

1

The GraphQL arguments you are accessing via $ctx.args are available at the field level. So when you access them when resolving the Post.comments field, you get back all the arguments on that field and that field only.

So to get the content string argument you could update your Todo type to:

type Todo {
    id: ID!
    name: String
    description: String
    priority: Int
    status: TodoStatus
    comments(content: String): [Comment]
}

Another way, if you don't want to expose the content argument on the Post.comments field and keep the argument on the Query.getTodo field, you could pass the arguments via the source. In your Query.getTodo resolver you could return the content field as part of the todo result. You would then be able to access it inside the Todo.comments resolver via $ctx.source.content.

Tinou
  • 5,908
  • 4
  • 21
  • 24
  • Hi, how can I access the content args that is passed in comments in the comments resolver? – Reden John Zarra Jul 18 '19 at 00:22
  • Will I be able to access content in Todo.contents resolver if I pass it in the result of getTodo resolver using this: `#set($result = $ctx.result)` `#set($result.content = $util.parseJson($ctx.args.content))` `$util.toJson($result)` – Reden John Zarra Jul 18 '19 at 00:46
  • Which solution are you going for? If you are ok with changing your schema by adding the content argument on Todo.comments field, then you can just access the arguments via $ctx.args.content in the Todo.comments resolver. – Tinou Jul 18 '19 at 02:10