extractgql
Advanced tools
Comparing version 0.1.8 to 0.1.9
@@ -1,3 +0,3 @@ | ||
/// <reference types="typed-graphql" /> | ||
import { OperationDefinition, Document } from 'graphql'; | ||
/// <reference types="graphql" /> | ||
import { OperationDefinitionNode, DocumentNode } from 'graphql'; | ||
export interface OutputMap { | ||
@@ -7,8 +7,8 @@ [key: string]: TransformedQueryWithId; | ||
export interface TransformedQueryWithId { | ||
transformedQuery: Document; | ||
transformedQuery: DocumentNode; | ||
id: number | string; | ||
} | ||
export declare type QueryTransformer = (doc: Document) => Document; | ||
export declare function applyQueryTransformers(document: Document, queryTransformers?: QueryTransformer[]): Document; | ||
export declare function getQueryKey(definition: OperationDefinition, queryTransformers?: QueryTransformer[]): string; | ||
export declare function getQueryDocumentKey(document: Document, definition: OperationDefinition): string; | ||
export declare type QueryTransformer = (doc: DocumentNode) => DocumentNode; | ||
export declare function applyQueryTransformers(document: DocumentNode, queryTransformers?: QueryTransformer[]): DocumentNode; | ||
export declare function getQueryKey(definition: OperationDefinitionNode, queryTransformers?: QueryTransformer[]): string; | ||
export declare function getQueryDocumentKey(document: DocumentNode, definition: OperationDefinitionNode): string; |
@@ -1,17 +0,17 @@ | ||
/// <reference types="typed-graphql" /> | ||
import { Document, Definition, OperationDefinition, SelectionSet, Selection, FragmentSpread, Field, InlineFragment, FragmentDefinition } from 'graphql'; | ||
export declare function isOperationDefinition(defn: Definition): defn is OperationDefinition; | ||
export declare function isFragmentSpread(selection: Selection): selection is FragmentSpread; | ||
export declare function isFragmentDefinition(selection: Definition): selection is FragmentDefinition; | ||
export declare function isField(selection: Selection): selection is Field; | ||
export declare function isInlineFragment(selection: Selection): selection is InlineFragment; | ||
export declare function isQueryDefinition(defn: Definition): defn is OperationDefinition; | ||
export declare function createDocumentFromQuery(definition: OperationDefinition): { | ||
/// <reference types="graphql" /> | ||
import { DocumentNode, DefinitionNode, OperationDefinitionNode, SelectionSetNode, SelectionNode, FragmentSpreadNode, FieldNode, InlineFragmentNode, FragmentDefinitionNode } from 'graphql'; | ||
export declare function isOperationDefinition(defn: DefinitionNode): defn is OperationDefinitionNode; | ||
export declare function isFragmentSpread(selection: SelectionNode): selection is FragmentSpreadNode; | ||
export declare function isFragmentDefinition(selection: DefinitionNode): selection is FragmentDefinitionNode; | ||
export declare function isField(selection: SelectionNode): selection is FieldNode; | ||
export declare function isInlineFragment(selection: SelectionNode): selection is InlineFragmentNode; | ||
export declare function isQueryDefinition(defn: DefinitionNode): defn is OperationDefinitionNode; | ||
export declare function createDocumentFromQuery(definition: OperationDefinitionNode): { | ||
kind: string; | ||
definitions: OperationDefinition[]; | ||
definitions: OperationDefinitionNode[]; | ||
}; | ||
export declare function getQueryDefinitions(doc: Document): OperationDefinition[]; | ||
export declare function getOperationDefinitions(doc: Document): OperationDefinition[]; | ||
export declare function getFragmentNames(selectionSet: SelectionSet, document: Document): { | ||
export declare function getQueryDefinitions(doc: DocumentNode): OperationDefinitionNode[]; | ||
export declare function getOperationDefinitions(doc: DocumentNode): OperationDefinitionNode[]; | ||
export declare function getFragmentNames(selectionSet: SelectionSetNode, document: DocumentNode): { | ||
[name: string]: number; | ||
}; |
@@ -1,3 +0,3 @@ | ||
/// <reference types="typed-graphql" /> | ||
import { Document, OperationDefinition } from 'graphql'; | ||
/// <reference types="graphql" /> | ||
import { DocumentNode, OperationDefinitionNode } from 'graphql'; | ||
import { OutputMap, QueryTransformer } from './common'; | ||
@@ -18,10 +18,10 @@ export declare class ExtractGQL { | ||
addQueryTransformer(queryTransformer: QueryTransformer): void; | ||
applyQueryTransformers(document: Document): Document; | ||
getQueryKey(definition: OperationDefinition): string; | ||
createMapFromDocument(document: Document): OutputMap; | ||
applyQueryTransformers(document: DocumentNode): DocumentNode; | ||
getQueryKey(definition: OperationDefinitionNode): string; | ||
createMapFromDocument(document: DocumentNode): OutputMap; | ||
processGraphQLFile(graphQLFile: string): Promise<OutputMap>; | ||
processInputFile(inputFile: string): Promise<OutputMap>; | ||
processInputPath(inputPath: string): Promise<OutputMap>; | ||
getQueryFragments(document: Document, queryDefinition: OperationDefinition): Document; | ||
trimDocumentForQuery(document: Document, queryDefinition: OperationDefinition): Document; | ||
getQueryFragments(document: DocumentNode, queryDefinition: OperationDefinitionNode): DocumentNode; | ||
trimDocumentForQuery(document: DocumentNode, queryDefinition: OperationDefinitionNode): DocumentNode; | ||
getQueryId(): number; | ||
@@ -28,0 +28,0 @@ writeOutputMap(outputMap: OutputMap, outputFilePath: string): Promise<void>; |
@@ -136,3 +136,4 @@ "use strict"; | ||
retDocument.definitions = document.definitions.filter(function (definition) { | ||
return (extractFromAST_1.isFragmentDefinition(definition) && queryFragmentNames[definition.name.value] === 1); | ||
var definitionName = definition.name; | ||
return (extractFromAST_1.isFragmentDefinition(definition) && queryFragmentNames[definitionName.value] === 1); | ||
}); | ||
@@ -148,3 +149,4 @@ return retDocument; | ||
retDocument.definitions = document.definitions.filter(function (definition) { | ||
return ((extractFromAST_1.isFragmentDefinition(definition) && queryFragmentNames[definition.name.value] === 1) | ||
var name = definition.name; | ||
return ((extractFromAST_1.isFragmentDefinition(definition) && queryFragmentNames[name.value] === 1) | ||
|| definition === queryDefinition); | ||
@@ -151,0 +153,0 @@ }); |
{ | ||
"name": "extractgql", | ||
"version": "0.1.8", | ||
"version": "0.1.9", | ||
"description": "A build tool for GraphQL projects.", | ||
@@ -40,3 +40,2 @@ "main": "lib/src/index.js", | ||
"tslint": "^3.15.1", | ||
"typed-graphql": "^1.0.2", | ||
"typescript": "^2.0.3", | ||
@@ -48,3 +47,4 @@ "whatwg-fetch": "^2.0.0", | ||
"@types/express": "^4.0.34", | ||
"apollo-client": "^0.5.6", | ||
"@types/graphql": "^0.8.5", | ||
"apollo-client": "^0.6", | ||
"fetch-mock": "^5.5.0", | ||
@@ -51,0 +51,0 @@ "isomorphic-fetch": "^2.2.1" |
@@ -7,4 +7,4 @@ // A set of things that are common across the CLI tool and the stuff that runs on the client/server | ||
import { | ||
OperationDefinition, | ||
Document, | ||
OperationDefinitionNode, | ||
DocumentNode, | ||
print, | ||
@@ -19,13 +19,13 @@ } from 'graphql'; | ||
export interface TransformedQueryWithId { | ||
transformedQuery: Document; | ||
transformedQuery: DocumentNode; | ||
id: number | string; | ||
} | ||
export type QueryTransformer = (doc: Document) => Document; | ||
export type QueryTransformer = (doc: DocumentNode) => DocumentNode; | ||
// Apply queryTransformers to a query document. | ||
export function applyQueryTransformers( | ||
document: Document, | ||
document: DocumentNode, | ||
queryTransformers: QueryTransformer[] = [] | ||
): Document { | ||
): DocumentNode { | ||
let currentDocument = document; | ||
@@ -42,6 +42,6 @@ queryTransformers.forEach((transformer) => { | ||
export function getQueryKey( | ||
definition: OperationDefinition, | ||
definition: OperationDefinitionNode, | ||
queryTransformers: QueryTransformer[] = [], | ||
): string { | ||
const wrappingDocument: Document = { | ||
const wrappingDocument: DocumentNode = { | ||
kind: 'Document', | ||
@@ -60,4 +60,7 @@ definitions: [ definition ], | ||
// before making it a document key. | ||
export function getQueryDocumentKey(document: Document, definition: OperationDefinition): string { | ||
export function getQueryDocumentKey( | ||
document: DocumentNode, | ||
definition: OperationDefinitionNode | ||
): string { | ||
return print(this.applyQueryTransformers(this.trimDocumentForQuery(document, definition))); | ||
} |
import { | ||
Document, | ||
Definition, | ||
OperationDefinition, | ||
SelectionSet, | ||
Selection, | ||
FragmentSpread, | ||
Field, | ||
InlineFragment, | ||
FragmentDefinition, | ||
DocumentNode, | ||
DefinitionNode, | ||
OperationDefinitionNode, | ||
SelectionSetNode, | ||
SelectionNode, | ||
FragmentSpreadNode, | ||
FieldNode, | ||
InlineFragmentNode, | ||
FragmentDefinitionNode, | ||
} from 'graphql'; | ||
@@ -16,3 +16,3 @@ | ||
// Checks if a given GraphQL definition is an operation definition (i.e. either query or mutation). | ||
export function isOperationDefinition(defn: Definition): defn is OperationDefinition { | ||
export function isOperationDefinition(defn: DefinitionNode): defn is OperationDefinitionNode { | ||
return (defn.kind === 'OperationDefinition'); | ||
@@ -22,3 +22,3 @@ } | ||
// Checks if a given GraphQL selection is a FragmentSpread. | ||
export function isFragmentSpread(selection: Selection): selection is FragmentSpread { | ||
export function isFragmentSpread(selection: SelectionNode): selection is FragmentSpreadNode { | ||
return (selection.kind === 'FragmentSpread'); | ||
@@ -28,3 +28,3 @@ } | ||
// Checks if a given GraphQL definition is a FragmentDefinition. | ||
export function isFragmentDefinition(selection: Definition): selection is FragmentDefinition { | ||
export function isFragmentDefinition(selection: DefinitionNode): selection is FragmentDefinitionNode { | ||
return (selection.kind === 'FragmentDefinition'); | ||
@@ -34,3 +34,3 @@ } | ||
// Checks if a given GraphQL selection is a Field. | ||
export function isField(selection: Selection): selection is Field { | ||
export function isField(selection: SelectionNode): selection is FieldNode { | ||
return (selection.kind === 'Field'); | ||
@@ -40,7 +40,7 @@ } | ||
// Checks if a given GraphQL selection is an InlineFragment. | ||
export function isInlineFragment(selection: Selection): selection is InlineFragment { | ||
export function isInlineFragment(selection: SelectionNode): selection is InlineFragmentNode { | ||
return (selection.kind === 'InlineFragment'); | ||
} | ||
export function isQueryDefinition(defn: Definition): defn is OperationDefinition { | ||
export function isQueryDefinition(defn: DefinitionNode): defn is OperationDefinitionNode { | ||
return (isOperationDefinition(defn) && defn.operation === 'query'); | ||
@@ -50,3 +50,3 @@ } | ||
// Creates a query document out of a single query operation definition. | ||
export function createDocumentFromQuery(definition: OperationDefinition) { | ||
export function createDocumentFromQuery(definition: OperationDefinitionNode) { | ||
return { | ||
@@ -59,4 +59,4 @@ kind: 'Document', | ||
// Get query definitions from query document. | ||
export function getQueryDefinitions(doc: Document): OperationDefinition[] { | ||
const queryDefinitions: OperationDefinition[] = []; | ||
export function getQueryDefinitions(doc: DocumentNode): OperationDefinitionNode[] { | ||
const queryDefinitions: OperationDefinitionNode[] = []; | ||
doc.definitions.forEach((definition) => { | ||
@@ -70,4 +70,4 @@ if (isQueryDefinition(definition)) { | ||
export function getOperationDefinitions(doc: Document): OperationDefinition[] { | ||
return doc.definitions.filter(isOperationDefinition) as OperationDefinition[]; | ||
export function getOperationDefinitions(doc: DocumentNode): OperationDefinitionNode[] { | ||
return doc.definitions.filter(isOperationDefinition) as OperationDefinitionNode[]; | ||
} | ||
@@ -78,3 +78,3 @@ | ||
// the name of fragment to the integer "1" to support O(1) lookups. | ||
export function getFragmentNames(selectionSet: SelectionSet, document: Document): { | ||
export function getFragmentNames(selectionSet: SelectionSetNode, document: DocumentNode): { | ||
[name: string]: number, | ||
@@ -87,3 +87,3 @@ } { | ||
// Construct a map going from the name of a fragment to the definition of the fragment. | ||
const fragmentDefinitions: { [name: string]: FragmentDefinition } = {}; | ||
const fragmentDefinitions: { [name: string]: FragmentDefinitionNode } = {}; | ||
document.definitions.forEach((definition) => { | ||
@@ -90,0 +90,0 @@ if (isFragmentDefinition(definition)) { |
@@ -8,6 +8,7 @@ // This file implements the extractgql CLI tool. | ||
parse, | ||
Document, | ||
OperationDefinition, | ||
DocumentNode, | ||
OperationDefinitionNode, | ||
FragmentDefinitionNode, | ||
print, | ||
Definition, | ||
DefinitionNode, | ||
} from 'graphql'; | ||
@@ -103,3 +104,3 @@ | ||
// Applies this.queryTransformers to a query document. | ||
public applyQueryTransformers(document: Document) { | ||
public applyQueryTransformers(document: DocumentNode) { | ||
return applyQueryTransformers(document, this.queryTransformers); | ||
@@ -110,3 +111,3 @@ } | ||
// query transformers and returns a serialization of the query. | ||
public getQueryKey(definition: OperationDefinition): string { | ||
public getQueryKey(definition: OperationDefinitionNode): string { | ||
return getQueryKey(definition, this.queryTransformers); | ||
@@ -117,3 +118,3 @@ } | ||
// queries, mutations and fragments. | ||
public createMapFromDocument(document: Document): OutputMap { | ||
public createMapFromDocument(document: DocumentNode): OutputMap { | ||
const transformedDocument = this.applyQueryTransformers(document); | ||
@@ -202,10 +203,11 @@ const queryDefinitions = getOperationDefinitions(transformedDocument); | ||
// only those fragments. | ||
public getQueryFragments(document: Document, queryDefinition: OperationDefinition): Document { | ||
public getQueryFragments(document: DocumentNode, queryDefinition: OperationDefinitionNode): DocumentNode { | ||
const queryFragmentNames = getFragmentNames(queryDefinition.selectionSet, document); | ||
const retDocument: Document = { | ||
const retDocument: DocumentNode = { | ||
kind: 'Document', | ||
definitions: [], | ||
}; | ||
retDocument.definitions = document.definitions.filter((definition: Definition) => { | ||
return (isFragmentDefinition(definition) && queryFragmentNames[definition.name.value] === 1); | ||
retDocument.definitions = document.definitions.filter((definition: DefinitionNode) => { | ||
const definitionName = (definition as (FragmentDefinitionNode | OperationDefinitionNode)).name; | ||
return (isFragmentDefinition(definition) && queryFragmentNames[definitionName.value] === 1); | ||
}); | ||
@@ -220,10 +222,11 @@ return retDocument; | ||
// may be mutated. | ||
public trimDocumentForQuery(document: Document, queryDefinition: OperationDefinition): Document { | ||
public trimDocumentForQuery(document: DocumentNode, queryDefinition: OperationDefinitionNode): DocumentNode { | ||
const queryFragmentNames = getFragmentNames(queryDefinition.selectionSet, document); | ||
const retDocument: Document = { | ||
const retDocument: DocumentNode = { | ||
kind: 'Document', | ||
definitions: [], | ||
}; | ||
retDocument.definitions = document.definitions.filter((definition: Definition) => { | ||
return ((isFragmentDefinition(definition) && queryFragmentNames[definition.name.value] === 1) | ||
retDocument.definitions = document.definitions.filter((definition: DefinitionNode) => { | ||
const name = (definition as (OperationDefinitionNode | FragmentDefinitionNode)).name; | ||
return ((isFragmentDefinition(definition) && queryFragmentNames[name.value] === 1) | ||
|| definition === queryDefinition); | ||
@@ -230,0 +233,0 @@ }); |
import { | ||
Field, | ||
SelectionSet, | ||
Document, | ||
FieldNode, | ||
SelectionSetNode, | ||
DocumentNode, | ||
print, | ||
@@ -26,3 +26,3 @@ } from 'graphql'; | ||
// along with some of the other AST-related stuff implemented for apollo-client. | ||
const TYPENAME_FIELD: Field = { | ||
const TYPENAME_FIELD: FieldNode = { | ||
kind: 'Field', | ||
@@ -37,4 +37,4 @@ alias: null, | ||
// Query transformer that adds a `__typename` field to every level of the document. | ||
export const addTypenameTransformer: QueryTransformer = (document: Document) => { | ||
const docClone: Document = _.cloneDeep(document); | ||
export const addTypenameTransformer: QueryTransformer = (document: DocumentNode) => { | ||
const docClone: DocumentNode = _.cloneDeep(document); | ||
docClone.definitions.forEach((definition) => { | ||
@@ -50,3 +50,3 @@ if (isOperationDefinition(definition) || isFragmentDefinition(definition)) { | ||
function addTypenameToSelectionSet( | ||
selectionSet: SelectionSet, | ||
selectionSet: SelectionSetNode, | ||
isRoot = false, | ||
@@ -57,3 +57,3 @@ ) { | ||
const alreadyHasThisField = selectionSet.selections.some((selection) => { | ||
return isField(selection) && (selection as Field).name.value === '__typename'; | ||
return isField(selection) && (selection as FieldNode).name.value === '__typename'; | ||
}); | ||
@@ -60,0 +60,0 @@ |
@@ -23,4 +23,4 @@ import * as chai from 'chai'; | ||
print, | ||
OperationDefinition, | ||
Document, | ||
OperationDefinitionNode, | ||
DocumentNode, | ||
} from 'graphql'; | ||
@@ -316,3 +316,3 @@ | ||
`; | ||
const newDocument: Document = gql` | ||
const newDocument: DocumentNode = gql` | ||
query { | ||
@@ -324,4 +324,4 @@ person { | ||
`; | ||
const newQueryDef = newDocument.definitions[0] as OperationDefinition; | ||
const queryTransformer = (queryDoc: Document) => { | ||
const newQueryDef = newDocument.definitions[0] as OperationDefinitionNode; | ||
const queryTransformer = (queryDoc: DocumentNode) => { | ||
return newDocument; | ||
@@ -328,0 +328,0 @@ }; |
@@ -5,3 +5,3 @@ import * as chai from 'chai'; | ||
import { | ||
GraphQLResult, | ||
ExecutionResult, | ||
} from 'graphql'; | ||
@@ -191,3 +191,3 @@ | ||
before(() => { | ||
fetchMock.post(uri, (url: string, opts: Object): GraphQLResult => { | ||
fetchMock.post(uri, (url: string, opts: Object): ExecutionResult => { | ||
const receivedObject = JSON.parse((opts as RequestInit).body.toString()); | ||
@@ -194,0 +194,0 @@ if (_.isEqual(receivedObject, simpleQueryRequest)) { |
@@ -8,3 +8,3 @@ import * as chai from 'chai'; | ||
print, | ||
Document, | ||
DocumentNode, | ||
} from 'graphql'; | ||
@@ -22,3 +22,3 @@ | ||
describe('typename query transformer', () => { | ||
const assertTransform = (inputQuery: Document, expected: Document) => { | ||
const assertTransform = (inputQuery: DocumentNode, expected: DocumentNode) => { | ||
assert.equal( | ||
@@ -25,0 +25,0 @@ print(addTypenameTransformer(inputQuery)), |
@@ -7,3 +7,3 @@ import * as chai from 'chai'; | ||
import { | ||
Document, | ||
DocumentNode, | ||
print, | ||
@@ -42,3 +42,3 @@ } from 'graphql'; | ||
id: '1', | ||
query: undefined as Document, | ||
query: undefined as DocumentNode, | ||
}; | ||
@@ -45,0 +45,0 @@ const expectedQuery = gql` |
@@ -1,3 +0,1 @@ | ||
/// <reference types="typed-graphql" /> | ||
/* | ||
@@ -7,3 +5,3 @@ GRAPHQL | ||
declare module 'graphql-tag/parser' { | ||
import { Source, ParseOptions, Document } from 'graphql'; | ||
import { Source, ParseOptions, DocumentNode } from 'graphql'; | ||
// XXX figure out how to directly export this method | ||
@@ -23,2 +21,2 @@ function parse( | ||
export = deepAssign; | ||
} | ||
} |
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
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
183458
16
3362
5
- Removedtyped-graphql@^1.0.2
- Removedtyped-graphql@1.0.2(transitive)