extractgql
Advanced tools
Comparing version 0.1.7 to 0.1.8
@@ -14,4 +14,5 @@ /// <reference types="typed-graphql" /> | ||
export declare function getQueryDefinitions(doc: Document): OperationDefinition[]; | ||
export declare function getOperationDefinitions(doc: Document): OperationDefinition[]; | ||
export declare function getFragmentNames(selectionSet: SelectionSet, document: Document): { | ||
[name: string]: number; | ||
}; |
@@ -44,2 +44,6 @@ "use strict"; | ||
exports.getQueryDefinitions = getQueryDefinitions; | ||
function getOperationDefinitions(doc) { | ||
return doc.definitions.filter(isOperationDefinition); | ||
} | ||
exports.getOperationDefinitions = getOperationDefinitions; | ||
function getFragmentNames(selectionSet, document) { | ||
@@ -46,0 +50,0 @@ if (!selectionSet) { |
@@ -61,3 +61,3 @@ "use strict"; | ||
var transformedDocument = this.applyQueryTransformers(document); | ||
var queryDefinitions = extractFromAST_1.getQueryDefinitions(transformedDocument); | ||
var queryDefinitions = extractFromAST_1.getOperationDefinitions(transformedDocument); | ||
var result = {}; | ||
@@ -64,0 +64,0 @@ queryDefinitions.forEach(function (transformedDefinition) { |
@@ -25,7 +25,7 @@ "use strict"; | ||
var queryDocument = request.query; | ||
var queryDefinitions = extractFromAST_1.getQueryDefinitions(queryDocument); | ||
if (queryDefinitions.length !== 1) { | ||
var operationDefinitions = extractFromAST_1.getOperationDefinitions(queryDocument); | ||
if (operationDefinitions.length !== 1) { | ||
throw new Error('Multiple queries in a single document.'); | ||
} | ||
var queryKey = common_1.getQueryKey(queryDefinitions[0]); | ||
var queryKey = common_1.getQueryKey(operationDefinitions[0]); | ||
if (!this.queryMap[queryKey]) { | ||
@@ -32,0 +32,0 @@ throw new Error('Could not find query inside query map.'); |
@@ -60,3 +60,3 @@ "use strict"; | ||
it('should be able to handle a document with no queries', function () { | ||
var document = (_a = ["mutation something { otherThing }"], _a.raw = ["mutation something { otherThing }"], graphql_tag_1.default(_a)); | ||
var document = (_a = ["fragment something on Type { otherThing }"], _a.raw = ["fragment something on Type { otherThing }"], graphql_tag_1.default(_a)); | ||
var map = egql.createMapFromDocument(document); | ||
@@ -144,2 +144,14 @@ assert.deepEqual(map, {}); | ||
}); | ||
it('should be able to handle a document with a mutation', function () { | ||
var myegql = new ExtractGQL_1.ExtractGQL({ inputFilePath: 'empty' }); | ||
var document = (_a = ["\n mutation changeAuthorStuff {\n firstName\n lastName\n }"], _a.raw = ["\n mutation changeAuthorStuff {\n firstName\n lastName\n }"], graphql_tag_1.default(_a)); | ||
var map = myegql.createMapFromDocument(document); | ||
assert.deepEqual(map, (_b = {}, | ||
_b[egql.getQueryKey(document.definitions[0])] = { | ||
transformedQuery: extractFromAST_1.createDocumentFromQuery(document.definitions[0]), | ||
id: 1, | ||
}, | ||
_b)); | ||
var _a, _b; | ||
}); | ||
}); | ||
@@ -146,0 +158,0 @@ describe('queryTransformers', function () { |
@@ -55,3 +55,3 @@ "use strict"; | ||
var egql = new ExtractGQL_1.ExtractGQL({ inputFilePath: 'nothing' }); | ||
var queriesDocument = (_a = ["\n query {\n author {\n firstName\n lastName\n }\n }\n query {\n person {\n ...personDetails\n }\n }\n query {\n house {\n address\n }\n }\n query {\n person(id: $id) {\n name\n }\n }\n query ListOfAuthors {\n author {\n firstName\n lastName\n }\n }\n fragment personDetails on Person {\n firstName\n lastName\n }\n "], _a.raw = ["\n query {\n author {\n firstName\n lastName\n }\n }\n query {\n person {\n ...personDetails\n }\n }\n query {\n house {\n address\n }\n }\n query {\n person(id: $id) {\n name\n }\n }\n query ListOfAuthors {\n author {\n firstName\n lastName\n }\n }\n fragment personDetails on Person {\n firstName\n lastName\n }\n "], graphql_tag_1.default(_a)); | ||
var queriesDocument = (_a = ["\n query {\n author {\n firstName\n lastName\n }\n }\n query {\n person {\n ...personDetails\n }\n }\n query {\n house {\n address\n }\n }\n query {\n person(id: $id) {\n name\n }\n }\n query ListOfAuthors {\n author {\n firstName\n lastName\n }\n }\n mutation changeAuthorStuff {\n firstName\n lastName\n }\n fragment personDetails on Person {\n firstName\n lastName\n }\n "], _a.raw = ["\n query {\n author {\n firstName\n lastName\n }\n }\n query {\n person {\n ...personDetails\n }\n }\n query {\n house {\n address\n }\n }\n query {\n person(id: $id) {\n name\n }\n }\n query ListOfAuthors {\n author {\n firstName\n lastName\n }\n }\n mutation changeAuthorStuff {\n firstName\n lastName\n }\n fragment personDetails on Person {\n firstName\n lastName\n }\n "], graphql_tag_1.default(_a)); | ||
var simpleQueryRequest = { | ||
@@ -104,2 +104,9 @@ id: 1, | ||
}; | ||
var mutationRequest = { | ||
id: 6, | ||
}; | ||
var mutationData = { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}; | ||
var queryMap = egql.createMapFromDocument(queriesDocument); | ||
@@ -129,2 +136,5 @@ var uri = 'http://fake.com/fakegraphql'; | ||
} | ||
else if (_.isEqual(receivedObject, mutationRequest)) { | ||
return { data: mutationData }; | ||
} | ||
else { | ||
@@ -189,2 +199,11 @@ throw new Error('Received unmatched request in mock fetch.'); | ||
}); | ||
it('should also work with mutations', function (done) { | ||
pni.query({ | ||
query: (_a = ["\n mutation changeAuthorStuff {\n firstName\n lastName\n }"], _a.raw = ["\n mutation changeAuthorStuff {\n firstName\n lastName\n }"], graphql_tag_1.default(_a)), | ||
}).then(function (result) { | ||
assert.deepEqual(result.data, mutationData); | ||
done(); | ||
}); | ||
var _a; | ||
}); | ||
var _a; | ||
@@ -191,0 +210,0 @@ }); |
{ | ||
"name": "extractgql", | ||
"version": "0.1.7", | ||
"version": "0.1.8", | ||
"description": "A build tool for GraphQL projects.", | ||
@@ -5,0 +5,0 @@ "main": "lib/src/index.js", |
@@ -52,1 +52,2 @@ # ExtractGQL | ||
- Added command line opt parsing to handle "--add-typename" which allows the user to select the query transformer that adds the typename to each level of the query. It seems unlikely that there'll be tons of query transformers straight off the bat so this seems like a reasonable approach for the timebeing. | ||
- Added handling of the README |
@@ -63,2 +63,6 @@ import { | ||
export function getOperationDefinitions(doc: Document): OperationDefinition[] { | ||
return doc.definitions.filter(isOperationDefinition) as OperationDefinition[]; | ||
} | ||
// Extracts the names of fragments from a SelectionSet recursively, given a document in which | ||
@@ -65,0 +69,0 @@ // each of the fragments defined are given. Returns a map going from |
@@ -15,3 +15,3 @@ // This file implements the extractgql CLI tool. | ||
import { | ||
getQueryDefinitions, | ||
getOperationDefinitions, | ||
getFragmentNames, | ||
@@ -117,3 +117,3 @@ isFragmentDefinition, | ||
const transformedDocument = this.applyQueryTransformers(document); | ||
const queryDefinitions = getQueryDefinitions(transformedDocument); | ||
const queryDefinitions = getOperationDefinitions(transformedDocument); | ||
const result: OutputMap = {}; | ||
@@ -120,0 +120,0 @@ queryDefinitions.forEach((transformedDefinition) => { |
@@ -11,3 +11,3 @@ import { | ||
import { getQueryDefinitions } from '../extractFromAST'; | ||
import { getOperationDefinitions } from '../extractFromAST'; | ||
const _ = require('lodash'); | ||
@@ -43,8 +43,8 @@ | ||
const queryDocument = request.query; | ||
const queryDefinitions = getQueryDefinitions(queryDocument); | ||
if (queryDefinitions.length !== 1) { | ||
const operationDefinitions = getOperationDefinitions(queryDocument); | ||
if (operationDefinitions.length !== 1) { | ||
throw new Error('Multiple queries in a single document.'); | ||
} | ||
const queryKey = getQueryKey(queryDefinitions[0]); | ||
const queryKey = getQueryKey(operationDefinitions[0]); | ||
if (!this.queryMap[queryKey]) { | ||
@@ -51,0 +51,0 @@ throw new Error('Could not find query inside query map.'); |
@@ -99,3 +99,3 @@ import * as chai from 'chai'; | ||
it('should be able to handle a document with no queries', () => { | ||
const document = gql`mutation something { otherThing }`; | ||
const document = gql`fragment something on Type { otherThing }`; | ||
const map = egql.createMapFromDocument(document); | ||
@@ -288,2 +288,18 @@ assert.deepEqual(map, {}); | ||
}); | ||
it('should be able to handle a document with a mutation', () => { | ||
const myegql = new ExtractGQL({ inputFilePath: 'empty' }); | ||
const document = gql` | ||
mutation changeAuthorStuff { | ||
firstName | ||
lastName | ||
}`; | ||
const map = myegql.createMapFromDocument(document); | ||
assert.deepEqual(map, { | ||
[egql.getQueryKey(document.definitions[0])]: { | ||
transformedQuery: createDocumentFromQuery(document.definitions[0]), | ||
id: 1, | ||
}, | ||
}); | ||
}); | ||
}); | ||
@@ -290,0 +306,0 @@ |
@@ -115,2 +115,6 @@ import * as chai from 'chai'; | ||
} | ||
mutation changeAuthorStuff { | ||
firstName | ||
lastName | ||
} | ||
fragment personDetails on Person { | ||
@@ -160,3 +164,2 @@ firstName | ||
}; | ||
const operationNameQueryRequest = { | ||
@@ -173,2 +176,10 @@ operationName: 'ListOfAuthors', | ||
const mutationRequest = { | ||
id: 6, | ||
}; | ||
const mutationData = { | ||
firstName: 'John', | ||
lastName: 'Smith', | ||
}; | ||
const queryMap = egql.createMapFromDocument(queriesDocument); | ||
@@ -194,2 +205,4 @@ const uri = 'http://fake.com/fakegraphql'; | ||
return { data: operationNameQueryData }; | ||
} else if (_.isEqual(receivedObject, mutationRequest)) { | ||
return { data: mutationData }; | ||
} else { | ||
@@ -288,3 +301,16 @@ throw new Error('Received unmatched request in mock fetch.'); | ||
}); | ||
it('should also work with mutations', (done) => { | ||
pni.query({ | ||
query: gql` | ||
mutation changeAuthorStuff { | ||
firstName | ||
lastName | ||
}`, | ||
}).then((result) => { | ||
assert.deepEqual(result.data, mutationData); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
}); |
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
182639
3354
53
63