graphql-codegen-persisted-query-ids
Advanced tools
Comparing version 0.0.4 to 0.0.5
import { parse } from "graphql"; | ||
import { generateQueryIds } from "../src"; | ||
import { generateQueryIds, findFragmentSpreadsNames } from "../src"; | ||
@@ -108,5 +108,29 @@ // Nooop gql fn for prettier | ||
}); | ||
test("can use fragment before it's definition", () => { | ||
const doc = parse(gql` | ||
query Foo { | ||
bar | ||
...myFragment | ||
} | ||
fragment myFragment on Ding { | ||
name | ||
} | ||
`); | ||
const server = generateQueryIds([doc], { | ||
output: "server", | ||
}); | ||
const client = generateQueryIds([doc], { | ||
output: "client", | ||
}); | ||
const query = server[client["Foo"]]; | ||
expect(query).toBeTruthy(); | ||
}); | ||
}); | ||
describe("Mutations", () => { | ||
describe("mutation", () => { | ||
const doc = parse(gql` | ||
@@ -143,4 +167,29 @@ mutation AddTodo($title: String!) { | ||
expect(query).toBeTruthy(); | ||
expect(query).toMatchSnapshot(); | ||
}); | ||
}); | ||
test("can find nested fragment user", () => { | ||
const doc = parse(gql` | ||
query DualTodoList($cursorTodos: String!, $cursorDones: String!) { | ||
todos(first: 3, after: $cursorTodos, where: { completed: false }) { | ||
...TodoParts | ||
} | ||
dones: todos( | ||
first: 3 | ||
after: $cursorDones | ||
where: { completed: true } | ||
) { | ||
...TodoParts | ||
} | ||
} | ||
`); | ||
const def = doc.definitions[0]; | ||
if (def.kind !== "OperationDefinition") { | ||
throw new Error("bad type"); | ||
} | ||
const fragmentNames = findFragmentSpreadsNames(def); | ||
expect(fragmentNames).toEqual(["TodoParts"]); | ||
}); |
@@ -1,7 +0,8 @@ | ||
import { DocumentNode } from "graphql"; | ||
import { DocumentNode, OperationDefinitionNode, FragmentDefinitionNode } from "graphql"; | ||
import { PluginFunction } from "graphql-codegen-core"; | ||
export interface PluginConfig { | ||
output: "server" | "client" | undefined; | ||
addTypeName?: boolean; | ||
} | ||
export declare function findFragmentSpreadsNames(operation: OperationDefinitionNode): string[]; | ||
export declare function findFragments(docs: DocumentNode[]): FragmentDefinitionNode[]; | ||
export declare function generateQueryIds(docs: DocumentNode[], config: PluginConfig): { | ||
@@ -8,0 +9,0 @@ [key: string]: string; |
@@ -68,50 +68,58 @@ "use strict"; | ||
} | ||
function findUsedFragments(operation) { | ||
function findFragmentSpreadsNames(operation) { | ||
var fragmentNames = []; | ||
for (var _i = 0, _a = operation.selectionSet.selections; _i < _a.length; _i++) { | ||
var selection = _a[_i]; | ||
if (selection.kind == "FragmentSpread") { | ||
if (!fragmentNames.includes(selection.name.value)) { | ||
fragmentNames.push(selection.name.value); | ||
} | ||
} | ||
} | ||
graphql_1.visit(operation, { | ||
FragmentSpread: { | ||
enter: function (node) { | ||
if (!fragmentNames.includes(node.name.value)) { | ||
fragmentNames.push(node.name.value); | ||
} | ||
}, | ||
}, | ||
}); | ||
return fragmentNames; | ||
} | ||
exports.findFragmentSpreadsNames = findFragmentSpreadsNames; | ||
function findFragments(docs) { | ||
var fragments = []; | ||
for (var _i = 0, docs_1 = docs; _i < docs_1.length; _i++) { | ||
var doc = docs_1[_i]; | ||
graphql_1.visit(doc, { | ||
FragmentDefinition: { | ||
enter: function (node) { | ||
fragments.push(node); | ||
}, | ||
}, | ||
}); | ||
} | ||
return fragments; | ||
} | ||
exports.findFragments = findFragments; | ||
function generateQueryIds(docs, config) { | ||
docs = docs.map(addTypenameToDocument); | ||
var out = {}; | ||
for (var _i = 0, docs_1 = docs; _i < docs_1.length; _i++) { | ||
var doc = docs_1[_i]; | ||
if (config.addTypeName) { | ||
doc = addTypenameToDocument(doc); | ||
} | ||
var fragments = []; | ||
for (var _a = 0, _b = doc.definitions; _a < _b.length; _a++) { | ||
var def = _b[_a]; | ||
if (def.kind === "FragmentDefinition") { | ||
fragments.push(def); | ||
} | ||
if (def.kind === "OperationDefinition") { | ||
if (!def.name) { | ||
throw new Error("OperationDefinition missing name"); | ||
} | ||
var usedFragments = findUsedFragments(def); | ||
var definitions = []; | ||
for (var _c = 0, fragments_1 = fragments; _c < fragments_1.length; _c++) { | ||
var fragment = fragments_1[_c]; | ||
if (usedFragments.includes(fragment.name.value)) { | ||
definitions.push(fragment); | ||
var fragments = findFragments(docs); | ||
for (var _i = 0, docs_2 = docs; _i < docs_2.length; _i++) { | ||
var doc = docs_2[_i]; | ||
graphql_1.visit(doc, { | ||
OperationDefinition: { | ||
enter: function (def) { | ||
if (!def.name) { | ||
throw new Error("OperationDefinition missing name"); | ||
} | ||
} | ||
definitions.push(def); | ||
var query = printDefinitions(definitions); | ||
var hash = createHash(query); | ||
if (config.output === "client") { | ||
out[def.name.value] = hash; | ||
} | ||
else { | ||
out[hash] = query; | ||
} | ||
} | ||
} | ||
var usedFragmentNames = findFragmentSpreadsNames(def); | ||
var usedFragments = fragments.filter(function (frag) { | ||
return usedFragmentNames.includes(frag.name.value); | ||
}); | ||
var query = printDefinitions(usedFragments.concat([def])); | ||
var hash = createHash(query); | ||
if (config.output === "client") { | ||
out[def.name.value] = hash; | ||
} | ||
else { | ||
out[hash] = query; | ||
} | ||
}, | ||
}, | ||
}); | ||
} | ||
@@ -118,0 +126,0 @@ return out; |
{ | ||
"name": "graphql-codegen-persisted-query-ids", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Generate persisted query ids", | ||
@@ -5,0 +5,0 @@ "main": "lib/index.js", |
@@ -9,2 +9,3 @@ import crypto from "crypto"; | ||
FragmentDefinitionNode, | ||
DefinitionNode, | ||
} from "graphql"; | ||
@@ -23,3 +24,3 @@ import { PluginFunction } from "graphql-codegen-core"; | ||
function printDefinitions(definitions: Definition[]) { | ||
function printDefinitions(definitions: (Definition | DocumentNode)[]) { | ||
return definitions.map(print).join("\n"); | ||
@@ -84,15 +85,16 @@ } | ||
output: "server" | "client" | undefined; | ||
addTypeName?: boolean; | ||
} | ||
function findUsedFragments(operation: OperationDefinitionNode) { | ||
export function findFragmentSpreadsNames(operation: OperationDefinitionNode) { | ||
const fragmentNames: string[] = []; | ||
for (const selection of operation.selectionSet.selections) { | ||
if (selection.kind == "FragmentSpread") { | ||
if (!fragmentNames.includes(selection.name.value)) { | ||
fragmentNames.push(selection.name.value); | ||
} | ||
} | ||
} | ||
visit(operation, { | ||
FragmentSpread: { | ||
enter(node) { | ||
if (!fragmentNames.includes(node.name.value)) { | ||
fragmentNames.push(node.name.value); | ||
} | ||
}, | ||
}, | ||
}); | ||
@@ -102,44 +104,50 @@ return fragmentNames; | ||
export function generateQueryIds(docs: DocumentNode[], config: PluginConfig) { | ||
const out: { [key: string]: string } = {}; | ||
export function findFragments(docs: DocumentNode[]) { | ||
const fragments: FragmentDefinitionNode[] = []; | ||
for (let doc of docs) { | ||
if (config.addTypeName) { | ||
doc = addTypenameToDocument(doc); | ||
} | ||
for (const doc of docs) { | ||
visit(doc, { | ||
FragmentDefinition: { | ||
enter(node) { | ||
fragments.push(node); | ||
}, | ||
}, | ||
}); | ||
} | ||
let fragments: FragmentDefinitionNode[] = []; | ||
return fragments; | ||
} | ||
for (const def of doc.definitions) { | ||
if (def.kind === "FragmentDefinition") { | ||
fragments.push(def); | ||
} | ||
export function generateQueryIds(docs: DocumentNode[], config: PluginConfig) { | ||
docs = docs.map(addTypenameToDocument); | ||
if (def.kind === "OperationDefinition") { | ||
if (!def.name) { | ||
throw new Error("OperationDefinition missing name"); | ||
} | ||
const out: { [key: string]: string } = {}; | ||
const usedFragments = findUsedFragments(def); | ||
const fragments = findFragments(docs); | ||
const definitions: Definition[] = []; | ||
for (const fragment of fragments) { | ||
if (usedFragments.includes(fragment.name.value)) { | ||
definitions.push(fragment); | ||
for (const doc of docs) { | ||
visit(doc, { | ||
OperationDefinition: { | ||
enter(def) { | ||
if (!def.name) { | ||
throw new Error("OperationDefinition missing name"); | ||
} | ||
} | ||
definitions.push(def); | ||
const usedFragmentNames = findFragmentSpreadsNames(def); | ||
const query = printDefinitions(definitions); | ||
const hash = createHash(query); | ||
const usedFragments = fragments.filter(frag => | ||
usedFragmentNames.includes(frag.name.value), | ||
); | ||
if (config.output === "client") { | ||
out[def.name.value] = hash; | ||
} else { | ||
out[hash] = query; | ||
} | ||
} | ||
} | ||
const query = printDefinitions([...usedFragments, def]); | ||
const hash = createHash(query); | ||
if (config.output === "client") { | ||
out[def.name.value] = hash; | ||
} else { | ||
out[hash] = query; | ||
} | ||
}, | ||
}, | ||
}); | ||
} | ||
@@ -146,0 +154,0 @@ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
26050
525