-
-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(server): local dev server to return __typename
- Loading branch information
Showing
2 changed files
with
42 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, '')) | ||
} | ||
}) | ||
} | ||
|