Skip to content

Instantly share code, notes, and snippets.

@mizchi
Last active March 24, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mizchi/fbf3224567a31871f4f1ce6ead1e2ad7 to your computer and use it in GitHub Desktop.
Save mizchi/fbf3224567a31871f4f1ce6ead1e2ad7 to your computer and use it in GitHub Desktop.
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);
@mizchi
Copy link
Author

mizchi commented Mar 24, 2021

Result

export type Maybe<T> = T | null;
export type Exact<T extends { [key: string]: unknown }> = { [K in keyof T]: T[K] };
export type MakeOptional<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]?: Maybe<T[SubKey]> };
export type MakeMaybe<T, K extends keyof T> = Omit<T, K> & { [SubKey in K]: Maybe<T[SubKey]> };
/** All built-in and custom scalars, mapped to their actual values */
export type Scalars = {
  ID: string;
  String: string;
  Boolean: boolean;
  Int: number;
  Float: number;
};

export type Query = {
  __typename?: 'Query';
  userData: UserData;
  template: ExpandedTemplate;
};


export type QueryUserDataArgs = {
  q: UserDataQuery;
};


export type QueryTemplateArgs = {
  source: Scalars['String'];
  args: Array<UserDataQuery>;
};

export type ExpandedTemplate = {
  __typename?: 'ExpandedTemplate';
  result: Scalars['String'];
};

export type UserData = {
  __typename?: 'UserData';
  key: Scalars['String'];
  result: Scalars['String'];
};

export type UserDataQuery = {
  key: Scalars['String'];
};

export type ExampleQueryVariables = Exact<{ [key: string]: never; }>;


export type ExampleQuery = (
  { __typename?: 'Query' }
  & { template: (
    { __typename?: 'ExpandedTemplate' }
    & Pick<ExpandedTemplate, 'result'>
  ), aaa: (
    { __typename?: 'UserData' }
    & Pick<UserData, 'result'>
  ), bbb: (
    { __typename?: 'UserData' }
    & Pick<UserData, 'result'>
  ) }
);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment