0

I'm using GraphQL to scan a collection in dynamodb that looks like this:

{
  "config_name": "COLOR_PALETTE",
  "config_value": {
    "SHOW_CUSTOM_SUBSCRIPTION": "NO",
    "SHOW_DD_SUBSCRIPTION": "NO",
    "SHOW_GRADIENT_SUBSCRIPTION": "YES",
    "SHOW_SOLID_SUBSCRIPTION": "NO"
  }
}

I want to return "config_value" as key value pair. How I define AppSync schema and resolver to get "config_value" as key value pair ?

1 Answer 1

1

If you set your schema up as follows:

type ConfigValues {
    SHOW_CUSTOM_SUBSCRIPTION: String!
    SHOW_DD_SUBSCRIPTION: String!
    SHOW_GRADIENT_SUBSCRIPTION: String!
    SHOW_SOLID_SUBSCRIPTION: String!
}

type Query {
    getConfigValues(configName: String!): ConfigValues
}

schema {
    query: Query
}

With a resolver on getConfigValues with a request mapping template of:

{
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
        "config_name": $util.dynamodb.toDynamoDBJson($ctx.args.configName),
    }
}

And a response mapping template of:

$util.toJson($ctx.result.config_value)

Performing the following query:

query {
  getConfigValues(configName: "COLOR_PALETTE") {
    SHOW_CUSTOM_SUBSCRIPTION
    SHOW_DD_SUBSCRIPTION
    SHOW_GRADIENT_SUBSCRIPTION
    SHOW_SOLID_SUBSCRIPTION
  }
}

Will have the response of:

{
  "data": {
    "getConfigValues": {
      "SHOW_CUSTOM_SUBSCRIPTION": "NO",
      "SHOW_DD_SUBSCRIPTION": "NO",
      "SHOW_GRADIENT_SUBSCRIPTION": "YES",
      "SHOW_SOLID_SUBSCRIPTION": "NO"
    }
  }
}

This answer assumes that your Primary partition key of your DynamoDB table is set to be config_name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.