content-graphql

1.0.7 • Public • Published

content-graphql

Generates an executable graphql schema from a list of masks.

Usage

npm install content-graphql graphql --save
const pg = require("content-graphql");
const { graphql } = require("graphql");
 
// create a prismic repository with a mask named `Page`
const masks = {
  Page: {
    Main: {
      uid: {
        type: "UID",
        config: {
          label: "slug"
        }
      }
    }
  }
};
const schema = pg(masks);
 
const query = `{
    searchOne{
        uid
    }
}`;
 
graphql(schema, query, null, {
  repository: "your_repository_name"
}).then(response => {
  console.log(response);
  // { data: { searchOne: { uid: 'ezf' } } }
});

Api

pg(masks, {
 
  contentUrl: (context, ref) => {
    // this function allows you overwrite the url to query data
    return `http://a-json-endpoint-somewhere-with-content`;
  },
 
  contentRefUrl: (context) => {
    // this function allows you overwrite the url to find the current ref
    return `http://a-json-endpoint-somewhere-with-the-ref-data`;
  },
 
  // [OPTIONAL]
  transform: (doc, context) => {
    // this function will run before returning a document
    // it lets you transform the prismic document before it is processed by graphql
    return doSomething(doc);
  },
 
  // [OPTIONAL]
  transformTextField: (field, context) => {
    // This function will be called on textual fields. `Text` or `StructuredText`
    return doSomething(field);
  },
 
  // [OPTIONAL]
  linkResolver: doc => {
    if (doc.type === "article") return `/article/${doc.uid}`;
    return "/";
  }
 
 
  // [OPTIONAL]
  typeResolver: doc => {
    // lets you use the same graphql type for multiple masks
    if (doc.type === "employer-article") return `article`;
    if (doc.type === "employee-article") return `article`;
    if (doc.type === "admin-article") return `article`;
    return "/";
  }
});

Debug

Setting the environment variable CONTENT_GRAPHQL_DEBUG=true will log queries that are actually sent to prismic.

Assumptions

- slice names must be globaly unique across masks. (A global Slice interface is declared)

Structured Text

StructuredText will be transformed to HTML using PrismicDOM.RichText.asHtml.

Link Resolving

Link resolving can be done server side by providing the optional linkResolver option.

TODO

[] add more tests
[] handle image sizes

Changelog

1.0.6

  • add DEBUG_CONTENT_GRAPHQL env variable to enable logging

1.0.5

  • add CONTENT_GRAPHQL_API_CACHE env variable

1.0.4

  • fix language issue with local file content

1.0.3

  • fix cache issue
  • fix lang query issue

1.0.2

  • only cache ref for 5 seconds in production mode

1.0.1

All queries are now resolved by filtering a single json document instead af sending all queries to prismic.

  • removed query argument from search and searchOne
  • added type argument to search and searchOne.
  • added uid argument to search and searchOne

BREAKING CHANGE

# before
search(query: "[[:d = at(my.document.type, \"article\")]]) {
  title
}
 
# after
search(type: "article") {
  title
}
 
 
 
# before
search(query: "[[:d = at(my.article.uid, \"my-artcile\")]]) {
  title
}
 
# after
search(type: "article", uid: "article") {
  title
}
 

0.0.19

  • add ability to assign multiple masks to the same graphqltype see typeResolver feature
  • add searchOneAsType

0.0.18

  • add id

0.0.17

  • fix missing url field on root values (was only working with Link fields)

0.0.16

  • handle null values in link

0.0.14

  • change preTransform for transformTextField
  • allow link resolving serverside
  • render structured text server side

0.0.13

  • fix overfetching issue
  • preTransform function
  • add debug env

0.0.11

fix issue with linked documents

0.0.11

fix issue with linked documents

0.0.10

fix another transform bug

0.0.9

transform bug fix

0.0.8

transform

0.0.7

Language support

0.0.6

Handle masks with "-"

0.0.5

handle unknown slices

0.0.4

handle lower case mask names

0.0.1

initial version

Readme

Keywords

none

Package Sidebar

Install

npm i content-graphql

Weekly Downloads

1

Version

1.0.7

License

none

Unpacked Size

177 kB

Total Files

12

Last publish

Collaborators

  • jbelfodil
  • romaric.laurent
  • mizak926