Last active
March 24, 2021 09:04
-
-
Save mizchi/fbf3224567a31871f4f1ce6ead1e2ad7 to your computer and use it in GitHub Desktop.
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
import { graphql, parse } from "graphql"; | |
import gql from "graphql-tag"; | |
import type { DocumentNode } from "graphql"; | |
const documentNode = gql` | |
type Query { | |
userData(q: UserDataQuery!): UserData! | |
template(source: String!, args: [UserDataQuery!]!): ExpandedTemplate! | |
} | |
type ExpandedTemplate { | |
result: String! | |
} | |
type UserData { | |
key: String! | |
result: String! | |
} | |
input UserDataQuery { | |
key: String! | |
} | |
`; | |
const exampleQuery = ` | |
query example { | |
template(source: "Hello, {{xxx}}", args: [ {key: "xxx"} ]) { | |
result | |
} | |
aaa: userData(q: {key: "aaa"}) { | |
result | |
} | |
bbb: userData(q: {key: "bbb"}) { | |
result | |
} | |
} | |
`; | |
let loaded: null | { | |
core: typeof import("@graphql-codegen/core"); | |
ts: typeof import("@graphql-codegen/typescript"); | |
tsOp: typeof import("@graphql-codegen/typescript-operations"); | |
} = null; | |
async function generateTypesFromGraphql(schema: DocumentNode) { | |
if (!loaded) { | |
// HACK: Codegen touch this for node perf | |
// @ts-ignore | |
globalThis.process = { | |
hrtime() { | |
return 0; | |
}, | |
}; | |
loaded = { | |
core: await import("@graphql-codegen/core"), | |
ts: await import("@graphql-codegen/typescript"), | |
tsOp: await import("@graphql-codegen/typescript-operations"), | |
}; | |
// restore | |
// @ts-ignore | |
delete globalThis.process; | |
} | |
return await loaded.core.codegen({ | |
filename: "schema.graphql", | |
plugins: [ | |
{ | |
typescript: {}, | |
}, | |
{ | |
"typescript-operations": {}, | |
}, | |
], | |
schema, | |
documents: [ | |
{ | |
location: "operation.graphql", | |
document: parse(exampleQuery), | |
}, | |
], | |
config: {}, | |
pluginMap: { | |
typescript: loaded.ts, | |
"typescript-operations": loaded.tsOp, | |
}, | |
}); | |
} | |
async function main() { | |
const ret = await generateTypesFromGraphql(documentNode); | |
console.log(ret); | |
} | |
main().catch(console.error); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Result