Skip to content

Commit

Permalink
feat(server): local dev server to return __typename
Browse files Browse the repository at this point in the history
  • Loading branch information
maoosi committed Apr 9, 2023
1 parent 12b1d3d commit 6090eb1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 9 deletions.
28 changes: 19 additions & 9 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { Authorizations } from '../../client/src'
import { queryObject } from './utils/useGraphqlFilter'
import useLambdaIdentity from './utils/useLambdaIdentity'
import useLambdaEvents from './utils/useLambdaEvents'
import { addTypename } from './utils/useGraphqlTypename'

declare global {
// eslint-disable-next-line no-var, vars-on-top
Expand Down Expand Up @@ -67,6 +68,14 @@ export async function createServer({ defaultQuery, lambdaHandler, port, schema,
if (!lambdaHandler?.main)
throw new Error('Handler has no exported function "main".')

const yogaSchema = createSchema({
typeDefs: [
readFileSync(join(__dirname, 'gql/appsync-scalars.gql'), { encoding: 'utf-8' }),
readFileSync(join(__dirname, 'gql/appsync-directives.gql'), { encoding: 'utf-8' }),
schema,
],
})

const useLambdaFunction = (): Plugin => {
return {
async onExecute({ args }) {
Expand Down Expand Up @@ -168,13 +177,20 @@ export async function createServer({ defaultQuery, lambdaHandler, port, schema,
}
})

const queryDocument = gql`${events[0].info.selectionSetGraphQL}`

const filteredResults = queryObject(
gql`${events[0].info.selectionSetGraphQL}`,
queryDocument,
lambdaResults,
{ includeMissingData: true },
)

setResult({ data: filteredResults })
const typedResults = await addTypename(
yogaSchema,
filteredResults,
)

setResult({ data: typedResults })
}
},
}
Expand All @@ -183,13 +199,7 @@ export async function createServer({ defaultQuery, lambdaHandler, port, schema,
}

const yoga = createYoga({
schema: createSchema({
typeDefs: [
readFileSync(join(__dirname, 'gql/appsync-scalars.gql'), { encoding: 'utf-8' }),
readFileSync(join(__dirname, 'gql/appsync-directives.gql'), { encoding: 'utf-8' }),
schema,
],
}),
schema: yogaSchema,
graphiql: {
title: 'Prisma-AppSync',
defaultQuery: defaultQuery ? prettier.format(defaultQuery, { parser: 'graphql' }) : undefined,
Expand Down
23 changes: 23 additions & 0 deletions packages/server/src/utils/useGraphqlTypename.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import type { GraphQLSchema } from 'graphql'
import { _ } from '../../../client/src'

// Function to add '__typename' values to the partial result object
export async function addTypename(
schema: GraphQLSchema,
partialResultObject: Record<string, any>,
) {
return await _.traverseNodes(partialResultObject, async (node) => {
if (node?.key === '__typename') {
const pathsWithoutArrays = node.path?.filter(p => typeof p === 'string')

let fields = schema.getQueryType()?.getFields()?.[pathsWithoutArrays?.[0]]

for (let index = 1; index < pathsWithoutArrays.length - 1; index++)
fields = fields?.type?.ofType?.getFields()?.[pathsWithoutArrays[index]]

if (fields?.type)
node.set(String(fields.type).replace(/[\])}[{(]/g, ''))
}
})
}

0 comments on commit 6090eb1

Please sign in to comment.