These slides (open them on your computer):
https://slides.com/jasonbahl-1/wcoc
or
Optional, but helpful:
GraphiQL
Reactive and Stateful
First things first...what is DOM?
GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing 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, makes it easier to evolve APIs over time, and
enables powerful developer tools.
http://graphql.org/
{
me {
name
}
}
{
data: {
me: {
name: "Jason Bahl"
}
}
}
GraphQL Query
GraphQL Result
query GetPost {
post( id: "..." ) {
title
link
bylines {
name
}
}
}
{ data: { post: { title: "Hello World!" link: "http://site.com/hello-world" bylines: [ { name: "John Doe", }, { name: "Jane Doe", } ] } } }
Post
Byline
"Jane Doe"
"Hello World"
name
title
Post
"Goodbye Mars"
title
Byline
"John Doe"
name
query GetPost {
post( id: "..." ) {
title
link
bylines {
name
}
}
}
{ data: { post: { title: "Hello World!" link: "http://site.com/hello-world" bylines: [ { name: "John Doe", }, { firstName: "Jane Doe", } ] } } }
Post
Byline
Jane Doe
"Hello World"
name
title
Post
"Goodbye Mars"
title
Byline
John Doe
name
Post
Byline
Jane Doe
"Hello World"
name
title
Byline
John Doe
name
bylines
bylines
RootQuery
post( id: "..." )
title
query {
}
post( id: "..." ) {
}
title
do_graphql_request()
get_post( $id )
$post->post_title
query {
posts {
edges {
node {
}
}
}
}
author {
name
avatar(size: 50) {
url
}
}
followLink
title
featuredImage(width: 300) {
url
}
excerpt
site {
name
link
}
<?php
use WPGraphQL\Types;
function wcoc_add_release_date_field( $fields ) {
$fields['releaseDate'] = [
'type' => Types::string(),
'description' => __( 'The date the film was released', 'graphql-workshop' ),
'resolve' => function( \WP_Post $post ) {
$release_date = get_post_meta( $post->ID, 'release_date', true );
$timestamp = strtotime( $release_date );
return ( false !== $timestamp ) ? date( 'M j, Y', $timestamp ) : null;
},
];
return $fields;
}
add_filter( 'graphql_post_fields', 'wcoc_add_release_date_field', 10, 1 );
App Size / Complexity
Effort
Line of "fed-up"
(pre-release)
query GetPost($id: Int!) {
post(id: $id) {
title
link
bylines {
firstName
lastName
email
}
}
}
variables: {
"id": "cG9zdDoxMTc4"
}
Operation type
Operation name
Variable Definitions
Selection set on query
Selection set on post
https://goo.gl/4PbVrq
http://graphql.org/learn/schema/
schema: { query: Query mutation: Mutation }
Every GraphQL service has a query type and may or may not have a mutation type. These types are the same as a regular object type, but they are special because they define the entry point of every GraphQL query
http://graphql.org/learn/schema/
http://graphql.org/learn/schema/
Type Post {
id: Id!
title: String
postId: Int!
bylines: [ Byline ]
}
Type Byline {
firstName: String!
lastName: String
email: String
posts: [ Post ]
}
The most basic components of a GraphQL schema are object types, which just represent a kind of object you can fetch from your service, and what fields it has
Every field on a GraphQL object type can have zero or more arguments, for example the length field below
http://graphql.org/learn/schema/
Type Byline {
firstName: String!
lastName: String
email: String
posts( first: Int! ): [ Post ]
}
At some point fields have to resolve to some concrete data. That's
where the scalar types come in: they represent the leaves of the query
http://graphql.org/learn/schema/
Int, Float, String, Boolean, ID
Custom defined (date? email? url? other?)
Also called Enums, enumeration types are a special kind of scalar that is restricted to a particular set of allowed values
http://graphql.org/learn/schema/
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
Non-null types will throw an error if a null value is returned,
or a null value is used for the input argument.
Lists indicate that an array will be returned or an array
is expected for input of an argument.
http://graphql.org/learn/schema/
Type Post {
id: Id!
bylines: [ Byline ]
}
Union Types can define on of many types that can be returned where
the returned type is not known until the query is run.
For example, in WordPress, an item in the media library can have a parent that could be a post, page or custom post type.
http://graphql.org/learn/schema/
Type MediaItem {
id: ID!
parent: Post | Page
}
Input types define the shape of what can be input into arguments
or mutations. This is particularly valuable where input values
might consistof entire objects or other nested values
In WordPress, an example might be the
arguments for tax_query or meta_query
http://graphql.org/learn/schema/