Introducing WordExpress Schema — A Sequelized Package for Connecting to WordPress Database

Ramsay Lanier
nclud
Published in
2 min readJan 26, 2016

WordExpress Schema on GitHub.

Maybe you’ve been following my progress on WordExpress.io, an open-source project that aims to replace PHP with Javascript in WordPress development but using Node, Express, React, GraphQL and Relay.

If you haven’t, the introduction is a good place to start.

Yesterday I posted the third article in an ongoing series about the project. The article discussed using Sequelize to connect to a WordPress database, and then building GraphQL queries around it.

Welp, I decided to build an NPM package that took care of the entire “connecting to Wordpress” thing. It’s called WordExpress Schema, because it will eventually contain the GraphQL schema as well; right now it just provides an easy connection to a WordPress database, which in turn gives you access to some Sequelize Models and queries. Here’s how it works.

import { WordExpressDatabase } from 'wordexpress-schema';
import { publicSettings, privateSettings } from '../settings/settings';

/*
Example settings object:
publicSettings: {
uploads: "http://wordexpress.s3.amazonaws.com/",
amazonS3: true
},
privateSettings: {
wp_prefix: "wp_",
database: {
name: "wpexpress_dev",
username: "root",
password: "",
host: "127.0.0.1"
}
}
*/

const connectionDetails = {
name: privateSettings.database.name,
username: privateSettings.database.username,
password: privateSettings.database.password,
host: privateSettings.database.host,
amazonS3: publicSettings.amazonS3,
uploadDirectory: publicSettings.uploads
}

const Database = new WordExpressDatabase(connectionDetails);
const ConnQueries = Database.queries;
export default ConnQueries;

Aaaaaaand here is how you can use this with GraphQL. Note: the package is GraphQL agnostic right now, so you can use it for lots of things.

const GraphQLUser = new GraphQLObjectType({
name: "User",
fields: {
id: {type: new GraphQLNonNull(GraphQLID)} ,
settings: {
type: GraphQLSetting,
resolve: ()=>{
return publicSettings
}
},
posts: {
type: PostsConnection,
args: {
post_type: {
type: GraphQLString,
defaultValue: 'post'
},
...connectionArgs
},
resolve(root, args) {
return connectionFromPromisedArray( ConnQueries.getPosts(args), args );
}
},
page: {
type: GraphQLPost,
args:{
post_name:{ type: GraphQLString },
},
resolve(root, args){
return ConnQueries.getPostByName(args.post_name);
}
},
menus: {
type: GraphQLMenu,
args: {
name: { type: GraphQLString }
},
resolve(root, args) {
return ConnQueries.getMenu(args.name);
}
},
postmeta: {
type: PostmetaConnection,
args: {
post_id: {
type: GraphQLInt
},
...connectionArgs
},
resolve(root, args){
return ConnQueries.getPostmeta(args.post_id)
}
}
}
})

const GraphQLRoot = new GraphQLObjectType({
name: 'Root',
fields: {
viewer: {
type: GraphQLUser,
resolve: () => {
return ConnQueries.getViewer();
}
}
}
});

const WordExpressSchema = new GraphQLSchema({
query: GraphQLRoot
});
export default WordExpressSchema;

Read the documentation for details.

Ramsay Lanier is a Sr. Developer at nclud, a provocative digital agency in Washington, D.C. You can find him on twitter, github, or at &pizza.

--

--

Ramsay Lanier
nclud
Writer for

I’m a senior web developer at @Novettasol and senior dad-joke developer at home.