mirror of
https://github.com/ritz078/transform.git
synced 2026-11-26 10:42:54 +03:00
use transform-graphql-type-annotations
This commit is contained in:
1 parent
e2bd9b00ed
commit
875816ee86
5 files changed
+15
-67
No files matched your search
+1
-2
@@ -18,7 +18,6 @@
|
||||
"license": "MIT",
|
||||
"dependencies": {
|
||||
"@tsuyoshiwada/htmltojsx": "^0.0.4",
|
||||
"apollo-codegen": "0.17.0-alpha.1",
|
||||
"babel-plugin-json-to-proptypes": "^0.1.0",
|
||||
"babel-standalone": "^6.26.0",
|
||||
"classnames": "^2.2.5",
|
||||
@@ -26,7 +25,6 @@
|
||||
"codemirror-graphql": "^0.6.11",
|
||||
"copy-text-to-clipboard": "^1.0.3",
|
||||
"generate-schema": "^2.5.1",
|
||||
"graphql": "^0.10.5",
|
||||
"is-in-browser": "^1.1.3",
|
||||
"js-beautify": "^1.6.14",
|
||||
"json2dts": "^0.0.2",
|
||||
@@ -42,6 +40,7 @@
|
||||
"react-github-corner": "^1.0.0",
|
||||
"stringify-object": "^3.2.0",
|
||||
"transform-css-to-js": "^0.1.1",
|
||||
"transform-graphql-type-annotations": "^0.1.1",
|
||||
"unfetch": "^3.0.0",
|
||||
"xml-js": "^1.4.1"
|
||||
},
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import React, { PureComponent } from "react";
|
||||
import { generateSource } from "apollo-codegen/lib/flow/codeGeneration";
|
||||
import Layout from "../components/Layout";
|
||||
import ConversionPanel from "../components/ConversionPanel";
|
||||
import { dummySchema, dummyQuery, transform } from "../utils/graphql-schema";
|
||||
import { dummySchema, dummyQuery } from "../utils/graphql-schema";
|
||||
import transform from "transform-graphql-type-annotations"
|
||||
|
||||
export default class Json2Ts extends PureComponent {
|
||||
getTransformedValue = (newValue, splitValue) => {
|
||||
return transform(newValue, splitValue, generateSource);
|
||||
return transform(newValue, splitValue);
|
||||
};
|
||||
|
||||
render() {
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import React, { PureComponent } from "react";
|
||||
import { generateSource } from "apollo-codegen/lib/typescript/codeGeneration";
|
||||
import transform from "transform-graphql-type-annotations";
|
||||
import Layout from "../components/Layout";
|
||||
import ConversionPanel from "../components/ConversionPanel";
|
||||
import { dummySchema, dummyQuery, transform } from "../utils/graphql-schema";
|
||||
import { dummySchema, dummyQuery } from "../utils/graphql-schema";
|
||||
|
||||
export default class Json2Ts extends PureComponent {
|
||||
getTransformedValue = (newValue, splitValue) => {
|
||||
return transform(newValue, splitValue, generateSource);
|
||||
return transform(newValue, splitValue, 'typescript');
|
||||
};
|
||||
|
||||
render() {
|
||||
|
||||
+1
-59
@@ -1,7 +1,3 @@
|
||||
import { compileToLegacyIR } from "apollo-codegen/lib/compiler/legacyIR";
|
||||
import CodeGenerator from "apollo-codegen/lib/utilities/CodeGenerator";
|
||||
import { parse, buildClientSchema } from "graphql";
|
||||
|
||||
const x = {
|
||||
data: {
|
||||
__schema: {
|
||||
@@ -2188,58 +2184,4 @@ fragment heroFriends on Character {
|
||||
friends {
|
||||
name
|
||||
}
|
||||
}`;
|
||||
|
||||
export function setup(schema) {
|
||||
const context = {
|
||||
schema: schema,
|
||||
operations: {},
|
||||
fragments: {},
|
||||
typesUsed: {}
|
||||
};
|
||||
|
||||
const generator = new CodeGenerator(context);
|
||||
|
||||
const compileFromSource = source => {
|
||||
const document = parse(source);
|
||||
const context = compileToLegacyIR(schema, document, {
|
||||
mergeInFieldsFromFragmentSpreads: true,
|
||||
addTypename: true
|
||||
});
|
||||
generator.context = context;
|
||||
return context;
|
||||
};
|
||||
|
||||
const addFragment = fragment => {
|
||||
generator.context.fragments[fragment.fragmentName] = fragment;
|
||||
};
|
||||
|
||||
return { generator, compileFromSource, addFragment };
|
||||
}
|
||||
|
||||
export function transform(newValue, splitValue, generateSource) {
|
||||
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"
|
||||
);
|
||||
}
|
||||
|
||||
try {
|
||||
const schema = buildClientSchema(
|
||||
schemaData.data ? schemaData.data : schemaData
|
||||
);
|
||||
|
||||
const { compileFromSource } = setup(schema);
|
||||
|
||||
const context = compileFromSource(splitValue);
|
||||
|
||||
return generateSource(context);
|
||||
} catch (e) {
|
||||
throw new Error(e.message);
|
||||
}
|
||||
}
|
||||
}`;
|
||||
@@ -4954,6 +4954,13 @@ transform-css-to-js@^0.1.1:
|
||||
lodash "^4.17.4"
|
||||
stringify-object "^3.2.0"
|
||||
|
||||
transform-graphql-type-annotations@^0.1.1:
|
||||
version "0.1.1"
|
||||
resolved "https://registry.yarnpkg.com/transform-graphql-type-annotations/-/transform-graphql-type-annotations-0.1.1.tgz#6ac353afcf693c1f038a8b4acbc8a0098a4bba6d"
|
||||
dependencies:
|
||||
apollo-codegen "0.17.0-alpha.1"
|
||||
graphql "^0.10.5"
|
||||
|
||||
trim-right@^1.0.1:
|
||||
version "1.0.1"
|
||||
resolved "https://registry.yarnpkg.com/trim-right/-/trim-right-1.0.1.tgz#cb2e1203067e0c8de1f614094b9fe45704ea6003"
|
||||
|
||||
Reference in new issue
Block a user