mirror of
https://github.com/ritz078/transform.git
synced 2026-11-20 16:22:53 +03:00
Merge pull request #15 from ritz078/3-2-window-support
added graphql transforms and split pane in left
This commit is contained in:
commit
94e6ed0b26
2 files changed
+52
-11
No files matched your search
Whitespace-only changes.
@@ -1,18 +1,59 @@
|
||||
import React, { PureComponent } from "react";
|
||||
import { schemaToInterfaces } from "@gql2ts/from-schema";
|
||||
import { parse, buildClientSchema } from "graphql"
|
||||
import CodeGenerator from "apollo-codegen/lib/utilities/CodeGenerator";
|
||||
import { generateSource } from "apollo-codegen/lib/typescript/codeGeneration"
|
||||
import { compileToIR } from "apollo-codegen/lib/compilation"
|
||||
import Layout from "../components/Layout";
|
||||
import ConversionPanel from "../components/ConversionPanel";
|
||||
import schema from "../utils/graphql-schema";
|
||||
|
||||
export default class Json2Ts extends PureComponent {
|
||||
state = {
|
||||
legacyTS: false
|
||||
const query = `query HeroName {
|
||||
hero {
|
||||
name
|
||||
}
|
||||
}`
|
||||
|
||||
function setup(schema) {
|
||||
const context = {
|
||||
schema: schema,
|
||||
operations: {},
|
||||
fragments: {},
|
||||
typesUsed: {}
|
||||
}
|
||||
|
||||
const generator = new CodeGenerator(context);
|
||||
|
||||
const compileFromSource = (source) => {
|
||||
const document = parse(source);
|
||||
const context = compileToIR(schema, document, {
|
||||
mergeInFieldsFromFragmentSpreads: true,
|
||||
addTypename: true
|
||||
});
|
||||
generator.context = context;
|
||||
return context;
|
||||
};
|
||||
|
||||
getTransformedValue = newValue => {
|
||||
return schemaToInterfaces(JSON.parse(newValue), {
|
||||
legacy: this.state.legacyTS
|
||||
});
|
||||
const addFragment = (fragment) => {
|
||||
generator.context.fragments[fragment.fragmentName] = fragment;
|
||||
};
|
||||
|
||||
return { generator, compileFromSource, addFragment };
|
||||
}
|
||||
|
||||
export default class Json2Ts extends PureComponent {
|
||||
getTransformedValue = (newValue, splitValue) => {
|
||||
if (!splitValue || !newValue) { return }
|
||||
const schemaData = JSON.parse(newValue)
|
||||
|
||||
if (!schemaData.data && !schemaData.__schema) {
|
||||
throw new Error('GraphQL schema file should contain a valid GraphQL introspection query result');
|
||||
}
|
||||
|
||||
const schema = buildClientSchema(schemaData.data ? schemaData.data : schemaData)
|
||||
|
||||
const { compileFromSource } = setup(schema)
|
||||
const context = compileFromSource(splitValue)
|
||||
return generateSource(context)
|
||||
};
|
||||
|
||||
render() {
|
||||
@@ -27,10 +68,10 @@ export default class Json2Ts extends PureComponent {
|
||||
leftMode="typescript"
|
||||
rightMode="typescript"
|
||||
url={this.props.url}
|
||||
onCheckboxChange={(checked, cb) =>
|
||||
this.setState({ legacyTS: checked }, cb)}
|
||||
checkboxText="Support TypeScript 1.x"
|
||||
prettifyRightPanel={false}
|
||||
splitLeft={true}
|
||||
splitTitle={'Query'}
|
||||
splitValue={query}
|
||||
/>
|
||||
</Layout>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user