AWS AppSync simplifies application development by letting you create a universal API to securely access, modify, and combine data from multiple sources. AppSync is a managed service that uses GraphQL so that applications can easily retrieve only the data they need.
With AppSync, you can build scalable applications, including those requiring real-time updates, using a range of data sources such as NoSQL data stores, relational databases, HTTP APIs, and native data sources with AWS Lambda. For mobile and web applications, AppSync also provides access to local data when devices go offline and syncs data when they reconnect to the Internet. In this case, the client can customize the order of conflict resolution.
Get started in minutes with your chosen IDE (e.g. Xcode, Android Studio, VS Code), and use the intuitive AWS AppSync or AWS Amplify CLI management console to automatically generate your API and client code. AWS AppSync integrates with Amazon DynamoDB, Amazon Aurora, Amazon Elasticsearch, AWS Lambda, and other AWS services, allowing you to build complex applications with virtually unlimited performance and memory that change based on your business needs.
AWS AppSync provides real-time subscriptions to millions of devices and offline access to application data. Once the device is reconnected, AWS AppSync syncs only the updates at the time the device was disconnected, not the entire database. AWS AppSync offers customizable server-side conflict detection and resolution.
Perform complex queries and generalizations across multiple data sources with a single network call using GraphQL. AWS AppSync makes it easy to protect your application data by using multiple authentication modes at the same time, and also allows you to determine the severity of the threat and perform granular access control at the data definition level directly from your GraphQL schema.
In this tutorial we will create a GraphQL API that interacts with a DynamoDB NoSQL database to perform CRUD (create, read, update, delete) operations.
After the selected items, the GraphQL schema will open in
amplify / backend / api / <datasourcename> / schema.graphql where we insert this model:
This is a GraphQL schema. GraphQL Transform provides an easy-to-use abstraction that helps you quickly build back-end for web and mobile apps on AWS. With GraphQL Transform, you define your application's data model using the GraphQL Schema Definition Language (SDL), and the library handles transforming the SDL definition into a set of fully descriptive AWS CloudFormation templates that implement your data model.
When used in conjunction with tools such as Amplify CLI, GraphQL Transform simplifies the process of developing, deploying, and maintaining GraphQL APIs. With it, you define your API using the GraphQL Schema Definition Language (SDL) and then you can use automation to transform it into a fully descriptive cloud content pattern that implements the specification. GraphQL is an API specification. It is a query language for the API and a runtime for making those queries with your data. It shares some similarities with REST and is the best replacement for REST.
GraphQL was introduced by Facebook in 2015, although it has been used internally since 2012. GraphQL allows clients to define the structure of the data they require, and it is this structure that is returned from the server. Querying data in this manner provides a much more efficient way for client-side applications to interact with APIs, reducing the number of underfetching and preventing overfetching.
Learn more about GraphQL here
Let's go back to our diagram, where the main components of the GraphQL schema are object types, which is the type of object that you can retrieve from your service:
Jobis a GraphQL Object Type, that is, a type with some fields. Most of the types in your schema will be object types.
id position rate description owner- fields in the Job type. This means that these are the only fields that can appear in any part of a GraphQL query that works with a Job type.
Stringis one of the built-in scalar types - these are types that resolve to a single scalar object and cannot have sub-selections in a query. We'll look at scalar types later.
String!- field is non-null, means that GraphQL promises to always give you a value when you request this field. In general, this is a required field.
GraphQL comes with a set of default scalar types out of the box:
IntA 32-bit signed integer.
Floatdouble precision floating point value.
Stringis a sequence of UTF - 8 characters.
Booleantrue or false.
IDscalar type ID is a unique identifier, often used to retrieve an object or as a key for a cache. The identifier type is serialized in the same way as a string; however, defining it as an identifier means that it is not intended for human perception.
@ model- Object types marked with
@ modelare top-level objects in the generated API. Objects marked with
@ modelare stored in Amazon DynamoDB and can be protected with
@ auth, linked to other objects with
@ auth- Authorization is required for applications to interact with your GraphQL API. API keys are best used for public APIs. The @ auth object types annotated with @ auth are protected by a set of authorization rules that provide you with more controls than the top-level authorization in the API. You can use the @ auth directive to define object and field types in your project schema. When using the
@ authdirective on object type definitions that are also annotated with
@ model, all recognizers that return objects of that type are protected.
Other directives and details in official documentation.
means that CREATE, UPDATE, DELETE operations are allowed only to the owner, and READ operations are allowed to all.
It's time to put it to the test! Therefore, we write the command in the console:
With this command, you can quickly test your changelog without having to provision or update the cloud resources you use at each step. This way, you can set up unit and integration tests that can run quickly without affecting your cloud backend.
Simply put, queries in GraphQL are how you are going to query data. You get exactly the data you need. No more no less.
Mutations in GraphQL are a way to change data on the server and get the updated data back.
A way to maintain a connection to the server in real time. This means that whenever an event occurs on the server and when that event is raised, the server will send the appropriate data to the client.
You can see all the available methods of our created API by clicking on the Docs (Documentation Explorer) in the upper right corner. The values are clickable, so all possible queries can be viewed.
We open our API at the address that issued (each has its own) result of the amplify mock api command and execute the CREATE request by pressing the play button.
To consolidate the material, create some more vacancies.
We get a list of all vacancies. Insert the request:
To update, we need to take the vacancy ID (be sure to enter your own, not from the example) and transfer it to this request with the changed data. For example, update the fields
To delete, as in the case of the update, we need to transfer the vacancy ID (be sure to enter your own, and not from the example).
Now let's check if our rules work as we indicated in the scheme. Only the owner can update, delete, and create.
To change the user click on UpdateAuth in the main menu. Where we arbitrarily update Username and Email.
If we send a READ request, then it works, but if we send an UPDATE or DELETE request and get an error. The rules work, as required!
Now that we have tested the functionality of the API, we can publish it to the cloud with the command:
After a few minutes, the model is uploaded to the AWS servers, so next we move on to the react native application.