Replies: 1 comment
-
in theory, something like this could work, i stiched this together quickly, but not yet tested it: import { configToSchema } from '@payloadcms/graphql'
import type { GraphQLError } from 'graphql'
import { graphql } from 'graphql'
import type { TypedDocumentNode } from '@graphql-typed-document-node/core'
import type { Maybe } from 'graphql/jsutils/Maybe'
let schemaPromise: Promise<ReturnType<typeof configToSchema>> | undefined = undefined
const buildSchema = async () => {
const config = await import('@payload-config').then((m) => m.default)
const schema = configToSchema(config)
return schema
}
const getSchema = async () => {
if (schemaPromise) {
return await schemaPromise
}
schemaPromise = buildSchema()
return schemaPromise
}
export async function execute<
Result = any,
Variables extends Maybe<{
readonly [variable: string]: unknown
}> = any,
>(
query: TypedDocumentNode<Result, Variables>,
variables: Variables,
): Promise<{
data: Result
errors: ReadonlyArray<GraphQLError>
}> {
const schema = await getSchema()
const result = await graphql({
schema: schema.schema,
source: query,
variableValues: variables,
})
return {
data: result.data as Result,
errors: result.errors ?? [],
}
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
you can actually execute graphql queries without going through network, usually by using
why you want to use this?
string | YourType
union and runtime errors with wrong assumption about the depthOf course this may be worse performance wise then using the local api directly, because it is only as optimized as payloads graphql api when dealing with db optimization. And there is probably some additional serialization going on.
But the developer experience is much better when querying data. You can also use fragments in your components which avoid overfetching data and potentially expose sensitive information
So the question is:
is there a way to get payloads underlying graphql schema? And how to build the context properly?
Beta Was this translation helpful? Give feedback.
All reactions