Resolver to filter a non-scalar type in AppSync / Amplify - aws-appsync

We are using AWS Amplify. This is my type
type Package #model {
id: ID!
desc: String!
company: Company! #connection
servicetype: ServiceType! #connection
price: Float!
active: Boolean!
createdAt: AWSDateTime
updatedAt: AWSDateTime
}
Amplify does not generate a filter option for listPackage to allow filtering on servicetype. My understanding is you need to add a custom query and resolver for this. I have added a query listPackageByServiceType but am confused on the resolver... cannot get it to work.
Is there a similar example of code I can follow? I cannot get the filter option to work correctly.

If you have a secondary index on your DynamoDB table based on that field, serviceType, you can do this with a query on that field.
Otherwise, the way to go is probably a scan with a filter on that field. See here for more: https://docs.aws.amazon.com/appsync/latest/devguide/resolver-mapping-template-reference-dynamodb.html#aws-appsync-resolver-mapping-template-reference-dynamodb-scan
From that link, the scan mapping template would look like this (note that depending on what you have, pagination might be necessary):
{
"version" : "2017-02-28",
"operation" : "Scan",
"index" : "fooIndex",
"filter" : {
"expression" : "filter expression"
"expressionNames" : {
"#name" : "name",
},
"expressionValues" : {
":value" : ... typed value
}
}
}

Related

Compound queries

I have a RESTful service that accepts a custom query, like this:
/entities/User?actions=
{
"$link": {
"entityType": "Manager",
"entity": {
"name": "John Smith"
},
"linkName": "managers",
"backLinkName": "account",
"$set": {
"propertyName": "aclWrite",
"propertyValue": {
"$ref": {
"propertyName": "entityId"
}
}
}
}
}
Which simply means:
Create a new Entity of type User
Create a new Entity of type Manager with the field name, linking the User to be created to this Manager through link name "managers"
Then back-linking the Manager entity to be created to the User with a link name "account" and setting the Manager entity write ACL (Access Control List) to the ID of the User to be created.
I created this query structure because I can't find any suitable Query language that can seem to support such action/procedure.
The question here is are there any Query language that can support such compound action/procedure or can GraphQL handle such?
As a specification, GraphQL doesn't care what fields your schema defines, what arguments those fields take or what your field resolvers do with those arguments. So it's perfectly feasible to design a schema that would let the client compose an equivalent mutation:
mutation {
link(
entityType: "Manager"
entity: {
name: "John Smith"
}
linkName: "managers"
backLinkName: "account"
set: {
propertyName: "aclWrite"
propertyValue: {
ref: {
propertyName: "entityId"
}
}
}
) {
# some fields here to return in the response
}
}
GraphQL does not support references to other nodes inside the same query, so you would still probably want a single mutation whose input mirrored your existing API. That said, using GraphQL for this may still be preferable because of request validation, which is all the more important with complex requests like this. Having an IDE like GraphiQL or GraphQL Playground that lets you write your queries using autocomplete is a big plus too.

LoopBack4 MongoDB Auto Increment custom ID

LoopBack itself is new for me and I see version 4 is way too different from version 3. My requirement is that I need to have a custom auto incremented id in my mongoDB document every time I create a POST to the REST end point similar to a running id in a MySQL database.
I did check this (auto-increment using loopback.js and MongoDB) and (https://gist.github.com/drmikecrowe/5a5568930bad567d4148aad75c94de5a) with a version 3 setup, but i did not find proper document to replicate the same on version 4.
Currently I am using a basic app with the out of the box REST implementations provided from the loopback 4. Below is an example of my model.
export class Test extends Entity {
#property({
type: 'string',
id: true,
})
_id?: string;
#property({
type: 'number',
generated: true,
required: false
})
id: number;
#property({
type: 'string',
required: true,
})
name: string;
#property({
type: 'boolean',
required: true,
})
val: boolean;
constructor(data?: Partial<Test>) {
super(data);
}
}
My mongodb document should look something like this:
{
"_id" : ObjectId("5c373c1168d18c18c4382e00"),
"id" : 1
"name" : "aaaa",
"val" : true
}
{
"_id" : ObjectId("5c3869a55548141c0c27f298"),
"id" : 2
"name" : "bbbbb",
"val" : false
}
You can do something like in this example
#post('/characters', {
responses: {
'200': {
description: 'Character model instance',
content: {'application/json': {schema: {'x-ts-type': Character}}},
},
},
})
async create(#requestBody() character: Character): Promise<Character> {
//add following lines
let characterId = 1;
while(await this.characterRepository.exists(characterId)){
characterId ++;
}
character.id = characterId;
//add above lines
return await this.characterRepository.create(character);
}
you probably already noticed the auto-increment id feature. When you call the post API multiple times (leave id blank), the id increased by 1 every time. This feature is supported by the in-memory database. But we are using MongoDB in this project. If we want to have that feature, we need to do that programmatically.
For more information follow below link
https://strongloop.com/strongblog/building-online-game-with-loopback-4-pt1/
see the section just above the API Explorer heading
or find for 'auto increment id' you will be taken to that paragraph
Hopefully, this helps, write me if there is any other query.
Thanks
I'm also playing with Mongo and it can autogenerate your id for you.
Specifically, when you create your model, using lb4 model, choosing 'Entity' and then you're prompted:
Let's add a property to Participant
Enter an empty property name when done
? Enter the property name: id
? Property type: string
? Is id the ID property? Yes
? Is id generated automatically? Yes
This will generate your model with the property:
#property({
type: 'string',
id: true,
generated: true,
})
id?: string;
Great.. then when creating your CRUD controller:
? What kind of controller would you like to generate? REST Controller with CRUD functions
? What is the name of the model to use with this CRUD repository? Person
? What is the name of your CRUD repository? PersonRepository
? What is the name of ID property? id
? What is the type of your ID? string
? Is the id omitted when creating a new instance? Yes
? What is the base HTTP path name of the CRUD operations? /persons
Now when hitting your endpoint, the create POST doesn't take an ID, but will return one for you.
You can do something like in this example
let last_record = await this.testRepository.findOne({order: ['id DESC']});
if(last_record) invoice.id = last_record.id+1;
This will generate your model with the property:
#property({
type: 'number',
id: true,
default: 1,
generated: false
})
id: number;
Hopefully, this helps, please write me if there is any other code. Thanks
This class inherits from the DefaultCrudRepository class and overrides the create method. The method uses the "Counters" collection to hold the last id of the current data class (this.entityClass.name). The findAndModify method will prevent duplicate id values from being created.
import {DefaultCrudRepository, Entity, DataObject, Options} from '#loopback/repository';
export class MongoAutoIncIdRepository<T extends Entity, ID, Relations extends object = {}> extends DefaultCrudRepository<T, ID, Relations> {
public async create(entity: DataObject<T>, options?: Options): Promise<T> {
if (!this.dataSource.connected) {
await this.dataSource.connect()
}
let mongoConnector = this.dataSource.connector!
let collection = mongoConnector.db.collection('Counters')
let result = await collection.findAndModify(
{
collection: this.entityClass.name
},
[['_id', 'asc']],
{
$inc: {value: 1}
},
{
upsert: true,
new: true
})
console.log(result)
// #ts-ignore
entity.id = result.value.value
return super.create(entity, options)
}
}
It's easy to use. Inherit your repository not from DefaultCrudRepository, but from MongoAutoIncIdRepository if auto increment is required. Then, when the create method is called, the id will increase by 1 automatically.

Appsync missing resolver

I'm using AWS appsync + DynamoDB.
The problem: I created the new field 'rating' in my 'Users' schema:
type Users {
id: ID!
first: String!
last: String!
rating: String #<----The new field
}
AppSync created all the resources and I can create new records with Mutations and that works like a charm.
mutation createUsers{
createUsers(input:{
first:"John"
last:"Smith"
rating:"B" #<---Writing new field without problem
}){
id
first
last
rating #<---Confirming that is recorded in DynamoDB
}
}
The problem is that I can't figure out how to write the resolver to make the following query work.
query{
queryUsersByRating(rating: "B"){
items{
id
username
rating
}
}
}
The result is this:
{
"data": {
"queryUsersByRating": null
}
}
The problem is clearly identified here under "Missing Resolver", but there's no clear solution.
I tried attaching the following Resolver directly in AppSync interface but is not working:
{
"version" : "2017-02-28",
"operation" : "Query",
"query" : {
"expression": "rating = :rating",
"expressionValues" : {
":rating" : $util.dynamodb.toDynamoDBJson($ctx.args.rating)
}
}
}
Any help would be appreciated, THANKS!
You don't have to write your own resolver for querying by rating, Appsync wrapped all the fields inside filter.
query{
queryUsersByRating(filter: {rating: "B"}){
items{
id
username
rating
}
}
}

Meteor - Update Collection with location object (in GeoJSON format)

Here is my schema (simple schema):
officelocation: {
type: String,
label: 'Location of Office',
autoform: {
type: 'map',
afFieldInput: {
type: 'map',
geolocation: true,
searchBox: true,
autolocate: true
}
}
},
location: {
optional: true,
type: 'Point'
}
My server side js code is below (note this is in a collection.after hook) so I want to update it based on the address that user has entered, which I have resolved into lat long:
Providers.update({_id: doc._id}, {$set: {location: {type:"Point", coordinates:[lng,lat]} } });
When I see the file in the collection (db.providers.find();), I see the below.. Note that the location embedded object is empty:
{ "_id" : "X8ZfKYJAP9cduwvmd", "phone" : 999999999, "officelocation" : "40.7192714,14.872363899999982", "createdAt" : ISODate("2015-04-24T02:00:40.447Z"), "updatedAt" : ISODate("2015-04-24T02:00:40.799Z"), "owner" : "GB4TxTHodkykeeXp6", "officeaddress" : "Via Califri, 5, 84099 San Cipriano Picentino SA, Italy", "location" : { } }
I am basically trying to make sure by collections are stored in a geo-spatial-searchable way, but this approach does not seem to work. Any help?
There could be a number of things causing your update to fail, from allow-deny rules to Simple Schema cleaning out your data.
I see that you are using a custom type to store your location. Make sure you have used a Transform to ensure the type isn't lost on the way to the server. From the Simple Schema readme:
Custom object types are treated as blackbox objects by default. However, when using collection2, you must ensure that the custom type is not lost between client and server. This can be done with a transform function that converts the generic Object to the custom object. Without this transformation, client-side inserts and updates might succeed on the client but then fail on the server. Alternatively, if you don't care about losing the custom type, you can explicitly set blackbox: true for a custom object type instead of using a transformation.
Alternatively you could use a sub-schema to define what a location is allowed to look like, instead of using a custom type, but it won't keep the methods of the Point type.

Does using dbref do anything more than just storing an `id`

My Mongoose schema:
// set up the schema
var CategorySubSchema = new Schema({
name: { type: String },
_category_main : { type: String, ref: 'CategoryMain' }
},
And my controller code:
CategorySub.create({
name : req.body.name,
_category_main : req.body.category_main
}, function(err, data){
An entry in my db:
{
"_id": "54dd163434d78ae58f6b1a69",
"name": "Snacks",
"_category_main": "54dcf4a71dfecb4d86ddcb87",
"__v": 0
},
So I used an underscore, because I was following an example. Does this mean anything to the database or is it just convention for references?
Also, instead of passing the entire JSON object in the request - req.body.category_main, why not just pass and id and change my schema to this?:
var CategorySubSchema = new Schema({
name: { type: String },
category_main_id : { type: String }
},
In short, Yes.
The below schema definition is an example of Manual references.
var CategorySubSchema = new Schema({
name: { type: String },
category_main_id : { type: String }
}
where,
you save the _id field of one document in another document as a
reference. Then your application can run a second query to return the
related data. These references are simple and sufficient for most use
cases.
In this case, we need to write explicit application code to fetch the referred document and resolve the reference. Since the driver that we use wouldn't know about the collection in which the referred document is present nor the database in which the referred document is present.
When you define the schema as below, this is an example of storing the details of the referred document .(Database references)
var CategorySubSchema = new Schema({
name: { type: String },
_category_main : { type: String, ref: 'CategoryMain' }
}
They include the name of the collection, and in some cases the
database name, in addition to the value from the _id field.
These details allow various drivers to resolve the references by themselves, since the name of the collection and the database(optional) of the referred document would be contained in the document itself, rather than we writing explicit application code to resolve the references.
So I used an underscore, because I was following an example. Does this mean anything to the database or is it just convention for
references?
Using underscore in the _id field is a valid naming convention, but mongoDb doesn't explicitly mention about the naming convention of other fields which are used to resolve references. You could just use any other field name as long as it conforms to this.