Skip to content

Commit

Permalink
support graphql mutations (#189)
Browse files Browse the repository at this point in the history
* support GraphQL mutations

* Release 1.0.0-edge.15
  • Loading branch information
vernaillen authored Aug 25, 2024
1 parent 391ac3b commit e29f02e
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 8 deletions.
5 changes: 5 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ export default createConfigForNuxt({
commaDangle: 'never',
braceStyle: '1tbs'
}
},
dirs: {
src: [
'./playground'
]
}
},
{
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wpnuxt/core",
"version": "1.0.0-edge.14",
"version": "1.0.0-edge.15",
"description": "WPNuxt",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export async function prepareContext(ctx: WPNuxtContext) {
const fnExp = (q: WPNuxtQuery, typed = false) => {
const functionName = fnName(q.name)
if (!typed) {
return `export const ${functionName} = (params) => useWPContent('${q.name}', [${q.nodes?.map(n => `'${n}'`).join(',')}], false, params)`
return `export const ${functionName} = (params) => useWPContent('${q.operation}', '${q.name}', [${q.nodes?.map(n => `'${n}'`).join(',')}], false, params)`
}
const fragmentSuffix = q.fragments?.length && q.nodes?.includes('nodes') ? '[]' : ''
const fragments = q.fragments?.length ? q.fragments.map(f => `${f}Fragment${fragmentSuffix}`).join(' | ') : 'any'
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/components/StagingBanner.vue
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<script setup lang="ts">
import { OperationTypeNode } from 'graphql'
import { useWPUri } from '../composables/useWPUri'
import { useWPContent } from '../composables'
import WPNuxtLogo from './WPNuxtLogo.vue'
Expand Down Expand Up @@ -27,7 +28,7 @@ if (uri.startsWith('/')) {
if (uri.endsWith('/')) {
uri = uri.substring(0, uri.length - 1)
}
const { data: post } = await useWPContent('NodeByUri', ['nodeByUri'], false, { uri: uri })
const { data: post } = await useWPContent(OperationTypeNode.QUERY, 'NodeByUri', ['nodeByUri'], false, { uri: uri })
if (import.meta.client) {
document.body.style.marginBottom = '40px'
Expand Down
3 changes: 2 additions & 1 deletion src/runtime/composables/usePrevNextPost.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { OperationTypeNode } from 'graphql'
import { useWPContent } from './useWPContent'

const _usePrevNextPost = async (currentPostSlug: string) => {
Expand All @@ -14,7 +15,7 @@ const _usePrevNextPost = async (currentPostSlug: string) => {
}

const getAllPosts = async () => {
const { data: allPosts } = await useWPContent('Posts', ['posts', 'nodes'], false)
const { data: allPosts } = await useWPContent(OperationTypeNode.QUERY, 'Posts', ['posts', 'nodes'], false)
if (allPosts) {
return {
slugs: allPosts?.map((post) => {
Expand Down
4 changes: 3 additions & 1 deletion src/runtime/composables/useWPContent.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import type { FetchError } from 'ofetch'
import type { OperationTypeNode } from 'graphql'
import { getRelativeImagePath } from '../util/images'
import type { AsyncData } from '#app'

const _useWPContent = async <T>(queryName: string, nodes: string[], fixImagePaths: boolean, params?: T) => {
const _useWPContent = async <T>(operation: OperationTypeNode, queryName: string, nodes: string[], fixImagePaths: boolean, params?: T) => {
const { data, error } = await $fetch<AsyncData<T, FetchError | null>>('/api/wpContent', {
method: 'POST',
body: {
operation,
queryName,
params
}
Expand Down
6 changes: 4 additions & 2 deletions src/runtime/server/api/wpContent.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ export default defineCachedEventHandler(async (event: H3Event) => {
'The request must contain a queryName'
)
}
return $fetch('/api/graphql_middleware/query/' + body.queryName, {
params: buildRequestParams(body.params),
return $fetch('/api/graphql_middleware/' + (body.operation || 'query') + '/' + body.queryName, {
method: body.operation === 'mutation' ? 'POST' : 'GET',
params: body.operation === 'query' ? buildRequestParams(body.params) : undefined,
body: body.operation === 'mutation' ? body.params : undefined,
headers: {
Authorization: `Bearer ${event.context.accessToken}`
}
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,5 @@ export type WPNuxtQuery = {
nodes?: string[]
fragments: string[]
params: Record<string, string>
operation: OperationTypeNode
}
3 changes: 2 additions & 1 deletion src/useParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ const _parseDoc = async (doc: string): Promise<WPNuxtQuery[]> => {
name: operationDefinition.name.value.trim(),
nodes: [],
fragments: [],
params: {}
params: {},
operation: operationDefinition.operation
}
processSelections(operationDefinition.selectionSet.selections, 0, query)
return query
Expand Down

0 comments on commit e29f02e

Please sign in to comment.