createResource

Create a resource.


const resource = await drupal.createResource<T = JsonApiResource>(
type,
body,
options?: {
params,
withAuth,
deserialize,
}
): Promise<T>
  • type: string
    • Required
    • The resource type. Example: node--article, taxonomy_term--tags, or block_content--basic.
  • body: JsonApiCreateResourceBody
    • Required
    • The body payload with data.
    • interface JsonApiCreateResourceBody {
      data: {
      type?: string
      attributes?: Record<string, unknown>
      relationships?: Record<string, JsonApiResourceBodyRelationship>
      }
      }
  • options
    • Optional
    • params: JsonApiParams: JSON:API params such as filter, fields, include or sort.
    • withAuth: boolean | DrupalClientAuth:
      • Set the authentication method to use. See the authentication docs.
      • Set to true to use the authentication method configured on the client.
    • deserialize: boolean: Set to false to return the raw JSON:API response.

Examples

  • Create a node--page resource.
const page = await drupal.createResource("node--page", {
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
})
  • Get a node--article resource with a taxonomy term.
const article = await drupal.createResource("node--article", {
data: {
attributes: {
title: "Title of Article",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
relationships: {
field_category: {
data: {
type: "taxonomy_term--category",
id: "28ab9f26-927d-4e33-9510-b59a7ccdafe6",
},
},
},
},
})
  • Using filters.
const page = await drupal.createResource(
"node--page",
{
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
},
{
params: {
"fields[node--page]": "title,path",
},
}
)

TypeScript

  • Using DrupalNode for a node entity type.
import { DrupalNode } from "next-drupal"
const page = await drupal.createResource<DrupalNode>("node--page", {
data: {
attributes: {
title: "Page Title",
body: {
value: "<p>Content of body field</p>",
format: "full_html",
},
},
},
})

See the TypeScript docs for more built-in types.