Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
GraphQL – the future of APIs?
1. GraphQL – the future
of APIs?
- OR -
GraphQL in a brief
Dmitry Stropalov, Sizmek Kraków 2017
2. What is GraphQL
• GraphQL is a (query) language
- declarative
- hierarchical
- strongly typed
• GraphQL is a runtime
- execution layer
- technology agnostic
2
3. Yet another technology?
• Era of mobile devices
• Flexibility & Transparency
• Productivity & Costs
3
5. I have a data. What’s next?
5
1. Define a schema for data representation
GraphQL Schema:
type Campaign {
name: ID!
title: String
running: Boolean
ads: [Ad]
}
Elixir DSL:
object :campaign do
field :id, non_null(:id)
field :title, :string
field :running, :boolean
field :ads, list_of(:ad),
resolve: assoc(:ads)
end
6. I have a data. What’s next?
6
2. Define a schema of queries and mutations
GraphQL Schema:
type Query {
campaign(id: ID!): Campaign
}
Elixir DSL:
field :campaign, :campaign do
arg :id, non_null(:id)
resolve &CampaignResolver.find/2
end
7. I have a data. What’s next?
7
3. Implement resolvers for queries and mutations
Elixir:
def find(%{id: id}, _info) do
case Repo.get(Campaign, id) do
nil -> {:error, "Campaign #{id} not found"}
campaign -> {:ok, campaign}
end
end
8. I have a data. What’s next?
8
4. Make some queries
GraphQL query:
query {
campaign(id: 1) {
running
ads {
budget {
amount
}
}
}
}
GraphQL mutation:
mutation {
createCampaign(
title: "New campaign",
running: false, ads: [
{title: “LI 1"}
]) {
id
}
}
10. Scalar Types
10
• Int: A signed 32‐bit integer.
• Float: A signed double-precision floating-point value.
• String: A UTF‐8 character sequence.
• Boolean: true or false.
• ID: non human-readable String.
• Custom scalars via scalar keyword.
11. Lists & Enumeration Types
• Lists defined with [ and ] around type.
• Enum – special type of scalar with a restricted set of
possible values:
enum Episode {
FIRST
SECOND
THIRD
}
11
12. Object Types
• Object type – named set of fields with theirs types.
• Fields could be scalars or other object types, enums, lists.
• Fields could be marked as non-nullable:
type Ad {
title: String!
targetings: [Targeting!]!
}
12
13. Interfaces & Unions
• Interface – an abstract object type:
interface Player {
nickname: String!
}
type User implements Player {
nickname: String!
…
}
• Union – specifies a set of possible object types:
union SearchResult = Picture | Video | Document
search(text: "Kraków") {
… on Picture { size }
… on Video { length }
… on Document { format }
}
13
14. Arguments
• Every field of an object type can have zero or more
arguments:
type FuelTank {
volume(unit: VolumeUnit = LITRE): Float
}
• All arguments are named.
• Could be required or optional.
14
15. Input Types
• Input types – object types used for mutations.
• Input and output object types can’t be mixed.
• No arguments on input object fields.
15
17. Introspection & Docs
• Introspection is built-in via special __schema object.
• Any type introspection via __type object.
• Documentation is a part of schema definition:
@desc "Get campaign by ID."
field :campaign, :campaign do
arg :id, non_null(:id), description: "Specifies campaign ID.”
resolve &CampaignResolver.find/2
end
17
21. The Glory of REST
21
From the article “Richardson Maturity Model” by M.Fowler
22. GraphQL & REST
• GraphQL is a language, specification and tools while REST is an
architectural concept.
• GraphQL is completely independent of a transport protocol used.
• With GraphQL you can think in terms of a business logic objects
instead of resources.
• Different caching approaches.
• API payload optimisations.
• Field usage monitoring.
22