diff --git a/package.json b/package.json index 7a6e2333..b104e09a 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "graphql-faker", - "version": "1.7.6", + "version": "1.7.6-damian0", "description": "Mock or extend your GraphQL API with faked data. No coding required", "main": "dist/index.js", "bin": "dist/index.js", @@ -11,7 +11,7 @@ "build:editor": "cd src/editor && yarn && npm run build && cd -", "build:typescript": "tsc", "copy:graphql": "cp src/*.graphql dist/", - "copy:editor": "mkdir -p dist/editor && cp src/editor/*.{html,js,css,svg} dist/editor", + "copy:editor": "mkdir -p dist/editor && bash -c \" cp src/editor/*.{html,js,css,svg} dist/editor\"", "build:all": "npm run build:editor && npm run build:typescript && npm run copy:graphql && npm run copy:editor" }, "repository": { @@ -30,12 +30,12 @@ "@types/express": "4.16.0", "@types/express-graphql": "0.6.1", "@types/faker": "4.1.3", - "@types/graphql": "0.11.4", + "@types/graphql": "14.0.0", "@types/lodash": "4.14.116", "@types/yargs": "11.1.1", - "nodemon": "1.18.3", + "nodemon": "1.18.4", "ts-node": "7.0.1", - "typescript": "3.0.1" + "typescript": "3.0.3" }, "dependencies": { "body-parser": "1.18.3", @@ -45,11 +45,12 @@ "express": "4.16.3", "express-graphql": "github:apis-guru/express-graphql#rootValue_dist", "faker": "4.1.0", - "graphql": "github:apis-guru/graphql-js#directives-fork-dist", - "lodash": "4.17.10", + "graphql": "*", + "lodash": "4.17.11", "moment": "2.22.2", "node-fetch": "2.2.0", "opn": "5.3.0", - "yargs": "12.0.1" + "updates": "^4.3.0", + "yargs": "12.0.2" } } diff --git a/src/editor/GraphQLEditor/GraphQLEditor.tsx b/src/editor/GraphQLEditor/GraphQLEditor.tsx index 2d14c657..8ef1ecba 100644 --- a/src/editor/GraphQLEditor/GraphQLEditor.tsx +++ b/src/editor/GraphQLEditor/GraphQLEditor.tsx @@ -57,9 +57,11 @@ export default class GraphQLEditor extends React.Component { foldGutter: { minFoldSize: 4, }, + /* lint: { schema, }, + */ hintOptions: { schema, closeOnUnfocus: false, @@ -122,7 +124,7 @@ export default class GraphQLEditor extends React.Component { const { value, schema } = this.props; if (schema != prevProps.schema) { - this.editor.options.lint.schema = schema; + //this.editor.options.lint.schema = schema; this.editor.options.hintOptions.schema = schema; this.editor.options.info.schema = schema; this.editor.options.jump.schema = schema; diff --git a/src/editor/index.tsx b/src/editor/index.tsx index 212d415d..46f0cb5c 100644 --- a/src/editor/index.tsx +++ b/src/editor/index.tsx @@ -5,7 +5,7 @@ import 'graphiql/graphiql.css'; import * as classNames from 'classnames'; import * as GraphiQL from 'graphiql'; -import { buildSchema, extendSchema, GraphQLSchema, parse } from 'graphql'; +import { buildSchema, extendSchema, GraphQLSchema, parse} from 'graphql'; import * as fetch from 'isomorphic-fetch'; import * as fakeIDL from 'raw-loader!../fake_definition.graphql'; import * as React from 'react'; @@ -88,22 +88,42 @@ class FakeEditor extends React.Component { return this.fetcher('/user-idl', { method: 'post', headers: { 'Content-Type': 'text/plain' }, - body: idl, + body: idl + }); } buildSchema(value) { + var concatFake = true; + var concat=''; + //Check if extend another fake schema to avoid add the fakeIDL + if (value.includes("fake__Locale")){ + concatFake=false; + } if (this.state.proxiedSchemaIDL) { - let schema = buildSchema(this.state.proxiedSchemaIDL + '\n' + fakeIDL); + if (concatFake && this.state.proxiedSchemaIDL.includes("fake__Locale")){ + concatFake=false; + } + concat = this.state.proxiedSchemaIDL; + if (concatFake){ + concat += '\n' + fakeIDL; + } + let schema = buildSchema(concat); return extendSchema(schema, parse(value)); + } else { - return buildSchema(value + '\n' + fakeIDL); + concat = value; + if (concatFake){ + concat += '\n' + fakeIDL; + } + return buildSchema(concat); } } updateIdl(value, noError = false) { try { const schema = this.buildSchema(value); + this.setState(prevState => ({ ...prevState, schema, @@ -130,7 +150,6 @@ class FakeEditor extends React.Component { if (!dirty) return; if (!this.updateIdl(value)) return; - this.postIDL(value).then(res => { if (res.ok) { this.setStatus('Saved!', 2000); @@ -161,7 +180,9 @@ class FakeEditor extends React.Component { let dirtySchema = null as GraphQLSchema | null; try { dirtySchema = this.buildSchema(val); - } catch(_) { } + } catch(_) { + + } this.setState(prevState => ({ ...prevState, diff --git a/src/editor/package.json b/src/editor/package.json index 5a7302b7..804924ec 100644 --- a/src/editor/package.json +++ b/src/editor/package.json @@ -6,10 +6,10 @@ }, "dependencies": { "classnames": "^2.2.5", - "codemirror": "5.23.0", - "codemirror-graphql": "github:apis-guru/codemirror-graphql#directives-fork-dist", - "graphiql": "^0.9.3", - "graphql": "^0.9.0", + "codemirror": "^5.40.0", + "codemirror-graphql": "^0.6.12", + "graphiql": "*", + "graphql": "^14.0.2", "isomorphic-fetch": "^2.2.1", "marked": "^0.3.6", "react": "^15.4.2", diff --git a/src/editor/yarn.lock b/src/editor/yarn.lock index c1b0d6cb..ecc11363 100644 --- a/src/editor/yarn.lock +++ b/src/editor/yarn.lock @@ -1211,21 +1211,21 @@ code-point-at@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/code-point-at/-/code-point-at-1.1.0.tgz#0d070b4d043a5bea33a2f1a40e2edb3d9a4ccf77" -codemirror-graphql@^0.6.3: +codemirror-graphql@^0.6.12, codemirror-graphql@^0.6.3: version "0.6.12" resolved "https://registry.yarnpkg.com/codemirror-graphql/-/codemirror-graphql-0.6.12.tgz#91a273fe5188857524a30221d06e645b4ca41f00" dependencies: graphql-language-service-interface "^1.0.16" graphql-language-service-parser "^0.1.14" -"codemirror-graphql@github:apis-guru/codemirror-graphql#directives-fork-dist": - version "0.6.3" - resolved "https://codeload.github.com/apis-guru/codemirror-graphql/tar.gz/518625d1d5229cffe1fceba62258683a03d62854" - codemirror@5.23.0: version "5.23.0" resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.23.0.tgz#ac9b6b2e163a79e915b44cf0f43cd115cf6982dc" +codemirror@^5.40.0: + version "5.40.0" + resolved "https://registry.yarnpkg.com/codemirror/-/codemirror-5.40.0.tgz#2f5ed47366e514f41349ba0fe34daaa39be4e257" + collection-visit@^1.0.0: version "1.0.0" resolved "https://registry.yarnpkg.com/collection-visit/-/collection-visit-1.0.0.tgz#4bc0373c164bc3291b4d368c829cf1a80a59dca0" @@ -2475,11 +2475,11 @@ graphql@^0.12.3: dependencies: iterall "1.1.3" -graphql@^0.9.0: - version "0.9.6" - resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.9.6.tgz#514421e9d225c29dfc8fd305459abae58815ef2c" +graphql@^14.0.2: + version "14.0.2" + resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.0.2.tgz#7dded337a4c3fd2d075692323384034b357f5650" dependencies: - iterall "^1.0.0" + iterall "^1.2.2" handle-thing@^1.2.5: version "1.2.5" @@ -3035,10 +3035,14 @@ isstream@~0.1.2: version "0.1.2" resolved "https://registry.yarnpkg.com/isstream/-/isstream-0.1.2.tgz#47e63f7af55afa6f92e1500e690eb8b8529c099a" -iterall@1.1.3, iterall@^1.0.0: +iterall@1.1.3: version "1.1.3" resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.1.3.tgz#1cbbff96204056dde6656e2ed2e2226d0e6d72c9" +iterall@^1.2.2: + version "1.2.2" + resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7" + js-base64@^2.1.9: version "2.4.0" resolved "https://registry.yarnpkg.com/js-base64/-/js-base64-2.4.0.tgz#9e566fee624751a1d720c966cd6226d29d4025aa" diff --git a/src/fake_schema.ts b/src/fake_schema.ts index 8fbfe053..cf8cee44 100644 --- a/src/fake_schema.ts +++ b/src/fake_schema.ts @@ -146,7 +146,7 @@ export function fakeSchema(schema: GraphQLSchema) { function abstractTypeResolver(type:GraphQLAbstractType) { const possibleTypes = schema.getPossibleTypes(type); - return () => ({__typename: getRandomItem(possibleTypes)}); + return () => ({__typename: getRandomItem(possibleTypes as Array)}); } } diff --git a/src/index.ts b/src/index.ts index 9a6b1083..ad52ba43 100644 --- a/src/index.ts +++ b/src/index.ts @@ -160,21 +160,29 @@ if (argv.e) { }); } -function buildServerSchema(idl) { - var ast = concatAST([parse(idl), fakeDefinitionAST]); +function buildServerSchema(idl, concatFake=true) { + var ast =parse(idl); + if (concatFake){ + var ast = concatAST([parse(idl), fakeDefinitionAST]); + } return buildASTSchema(ast); } function runServer(schemaIDL: Source, extensionIDL: Source, optionsCB) { const app = express(); - + //Check if extend another fake schema to avoid add the fakeIDL + var concatAST=true; + if (schemaIDL.body.includes("fake__Locale")){ + concatAST=false; + } if (extensionIDL) { - const schema = buildServerSchema(schemaIDL); + const schema = buildServerSchema(schemaIDL, concatAST); extensionIDL.body = extensionIDL.body.replace('', schema.getQueryType().name); } app.options('/graphql', cors(corsOptions)) app.use('/graphql', cors(corsOptions), graphqlHTTP(req => { - const schema = buildServerSchema(schemaIDL); + + const schema = buildServerSchema(schemaIDL, concatAST); const forwardHeaders = pick(req.headers, forwardHeaderNames); return { ...optionsCB(schema, extensionIDL, forwardHeaders), diff --git a/src/proxy.ts b/src/proxy.ts index a88c6f62..0e5855b7 100644 --- a/src/proxy.ts +++ b/src/proxy.ts @@ -37,11 +37,14 @@ export function proxyMiddleware(url, headers) { return getIntrospection().then(introspection => { const introspectionSchema = buildClientSchema(introspection.data); const introspectionIDL = printSchema(introspectionSchema); - return [introspectionIDL, (serverSchema, extensionIDL, forwardHeaders) => { const extensionAST = parse(extensionIDL); + const extensionFields = getExtensionFields(extensionAST); + const schema = extendSchema(serverSchema, extensionAST); + + fakeSchema(schema); //TODO: proxy extensions @@ -104,12 +107,22 @@ function buildRootValue(response) { function getExtensionFields(extensionAST) { const extensionFields = {}; - (extensionAST.definitions || []).forEach(def => { - if (def.kind !== Kind.TYPE_EXTENSION_DEFINITION) - return; - const typeName = def.definition.name.value; + extensionAST.definitions.forEach(def => { + var extensionsDefintions = [ + Kind.SCALAR_TYPE_EXTENSION, + Kind.SCHEMA_EXTENSION, + Kind.OPERATION_TYPE_DEFINITION, + Kind.ENUM_TYPE_EXTENSION, + Kind.INPUT_OBJECT_TYPE_EXTENSION, + Kind.INTERFACE_TYPE_EXTENSION, + Kind.OBJECT_TYPE_EXTENSION, + Kind.UNION_TYPE_EXTENSION + ] + if (extensionsDefintions.includes(def.kind)){ + const typeName = def.name.value; // FIXME: handle multiple extends of the same type - extensionFields[typeName] = def.definition.fields.map(field => field.name.value); + extensionFields[typeName] = def.fields.map(field => field.name.value); + } }); return extensionFields; }