Skip to content

Commit

Permalink
Staging -> Prod [ Add __typename to gql response ] (#14)
Browse files Browse the repository at this point in the history
* Fix Github Action Workflow (#7)

* Fix Github Action Workflow

* Debug Publish Script

* verify Build

* Fix Build

* Use Updated Versions

* Update the flag (#9)

* Change Flag To Folder

* Fix Typo

* Update version

* fix JS compilation-issue (#11)

* Update

* Add __typename to response (#13)

* Staging -> Prod [Fix JS Compilation Issue] (#12)

* Fix Github Action Workflow (#7)

* Fix Github Action Workflow

* Debug Publish Script

* verify Build

* Fix Build

* Use Updated Versions

* Update the flag (#9)

* Change Flag To Folder

* Fix Typo

* Update version

* fix JS compilation-issue (#11)

* Update

* Add __typename to response

* Fix test
  • Loading branch information
TosinJs authored Mar 18, 2024
1 parent 2a472eb commit 74ef12b
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gandalf-network/eyeofsauron",
"version": "1.0.2",
"version": "1.0.3",
"description": "",
"main": "dist/index.js",
"bin": {
Expand Down
1 change: 1 addition & 0 deletions src/scripts/generateCodeFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ async function generateCodeFromSchema(folder: string) {
config: {
rawRequest: true,
useTypeImports: true,
addTypeName: true
},
ignoreNoDocuments: true,
};
Expand Down
41 changes: 39 additions & 2 deletions src/scripts/generateOperationsFromSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import {
GraphQLSchema,
OperationTypeNode,
print,
Kind,
SelectionSetNode,
OperationDefinitionNode,
} from 'graphql';

import { buildOperationNodeForField } from '@graphql-tools/utils';
Expand All @@ -29,7 +32,7 @@ async function getSchemaFromUrl(url: string): Promise<GraphQLSchema> {
}

// Generate the query and mutations operations from the schema
async function generateOperationsFromSchema(url: string): Promise<string> {
async function generateOperationsFromSchema(url: string = "http://localhost:8080/public/gql"): Promise<string> {
const schema: GraphQLSchema = await getSchemaFromUrl(url);

const operationsDictionary = {
Expand All @@ -48,14 +51,48 @@ async function generateOperationsFromSchema(url: string): Promise<string> {
field,
});

documentString += print(operationAST);
const modifiedOperationAST: OperationDefinitionNode = {
...operationAST,
selectionSet: addTypenameToSelectionSet(operationAST.selectionSet),
};

documentString += print(modifiedOperationAST);
});
});

const mod = removeTypeNamesFromAST(documentString);
return mod;
}


// This function adds __typename to all selection sets recursively
function addTypenameToSelectionSet(selectionSet: SelectionSetNode): SelectionSetNode {
const selections = selectionSet.selections.map(selection => {
// If the selection is a field that itself has a selection set,
// recurse into that selection set.
if (selection.kind === Kind.FIELD && selection.selectionSet) {
return {
...selection,
selectionSet: addTypenameToSelectionSet(selection.selectionSet),
};
}
return selection;
});

// Add __typename to the selections if it's not already present
if (!selections.some(selection => selection.kind === Kind.FIELD && selection.name.value === '__typename')) {
selections.push({
kind: Kind.FIELD,
name: { kind: Kind.NAME, value: '__typename' },
});
}

return {
...selectionSet,
selections,
};
}

// The operations generated by generateOperationsFromSchema were of the form functionName_query
// This changes them to just functionName
function removeTypeNamesFromAST(ast: string) {
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/templates/errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class GandalfError extends Error {
}

function handleErrors(error: any): GandalfError {
if (error.response?.errors) {
if (error.response?.errors[0].extensions.code) {
switch (error.response.errors[0].extensions.code) {
case "INVALID_SIGNATURE":
return new GandalfError(
Expand Down
2 changes: 1 addition & 1 deletion src/scripts/tests/generateCodeFromSchema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe('generateCodeFromSchema', () => {
presetConfig: { gqlTagName: 'gql' },
},
},
config: { rawRequest: true, useTypeImports: true },
config: { rawRequest: true, useTypeImports: true, addTypeName: true },
ignoreNoDocuments: true,
};

Expand Down

0 comments on commit 74ef12b

Please sign in to comment.