Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

@mizchi 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