Skip to content

Instantly share code, notes, and snippets.

@ddemaree
Last active November 9, 2022 00:59
Show Gist options
  • Save ddemaree/9c58744fecfd0dd5652b2803c7e11cf1 to your computer and use it in GitHub Desktop.
Save ddemaree/9c58744fecfd0dd5652b2803c7e11cf1 to your computer and use it in GitHub Desktop.
Example of WordPress–Sanity import
// First, we must import the schema creator
import createSchema from "part:@sanity/base/schema-creator";
// Then import schema types from any plugins that might expose them
import schemaTypes from "all:part:@sanity/base/schema-type";
// We import object and document schemas
import blockContent from "./blockContent";
import category from "./category";
import post from "./post";
import author from "./author";
import gallery from "./gallery";
import twitter from "./twitter.jsx";
import richText from "./richText";
import quote from "./quote";
export const schemaData = {
// We name our schema
name: "default",
// Then proceed to concatenate our document type
// to the ones provided by any plugins that are installed
types: schemaTypes.concat([
// The following are document types which will appear
// in the studio.
post,
author,
category,
gallery,
twitter,
richText,
quote,
// When added to this list, object types can be used as
// { type: 'typename' } in other document schemas
blockContent,
]),
};
// Then we give our schema to the builder and provide the result to Sanity
export default createSchema(schemaData);
import _ from "lodash";
import { ApolloClient, HttpLink, InMemoryCache, gql } from "@apollo/client";
// import { DateTime } from "luxon";
import fetch from "cross-fetch";
import Schema from "@sanity/schema";
import blockTools from "@sanity/block-tools";
import { schemaData } from "../schemas/schema";
const WP_JWT = process.env.WP_JWT;
const cache = new InMemoryCache();
const link = new HttpLink({
uri: process.env.GRAPHQL_ENDPOINT,
headers: {
Authorization: `Bearer ${WP_JWT}`,
},
fetch,
});
const client = new ApolloClient({
cache,
link,
});
(async () => {
const query = gql`
query PostQuery {
post(idType: SLUG, id: "elden-ring") {
content(format: RENDERED)
dateGmt
excerpt
modifiedGmt
slug
title(format: RENDERED)
}
}
`;
const res = await client.query({ query });
const { content } = res.data;
const schema = Schema.compile(schemaData);
console.log({ content, schema });
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment