https://ebeldev.github.io/headlesswordpress-slides/
- Use Wordpress as a backend service api
& use new stack to deliver the front-end
Front-end stack for today = ReactJS and GraphQL
separate content and presentation
generate unique user experiences
make your content available to mobile apps, kiosk, VR experiences, and other mediums
/wp-json/wp/v2/
/posts
/media/"id"
/users/"id"
/categories/"id"
desavantages
Over-fetching = a client downloads more information than is actually needed
Under-fetching = a specific endpoint doesn’t provide enough of the required information
Maintainability and Scalability having too many endpoints
Open-sourced by Facebook in 2015
A query language for your API
Type of operations you can do on your api
Query get your data
Mutation modify your data
Subscription subscribe to action
Responsible for the SCHEMA of your API and resolvers of the data
type Query {
posts: [Post]
}
type Post {
title: String
content: String
author: Author
}
type Author {
name: String
avatar: String
posts: [Post]
}
Responsible for making the query to the server
query {
posts {
title
author {
name
avatar
}
}
}
{"data": {
"posts": {
"edges": [{
"node": {
"title": "Hello World",
"author": {
name: "My super name",
avatar: "http://www.imagesource.com/authoravatar.jpg"
}
}
}]
}
}}
Custom
PrismaFramework for self-hosted GraphQL server
WordExpressFramework to help develop Wordpress applications (server + client)
wp-graphqlWordPress pluginhttps://github.com/wp-graphql/wp-graphql
RelayFrom Facebook, for more advance developpers
Apollo-clientApollo-boost = no setup, cache, manage local and server data
WordPress will serve as the backend system on which GraphQL will be hocked on
Install wp-graphql plugin
use WPGraphQL\Types;
function wcgql_add_custom_fields_to_graphql ($fields) {
$fields['custom'] = [
'type' => Types::String(),
'description' => __( 'The date the film was released', 'wcmtl-graphql' ),
'resolve' => function ( \WP_Post $post ) {
// Get your data
$customField = get_post_meta( $post->ID, 'custom_field_name', true );
// make any modification on your data
// Return your data
return $customField;
},
];
return $fields;
}
add_filter( 'graphql_post_fields', 'wcgql_add_custom_fields_to_graphql', 10, 1 );
NodeJS
In your command line
npx create-react-app nameofyourproject
cd nameofyourproject
npm install react-router-dom —-save
npm start
localhost:3000
import React, { Component } from 'react';
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
const client = new ApolloClient({
uri: "http://localhost/wordcamp2018/graphql"
});
class App extends Component {
render() {
return (
Welcome to Headless WordPress
);
}
}
export default App;
import React, { Component } from 'react';
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
const client = new ApolloClient({
uri: "http://localhost/wordcamp2018/graphql"
});
class App extends Component {
render() {
return (
Welcome to Headless WordPress
);
}
}
export default App;
import React, { Component } from 'react';
import ApolloClient from "apollo-boost";
import { ApolloProvider } from "react-apollo";
const client = new ApolloClient({
uri: "http://localhost/wordcamp2018/graphql"
});
class App extends Component {
render() {
return (
Welcome to Headless WordPress
);
}
}
export default App;
import React, { Fragment } from 'react';
import { Query } from 'react-apollo';
import gql from 'graphql-tag';
import React, { Fragment } from 'react';
import { Query } from 'react-apollo';
import gql from 'graphql-tag';
const GET_POSTS = gql`
query GET_POSTS {
posts {
edges {
node {
id
title
date
}
}
}
}
`;
export default function Posts() {
return (
{({ data, loading, error }) => {
if (loading) return A spinner
;
if (error) return ERROR
;
return (
{data.posts &&
data.posts.posts &&
data.posts.posts.map(post => (
console.log(post)
))}
);
}}
);
};
See the slides
https://ebeldev.github.io/headlesswordpress-slides/
GraphQL + Wordpress with Jason Bahl https://www.youtube.com/watch?v=tYXlHb2eyQw
GraphQL + Wordpress Learning site https://edwincromley.gitbooks.io/wp-graphql/content/
Learn GraphQL howtographql.com
Apollo GraphQL apollographql.com