Having your cake and eating it too:
GraphQL and Reason
Any similar but new tech has a lot of ground to cover if it's to catch up.
Imagine we're building a node client, and we want to know if we've covered all of the endpoints.
How would we do that with Spotify's docs?
Computer assistance is one way only
GraphQL has three top-level parts:
query: Top-level read-only datamutation: "Write" or effectful operation that returns read-only datasubscription: Continuous read-only dataquery {
user {
# select these two fields from a User
id
email
}
}
mutation {
# The GraphQL server has defined a mutation called
# setEmail parameterized by a string argument
setEmail(newEmail: "sean@onegraph.com") {
# In this case, this operation returns a "User",
# same as query above
id
email
updatedAt
}
}
{
"query": "query {\n user {\n # select these two fields from a User\n id\n email\n }\n}",
"variables": null,
"operationName": null
}
Put the operations from before into a string, and put it in a JSON structure under the "query" key.
Then add "variables" and "operationName" (both nullable) to form a full payload for a GraphQL request.
query {
npm {
package(name: "express") {
name
downloads {
lastMonth {
count
}
}
}
}
}
curl 'https://serve.onegraph.com/dynamic?app_id=0b33e830-7cde-4b90-ad7e-2a39c57c0e11' \
-H 'Content-Type: application/json' \
-H 'show_beta_schema: true' \
-d '{"query":"query { npm { package(name: \"express\") { name downloads { lastMonth { count } } } } }"}'
Try copy/pasting the curl command, it should just work™!
We can select more than data from the API…
We can select more than data ABOUT the API…
query {
__schema {
types {
name
}
}
}
curl 'https://serve.onegraph.io/dynamic?app_id=0b33e830-7cde-4b90-ad7e-2a39c57c0e11' -k \
-H 'Content-Type: application/json' \
-H 'show_beta_schema: true' \
-d '{"query":"query IntrospectionQuery { __schema { types { name } } }"}' | jq -C "." | head -n 150
Try copy/pasting the curl command (with jq installed) to see the first 150 types defined by this GraphQL service!
Let's find all the objects belonging to Spotify:
fetch(
'https://serve.onegraph.com/dynamic?app_id=0b33e830-7cde-4b90-ad7e-2a39c57c0e11',
{
Headers: {'Content-Type': 'application/json', show_beta_schema: true},
method: 'POST',
body:
'{"query":"query IntrospectionQuery { __schema { types { name } } }"}',
},
).then(result => result.json()).then(result => window.spotifyTypes = result.data.__schema.types.filter(type => type.name.startsWith("Spotify")));
Enter: GraphiQL
Let's explore NPM API
We know the types of all the fields, it's easy to generate fake data!
Can even be taken further with analysis.
REST can struggle with over/under fetching, multiple round trips, etc.
https://developer.github.com/v3/repos/#get
What if we want to know which clients have ever looked at a given field?
Queries themselves are easily introspectable with GraphQL tooling
How far can introspectable servers and queries go?
What kind of crazy tooling could we build that would super-charge out ability to ship product?
How would you do that with the (great!) Spotify REST API documentation
…
…
¯\_(ツ)_/¯ ¯\_(ツ)_/¯ ¯\_(ツ)_/¯
GraphQL is not SQL, it's fundamentally - and intentionally - more limited in its expressiveness.
Any similar but new tech has a lot of ground to cover if it's to catch up.
function insertCustomer(name, subName, org, rating, order) {
...
}
Ambiguous and difficult
Reason is TypeScript with:
All built in, plus:
hello-world invocationReason focuses explicitly in making web UIs & frontends as easy as possible:
"[Reason] is the best way to take React to the next level"
- Jordan Walke, creator of Reason, React
Compare new ReasonReact component to new React.js component
Still, there are challenges in typed languages:
But, if we're on board with those challenges, what can we do?
What about combining GraphQL's introspectability, Reason's babel-like preprocesser, and the slick type-safe ReasonReact?
live reprocessing demo
… and a race!
Reason is:
Read https://github.com/Astrocoders/reason-apollo-hooks/blob/master/README.md#usequery
As mindful server developers, we have to remember the goals of the clients:
Make sure you're adhering to all the previous points:
bar none.※
※ … having not really used any others
Easily create a compile-time safe GraphQL engine:
type user = {
id: int,
name: string,
};
/* User object */
let user =
Schema.(
obj("user", ~doc="A user in the system", ~fields=_ =>
[
field(
"id",
~doc="Unique user identifier",
~typ=non_null(int),
~args=Arg.[],
~resolve=(ctx, p) =>
p.id
),
field("name", ~typ=non_null(string), ~args=Arg.[], ~resolve=(ctx, p) =>
p.name
),
]
)
);
Combine objects together into an explicit schema for your clients
/* Schema that clients will see */
let schema =
Schema.(
schema([
field(
"users",
~typ=non_null(list(non_null(user))),
~args=Arg.[],
~resolve=(ctx, ()) =>
users
),
])
);
Both GraphQL and Reason are still figuring lots of things out.
What you see is usable today, but isn't the final product.
React hooks are an example!
Not all of the GraphQL spec covered (though most of it is now)
Probably won't be able to easily get GraphiQL-like auto-complete in-editor (maybe atom?)
Still figuring lots of things out:
GraphQL wrappers make dealing with APIs in typed languages easier than any alternative:
What does it mean for Reason (or Elm/Purescript/etc.) client and server devs if every service has a GraphQL wrapper?
GraphQL and Reason, a match made in heaven!
Example app: SpotDJ
Ačiū, Vilnius!