extractgql
Advanced tools
Comparing version 0.1.6 to 0.1.7
/// <reference types="typed-graphql" /> | ||
import { Document, OperationDefinition } from 'graphql'; | ||
export interface OutputMap { | ||
[key: string]: TransformedQueryWithId; | ||
} | ||
export interface TransformedQueryWithId { | ||
transformedQuery: Document; | ||
id: number | string; | ||
} | ||
export declare type QueryTransformer = (doc: Document) => Document; | ||
import { OutputMap, QueryTransformer } from './common'; | ||
export declare class ExtractGQL { | ||
@@ -24,4 +17,5 @@ inputFilePath: string; | ||
}); | ||
addQueryTransformer(queryTransformer: QueryTransformer): void; | ||
applyQueryTransformers(document: Document): Document; | ||
addQueryTransformer(queryTransformer: QueryTransformer): void; | ||
getQueryKey(definition: OperationDefinition): string; | ||
createMapFromDocument(document: Document): OutputMap; | ||
@@ -33,4 +27,2 @@ processGraphQLFile(graphQLFile: string): Promise<OutputMap>; | ||
trimDocumentForQuery(document: Document, queryDefinition: OperationDefinition): Document; | ||
getQueryDocumentKey(document: Document, definition: OperationDefinition): string; | ||
getQueryKey(definition: OperationDefinition): string; | ||
getQueryId(): number; | ||
@@ -37,0 +29,0 @@ writeOutputMap(outputMap: OutputMap, outputFilePath: string): Promise<void>; |
@@ -6,2 +6,3 @@ "use strict"; | ||
var extractFromAST_1 = require("./extractFromAST"); | ||
var common_1 = require("./common"); | ||
var queryTransformers_1 = require("./queryTransformers"); | ||
@@ -49,12 +50,11 @@ var _ = require("lodash"); | ||
}; | ||
ExtractGQL.prototype.applyQueryTransformers = function (document) { | ||
var currentDocument = document; | ||
this.queryTransformers.forEach(function (transformer) { | ||
currentDocument = transformer(currentDocument); | ||
}); | ||
return currentDocument; | ||
}; | ||
ExtractGQL.prototype.addQueryTransformer = function (queryTransformer) { | ||
this.queryTransformers.push(queryTransformer); | ||
}; | ||
ExtractGQL.prototype.applyQueryTransformers = function (document) { | ||
return common_1.applyQueryTransformers(document, this.queryTransformers); | ||
}; | ||
ExtractGQL.prototype.getQueryKey = function (definition) { | ||
return common_1.getQueryKey(definition, this.queryTransformers); | ||
}; | ||
ExtractGQL.prototype.createMapFromDocument = function (document) { | ||
@@ -154,12 +154,2 @@ var _this = this; | ||
}; | ||
ExtractGQL.prototype.getQueryDocumentKey = function (document, definition) { | ||
return graphql_1.print(this.applyQueryTransformers(this.trimDocumentForQuery(document, definition))); | ||
}; | ||
ExtractGQL.prototype.getQueryKey = function (definition) { | ||
var wrappingDocument = { | ||
kind: 'Document', | ||
definitions: [definition], | ||
}; | ||
return graphql_1.print(this.applyQueryTransformers(wrappingDocument).definitions[0]); | ||
}; | ||
ExtractGQL.prototype.getQueryId = function () { | ||
@@ -166,0 +156,0 @@ this.queryId += 1; |
/// <reference types="isomorphic-fetch" /> | ||
import { HTTPFetchNetworkInterface, RequestAndOptions } from 'apollo-client/transport/networkInterface'; | ||
import { OutputMap } from '../ExtractGQL'; | ||
import { OutputMap } from '../common'; | ||
export declare class PersistedQueryNetworkInterface extends HTTPFetchNetworkInterface { | ||
@@ -5,0 +5,0 @@ queryMap: OutputMap; |
@@ -8,4 +8,4 @@ "use strict"; | ||
var networkInterface_1 = require("apollo-client/transport/networkInterface"); | ||
var common_1 = require("../common"); | ||
var extractFromAST_1 = require("../extractFromAST"); | ||
var ExtractGQL_1 = require("../ExtractGQL"); | ||
var _ = require('lodash'); | ||
@@ -30,4 +30,3 @@ var PersistedQueryNetworkInterface = (function (_super) { | ||
} | ||
var mockEGQL = new ExtractGQL_1.ExtractGQL({ inputFilePath: '' }); | ||
var queryKey = mockEGQL.getQueryKey(queryDefinitions[0]); | ||
var queryKey = common_1.getQueryKey(queryDefinitions[0]); | ||
if (!this.queryMap[queryKey]) { | ||
@@ -34,0 +33,0 @@ throw new Error('Could not find query inside query map.'); |
@@ -1,2 +0,2 @@ | ||
import { QueryTransformer } from './ExtractGQL'; | ||
import { QueryTransformer } from './common'; | ||
export declare const addTypenameTransformer: QueryTransformer; |
@@ -22,2 +22,4 @@ "use strict"; | ||
resolve(middleware); | ||
}).catch(function (err) { | ||
reject(err); | ||
}); | ||
@@ -24,0 +26,0 @@ }); |
@@ -10,3 +10,3 @@ "use strict"; | ||
var queryMapPath = 'test/fixtures/extracted_queries.json'; | ||
it('it should resolve the returned promise with something', function (done) { | ||
it('should resolve the returned promise with something', function (done) { | ||
serverUtil_1.createPersistedQueryMiddleware('test/fixtures/extracted_queries.json').then(function (middleware) { | ||
@@ -17,2 +17,10 @@ assert(middleware); | ||
}); | ||
it('should reject the promise in the event that the file cannot be found', function (done) { | ||
serverUtil_1.createPersistedQueryMiddleware('made-up-file-path.json').then(function (middleware) { | ||
done(new Error('Returned a middleware instance when it should not have.')); | ||
}).catch(function (error) { | ||
assert.include(error.message, 'ENOENT'); | ||
done(); | ||
}); | ||
}); | ||
it('returned middleware should be able to pass along query id', function (done) { | ||
@@ -19,0 +27,0 @@ var req = { |
{ | ||
"name": "extractgql", | ||
"version": "0.1.6", | ||
"version": "0.1.7", | ||
"description": "A build tool for GraphQL projects.", | ||
@@ -5,0 +5,0 @@ "main": "lib/src/index.js", |
@@ -0,1 +1,3 @@ | ||
// This file implements the extractgql CLI tool. | ||
import fs = require('fs'); | ||
@@ -20,2 +22,10 @@ import path = require('path'); | ||
import { | ||
getQueryKey, | ||
applyQueryTransformers, | ||
TransformedQueryWithId, | ||
OutputMap, | ||
QueryTransformer, | ||
} from './common'; | ||
import { | ||
addTypenameTransformer, | ||
@@ -26,14 +36,2 @@ } from './queryTransformers'; | ||
// A map from a key (id or a hash) to a GraphQL document. | ||
export interface OutputMap { | ||
[key: string]: TransformedQueryWithId; | ||
} | ||
export interface TransformedQueryWithId { | ||
transformedQuery: Document; | ||
id: number | string; | ||
} | ||
export type QueryTransformer = (doc: Document) => Document; | ||
export class ExtractGQL { | ||
@@ -100,11 +98,2 @@ public inputFilePath: string; | ||
// Apply this.queryTransformers to a query definition. | ||
public applyQueryTransformers(document: Document): Document { | ||
let currentDocument = document; | ||
this.queryTransformers.forEach((transformer) => { | ||
currentDocument = transformer(currentDocument); | ||
}); | ||
return currentDocument; | ||
} | ||
// Add a query transformer to the end of the list of query transformers. | ||
@@ -115,2 +104,13 @@ public addQueryTransformer(queryTransformer: QueryTransformer) { | ||
// Applies this.queryTransformers to a query document. | ||
public applyQueryTransformers(document: Document) { | ||
return applyQueryTransformers(document, this.queryTransformers); | ||
} | ||
// Just calls getQueryKey with this.queryTransformers as its set of | ||
// query transformers and returns a serialization of the query. | ||
public getQueryKey(definition: OperationDefinition): string { | ||
return getQueryKey(definition, this.queryTransformers); | ||
} | ||
// Create an OutputMap from a GraphQL document that may contain | ||
@@ -232,21 +232,2 @@ // queries, mutations and fragments. | ||
// Returns a key for a query in a document definition. Should include exactly one query and a set | ||
// of fragments that the query references. Currently just uses GraphQL printing as a serialization | ||
// mechanism; may use hashes or ids in the future. Also applies query transformers to the document | ||
// before making it a document key. | ||
public getQueryDocumentKey(document: Document, definition: OperationDefinition): string { | ||
return print(this.applyQueryTransformers(this.trimDocumentForQuery(document, definition))); | ||
} | ||
// Returns a key for a query operation definition. Currently just uses GraphQL printing as a | ||
// serialization mechanism; may use hashes or ids in the future. Also applies the query | ||
// transformers to the query definition before returning the key. | ||
public getQueryKey(definition: OperationDefinition): string { | ||
const wrappingDocument: Document = { | ||
kind: 'Document', | ||
definitions: [ definition ], | ||
}; | ||
return print(this.applyQueryTransformers(wrappingDocument).definitions[0]); | ||
} | ||
// Returns unique query ids. | ||
@@ -253,0 +234,0 @@ public getQueryId() { |
@@ -6,5 +6,8 @@ import { | ||
import { OutputMap } from '../ExtractGQL'; | ||
import { | ||
getQueryKey, | ||
OutputMap, | ||
} from '../common'; | ||
import { getQueryDefinitions } from '../extractFromAST'; | ||
import { ExtractGQL } from '../ExtractGQL'; | ||
const _ = require('lodash'); | ||
@@ -44,5 +47,4 @@ | ||
} | ||
const mockEGQL = new ExtractGQL({ inputFilePath: '' }); | ||
const queryKey = mockEGQL.getQueryKey(queryDefinitions[0]); | ||
const queryKey = getQueryKey(queryDefinitions[0]); | ||
if (!this.queryMap[queryKey]) { | ||
@@ -49,0 +51,0 @@ throw new Error('Could not find query inside query map.'); |
@@ -10,3 +10,3 @@ import { | ||
QueryTransformer, | ||
} from './ExtractGQL'; | ||
} from './common'; | ||
@@ -13,0 +13,0 @@ import { |
@@ -60,4 +60,6 @@ // This file provides some basic utilities for servers that allow the server | ||
resolve(middleware); | ||
}).catch((err: Error) => { | ||
reject(err); | ||
}); | ||
}); | ||
} |
@@ -10,6 +10,9 @@ import * as chai from 'chai'; | ||
ExtractGQL, | ||
OutputMap, | ||
} from '../src/ExtractGQL'; | ||
import { | ||
OutputMap, | ||
} from '../src/common'; | ||
import { | ||
addTypenameTransformer, | ||
@@ -16,0 +19,0 @@ } from '../src/queryTransformers'; |
@@ -22,3 +22,3 @@ import * as chai from 'chai'; | ||
const queryMapPath = 'test/fixtures/extracted_queries.json'; | ||
it('it should resolve the returned promise with something', (done) => { | ||
it('should resolve the returned promise with something', (done) => { | ||
createPersistedQueryMiddleware('test/fixtures/extracted_queries.json').then((middleware) => { | ||
@@ -30,2 +30,11 @@ assert(middleware); | ||
it('should reject the promise in the event that the file cannot be found', (done) => { | ||
createPersistedQueryMiddleware('made-up-file-path.json').then((middleware) => { | ||
done(new Error('Returned a middleware instance when it should not have.')); | ||
}).catch((error) => { | ||
assert.include(error.message, 'ENOENT'); | ||
done(); | ||
}); | ||
}); | ||
it('returned middleware should be able to pass along query id', (done) => { | ||
@@ -32,0 +41,0 @@ const req = { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
65
3275
180553