Ellery Leung
Ellery Leung

Reputation: 675

AWS AppSync: Add multiple query string items in request mapping template not work

I have a request mapping template as follow: This is mapped to a GraphQL query called getPost

{
  "version": "2018-05-29",
  "method": "GET",
  ## E.G. if full path is https://api.xxxxxxxxx.com/posts then resourcePath would be /posts **
  "resourcePath": "/event1/wp-json/wp/v2/posts",
  "params":{
      ## "query":$util.toJson($ctx.args),
      "query":{
        "slug": "$ctx.args.slug",
        "lang": "$ctx.args.lang"
      },
      "headers": {
          "Authorization": "$ctx.request.headers.Authorization"
      }
  }
}

A valid JSON response URL is this:

http://my_domain/wp-json/wp/v2/posts?slug=new-post-en&lang=zh-hant

But when I tested in "Queries" using this:

query MyQuery {
  getPost(slug: "new-post-en", lang: "zh-hant") {
    id
  }
}

No result return, but it should have at least one record returned.

Can you please tell me what's wrong with my request?

Thank you.

UPDATED Add Schema:

type Post {
    id: ID!
    date: String!
    slug: String!
    type: String!
    link: AWSURL
    title: AWSJSON!
    content: AWSJSON!
    excerpt: AWSJSON!
}

type Query {
    getPost(slug: String!, lang: String): Post
    getPosts: [Post]
}

schema {
    query: Query
}

Upvotes: 0

Views: 928

Answers (1)

Ellery Leung
Ellery Leung

Reputation: 675

I found the solution:

  • When we make a request to http://my_domain/wp-json/wp/v2/posts?slug=new-post-en&lang=zh-hant, it will return an array in terms of JSON
  • However, in the schema, the query is: getPost(slug: String!, lang: String): Post
  • Notice the return type Post, it is not an array
  • So to get a valid return, we change it from
    • getPost(slug: String!, lang: String): Post to
    • getPost(slug: String!, lang: String): [Post]

And hola~ It works!

Upvotes: 1

Related Questions