-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
df6d2ee
commit d960dee
Showing
8 changed files
with
114 additions
and
118 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
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
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
File renamed without changes.
42 changes: 42 additions & 0 deletions
42
apps/lana-chat/lib/graphql/generated/codegen-query-template.js
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,42 @@ | ||
module.exports = { | ||
plugin: (_, documents) => { | ||
const imports = `import { getClient } from "../../client";`; | ||
|
||
const functions = documents | ||
.filter((doc) => doc.document?.definitions?.length > 0) | ||
.map((doc) => { | ||
const operation = doc.document.definitions.find( | ||
(def) => def.kind === "OperationDefinition" && def.name | ||
); | ||
|
||
if (operation?.name?.value) { | ||
const name = operation.name.value; | ||
const camelCaseName = name.charAt(0).toLowerCase() + name.slice(1); | ||
return functionTemplate(camelCaseName, name); | ||
} | ||
return ""; | ||
}) | ||
.filter(Boolean) | ||
.join("\n"); | ||
|
||
return [imports, functions].join("\n\n"); | ||
}, | ||
}; | ||
|
||
const functionTemplate = (functionName, operationName) => { | ||
return ` | ||
export async function ${functionName}(variables: ${operationName}QueryVariables) { | ||
try { | ||
const response = await getClient().query<${operationName}Query, ${operationName}QueryVariables>({ | ||
query: ${operationName}Document, | ||
variables, | ||
}); | ||
return response; | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
return { error: error.message }; | ||
} | ||
return { error: "An unknown error occurred" }; | ||
} | ||
}`; | ||
}; |
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