How to Troubleshoot Apps for the Modern Connected Worker
REST API Graph API GraphQL GraphiQL Presentation
1. Index
Social Graph
Graph API
Graph API Basics
a) HTTP
b) Access Tokens
c) Structure, Versions, Operations
GraphQL
GraphiQL : Graph API Explorer
REST API
REST VS GraphQL
2. Social Graph
A social graph is a diagram that illustrates interconnections among people, groups and
organizations in a social network.
The term is also used to describe an individual's social network.
3. Social Graph
When portrayed as a map, a social graph appears as a set
of network nodes that are connected by lines.
Each node, which may also be referred to as an actor, is
actually a data record.
The connecting lines used to map relationships between
data records are referred to as edges, ties or
interdependencies.
4. Social Graph
The interconnected objects are represented by points termed as vertices or actor or node
The links that connect the vertices are called edges or links.
5. Social Graph
When a social graph has a small number of nodes, it can
easily be mapped.
When the social graph has a large number of nodes it is
difficult to difficult for anyone to get meaningful information
out of the diagram.
Graph database software such as Apache Giraph makes it
easier to analyze a large, complex social graph database
through queries.
This is how most social media sites, including Facebook,
use search algorithms to personalize news feeds and
target advertising.
6. Graph API
Used to get data into and out of the Facebook platform.
The Graph API is the primary way for apps to read and
write to the Facebook social graph.
The Graph API is the primary way to get data into and out
of the Facebook platform.
It's a low-level HTTP-based API that apps can use to
programmatically query data, post new stories, manage
ads, upload photos, and perform a wide variety of other
tasks.
It is the primary way for apps to read and write to the
Facebook social graph.
7. Graph API Basics
The Graph API is named after the idea of a "social graph"
a representation of the information on Facebook. It's
composed of:
nodes — basically individual objects, such as a User, a
Photo, a Page, or a Comment
edges — connections between a collection of objects and
a single object, such as Photos on a Page or Comments
on a Photo
fields — data about an object, such as a User's birthday, or
a Page's name
9. Graph API Basics: HTTP
The Graph API is HTTP-based, so it works with any
language that has an HTTP library, such as cURL and
urllib. This means you can use the Graph API directly in
your browser. For example, requesting this URL in your
browser...
https://graph.facebook.com/facebook/picture?redirect=fals
e
... is equivalent to performing this cURL request:
curl -i -X GET
"https://graph.facebook.com/facebook/picture?redirect=fals
e&access_token={valid-access-token-goes-here}"
10. Graph API Basics: Access Tokens
Most Graph API requests require an access token.
The Access Token is a credential that can be used by an
application to access an API.
An access token is an opaque string that identifies a user,
app, or Page and can be used by the app to make graph
API calls.
It can be any type of token (such as an opaque string or a
JWT) and is meant for an API.
Its purpose is to inform the API that the bearer of this token
has been authorized to access the API and perform
specific actions
Access tokens formats:
11. Graph API Basics: Access Tokens
Access Token Type:
1. User Access Tokens:
This kind of access token is needed any time the app calls
an API to read, modify or write a specific person's
Facebook data on their behalf.
User access tokens are generally obtained via a login
dialog and require a person to permit your app to obtain
one.
2. App Access Tokens:
This kind of access token is needed to modify and read the
app settings.
You obtain an app access token via a server-to-server call.
12. Graph API Basics: Structure
use nodes to get data about individual objects
use edges to get collections of objects connected to a
node, or to publish objects to those collections
use fields to specify which data you want included in
responses
13. Graph API Basics: Structure
Host URL:
Almost all requests are passed to the graph.facebook.com
host URL. The single exception is video uploads, which
use graph-video.facebook.com.
Object Ids:
Nodes are individual objects, each with a unique ID, so to
get information about a node you directly query its ID. For
example, the official Facebook Page has the ID
20531316728. You query it directly by using its ID:
https://graph.facebook.com/20531316728
But to access it you will require access token first.
14. Graph API Basics: Structure
Fields Ids:
If you want to get specific data (called fields) about a node,
you can include the fields parameter and specify which
fields you want returned in the response
https://graph.facebook.com/20531316728?fields=cover
Edge Ids:
Most nodes have edges, which can return collections of
objects connected to that node. To query an edge, you use
both the node ID and the edge name.
https://graph.facebook.com/20531316728/photos
15. Graph API Basics: Versions
The Graph API has multiple versions. You can read more
about versioning in our App Version guide, but here we'll
explain how you make a call to a specific version.
It's really simple — just add the letter v followed by the
version number to the start of the request path. For
example, here's a call to version 2.9:
GET graph.facebook.com/v2.9/20531316728/photos
If you do not include a version number we will default to
the oldest available version, so it's best to include the
version number in your requests.
16. Operations you can perform with
the Graph API.
HTTP/1.1
Host URL
Access Tokens
Reading Nodes, Edges, Fields, Field Expansion
Limiting Results
Ordering Results
Publishing
Updating
Deleting
Traversing Paged Results
17. GraphQL
GraphQL is an open source data query and manipulation
language, and a runtime for fulfilling queries with existing
data.
GraphQL is a data query language and runtime designed
and used at Facebook to request and deliver data to
mobile and web apps since 2012.
GraphQL was developed internally by Facebook in 2012
before being publicly released in 2015.
18. GraphQL
GraphQL is a query language for your API, and a server-
side runtime for executing queries by using a type system
you define for your data.
GraphQL provides a complete and understandable
description of the data in your API, gives clients the power
to ask for exactly what they need and nothing more.
Enables powerful developer tools.
GraphQL isn't tied to any specific database or storage
engine and is instead backed by your existing code and
data.
It is meant to provide a common interface between the
client and the server for data fetching and manipulations.
19. GraphQL principles
It has three principles:
1. GraphQL is Declarative : Being Declarative means that
responses are decided by the client, not the server. A
query returns exactly what the client requested and
nothing more.
2. GraphQL is Compositional : Being Compositional means
that the queries themselves are a hierarchical set of fields.
The response to your request will be shaped just like the
response. This makes data formats predictable and
eliminate the need for separate specifications for data
returns.
3. GraphQL is Strong-typed : Being Strong-typed means
that a query can be ensured to be valid by the built in type
20. GraphQL Features
1. Ask for what you need,get exactly that :
Send a GraphQL query to your API and get exactly what
you need, nothing more and nothing less.
GraphQL queries always return predictable results.
Apps using GraphQL are fast and stable because they
control the data they get, not the server.
2. Get many resources in a single request :
GraphQL queries access not just the properties of one
resource but also smoothly follow references between
them.
GraphQL APIs get all the data your app needs in a single
request.
21. GraphQL Features
3. Describe what’s possible with a type system :
GraphQL APIs are organized in terms of types and fields,
not endpoints.
Access the full capabilities of your data from a single
endpoint.
GraphQL uses types to ensure Apps only ask for what’s
possible and provide clear and helpful errors.
Apps can use types to avoid writing manual parsing code.
4. Evolve your API without versions :
Add new fields and types to your GraphQL API without
impacting existing queries.
22. Disadvantages of GraphQL
Caching Isn’t Easy with GraphQL :
REST client can use HTTP caching to avoid refetching
resources.
GraphQL has no caching mechanism in place, hence
leaving the clients with the responsibility of taking care of
caching on their end.
Authorization Problems :
Authorization is completely a separate layer and the
language itself will not assist with the application or usage
of verification or authentication.
23. When to use GraphQL ?
Some APIs such as those with very few entities and
relationships across entities like analytics APIs may not be
suited for GraphQL.
Whereas applications with many different domain objects
like e-commerce where you have items, users, orders,
payments, and so on may be able to leverage GraphQL
much more.
Graph databases work best when the data you’re working
with is highly connected and should be represented by
how it links or refers to other data, typically by way of
many-to-many relationships.
social network is a useful example.
24. GraphiQL : Graph API Explorer
The Graph API Explorer is a tool that allows you to
construct and perform Graph API queries and see their
responses, for any apps on which you have an admin,
developer, or tester role.
The tool is extremely useful during app development
because it inherits all of your app's settings, including
approved login permissions, features, and settings for any
products you may have added.
In order to use the Graph API Explorer, you must be
signed into your Facebook developer account and have an
admin, developer, or tester role on at least one app.
25. GraphiQL : Graph API Explorer
GraphiQL is a simple UI for making queries.
GraphiQL is an in-browser tool for writing, validating, and
testing GraphQL queries.
Type queries into left side of the screen, and you will see
intelligent typeaheads aware of the current GraphQL type
schema and live syntax and validation errors highlighted
within the text.
GraphQL queries typically start with a "{" character. Lines
that starts with a # are ignored.
An example GraphQL query might look like:
{
field(arg: "value") {
26. GraphiQL : Graph API Explorer
This screen shot shows the GraphQL query { hello } being issued and giving a result of
{ data: { hello: 'Hello world!' } }.
GraphiQL is a great tool for debugging and inspecting a server, so we recommend
running it whenever your application is in development mode.
27. REST API
A RESTful API is an application program interface (API)
that uses HTTP requests to GET, PUT, POST and
DELETE data.
A RESTful API -- also referred to as a RESTful web service
-- is based on representational state transfer (REST)
technology, an architectural style and approach to
communications often used in web services development.
What is an API ?
An API for a website is code that allows two software
programs to communicate with each another.
A RESTful web service usually defines a URI, Uniform
Resource Identifier a service, which provides resource
28. Advantages of REST API
Client-Server: This constraint operates on the concept that
the client and the server should be separate from each
other and allowed to evolve individually.
Stateless: REST APIs are stateless, meaning that calls can
be made independently of one another, and each call
contains all of the data necessary to complete itself
successfully.
Cache: Because a stateless API can increase request
overhead by handling large loads of incoming and
outbound calls, a REST API should be designed to
encourage the storage of cacheable data.
Uniform Interface: The key to the decoupling client from
server is having a uniform interface that allows
29. what’s wrong with REST APIs?
The biggest problem with REST APIs is the nature of
multiple endpoints.
These require clients to do multiple round-trips to get their
data.
REST is a lightweight architecture but it is not suitable to
handle a complex environment.
REST requests (especially GET) are not suitable for large
amount of data.
In a REST API, there is no client request language. Clients
do not have control over what data the server will return.
Over-fetching of information is a waste of network and
memory resources for both the client and server.
30. REST VS GraphQL
Similarity: REST and GraphQL can both be operated over
HTTP
Advantages of GraphQL over REST API :
Data fetching
Network Requests
Over/Under Data Fetching
Error Handling
Caching
Versioning
Deprecation Made Easy
31. REST VS GraphQL
Data fetching :
The most significant improvement that GraphQL
introduced is data fetching.
In a typical REST API, to fetch or retrieve data from a
server, we might end up making requests to multiple
endpoints.
But with GraphQL, we only have one endpoint with which
we access data on a server.
With a single request, we can get an object and its related
objects.
32. REST VS GraphQL
Example:
Let’s consider this scenario and see how it can be tackled
with REST and then GraphQL: assuming we want to
retrieve an author along with the posts he/she has created,
and in turn get the comments on each of the posts.
With REST, we might have a /authors/:id endpoint to fetch
an author, then another /authors/:id/posts endpoint to fetch
the post of that particular author. Lastly, we could have a
/authors/:id/posts/:id/comments endpoint that fetches the
comments on the posts. As you can see, we need to make
requests to three different endpoints in other to accomplish
our scenario above.
With GraphQL, we only need to make a request to one
33. REST VS GraphQL
With GraphQL, we only need to make a request to one endpoint,
say /graphql
with the following query: and we would get a response like:
34. REST VS GraphQL
Over or Under Data Fetching:
supports declarative data fetching, the users can only get
what they actually need from the server.
Select only book title and price:
35. REST VS GraphQL
Network Requests :
GraphQL reduces network requests by allowing us fetch or
retrieve all the data we need in a single query.
Error Handling :
Error handling in REST is pretty straightforward, we simply
check the HTTP headers to get the status of a response.
Depending on the HTTP status code ( 404, 503, 500 etc)
we get, we can easily tell what the error is and how to go
about resolving it.
In graphQL we will always get a 200 OK response status.
When an error occurs while processing GraphQL queries,
the complete error message is sent to the client with the
36. REST VS GraphQL
Caching :
REST client can use HTTP caching to avoid refetching
resources.
GraphQL has no caching mechanism in place, hence
leaving the clients with the responsibility of taking care of
caching on their end.
Versioning :
Often when consuming third-party REST APIs, we see stuff
like v1, v2, v3 etc. which simply indicate the version of the
REST API we are using. This leads to code redundancy
and less maintainable code.
With GraphQL, there is no need for versioning as we can
37. REST VS GraphQL
Deprecation Made Easy :
we can easily mark fields as deprecated and the fields will
be excluded from the response gotten from the server.
Performance Optimization :
While REST usually defaults to the entirety, GraphQL is
generally the least possible request. Despite the fact that if
REST API gives backs an elementary partial, there are
more fragments being transmitted by default with
GraphQL.
38. SQL Database
store related data in tables
require a schema which defines tables prior to use
encourage normalization to reduce data redundancy
support table JOINs to retrieve related data from multiple
tables in a single command
implement data integrity rules
provide transactions to guarantee two or more updates
succeed or fail as an atomic unit
can be scaled (with some effort)
use a powerful declarative language for querying
offer plenty of support, expertise and tools.
39. NoSQL database
store related data in JSON-like, key-value paired
documents
can store data without specifying a schema
must usually be denormalized so information about an item
is contained in a single document
should not require JOINs (presuming denormalized
documents are used)
permit any data to be saved anywhere at anytime without
verification
guarantee updates to a single document — but not
multiple documents
provide excellent performance and scalability
40. Graph Database
A graph database (GDB) is a database that uses graph
structures for semantic queries with nodes, edges and
properties to represent and store data.
A key concept of the system is the graph (or edge or
relationship), which directly relates data items in the store.
The relationships allow data in the store to be linked
together directly, and in many cases retrieved with one
operation.
Graph databases, by design, allow simple and fast retrieva
of complex hierarchical structures that are difficult to model
in relational systems.
41. Graph Database
Compared with relational databases, graph databases are
often faster for associative data sets and map more
directly to the structure of object-oriented applications.
They can scale more naturally to large data sets as they
do not typically need costly join operations
Neo4j is a popular Graph Database. Other Graph
Databases are Oracle NoSQL Database, OrientDB,
HypherGraphDB, GraphBase, InfiniteGraph, and
AllegroGraph.
43. APIs and graph query-
programming languages
AQL (ArangoDB Query Language) - an SQL-like query
language used in ArangoDB for both documents and
graphs
Cypher Query Language (Cypher) – a graph query
declarative language for Neo4j that enables ad hoc and
programmatic (SQL-like) access to the graph.
GraphQL – Facebook query language for any backend
service
Gremlin – a graph programming language that works over
various graph database systems; part of Apache
TinkerPop open-source project
SPARQL – a query language for RDF databases, can
44. High level API and Low level API?
What is the difference between high level API and low level
API?
A high-level API allows the developer to work with WSDL.
A WSDL compiler generates all of your SOAP interfaces,
so as a programmer all you need to do is invoke methods
on objects, and you never need to diretly manipulate the
SOAP structures (the envelope, header, and body).
A low-level API allows the developer to work directly with
the SOAP structures. Rather than invoking methods on
objects, you need to construct SOAP messages and call
them.
High-level usually means generic and relatively simple.
45. WSDL and SOAP
WSDL is an XML format for describing network services as
a set of endpoints operating on messages containing
either document-oriented or procedure-oriented
information.
SOAP is an XML-based protocol that lets you exchange
info over a particular protocol (can be HTTP or SMTP, for
example) between applications.
Web services are of two kinds: Simple Object Access
Protocol (SOAP) and Representational State Transfer
(REST).
SOAP defines a standard communication protocol (set of
rules) specification for XML-based message exchange.
SOAP uses different transport protocols, such as HTTP
46. Thank You
A presentation by Atharva Jawalkar
facebook.com/jawalkaratharva
twitter.com/jawalkaratharva
instagram.com/atharvajawalkar
http://www.linkedin.com/in/atharvajawalkar
youtube.com/atharvajawalkar
atharvajawalkar.blogspot.com
slideshare.com/atharvaj1