UNPKG

1.06 kBPlain TextView Raw
1// importing print is a reasonable thing to do, since Apollo Link Http requires
2// it to be present
3import { DocumentNode, DirectiveNode, print } from 'graphql';
4
5import { checkDocument, removeDirectivesFromDocument } from 'apollo-utilities';
6
7const connectionRemoveConfig = {
8 test: (directive: DirectiveNode) => directive.name.value === 'client',
9 remove: true,
10};
11
12const removed = new Map();
13export function removeClientSetsFromDocument(
14 query: DocumentNode,
15): DocumentNode {
16 // caching
17 const cached = removed.get(query);
18 if (cached) return cached;
19
20 checkDocument(query);
21
22 const docClone = removeDirectivesFromDocument(
23 [connectionRemoveConfig],
24 query,
25 );
26
27 // caching
28 removed.set(query, docClone);
29 return docClone;
30}
31
32export function normalizeTypeDefs(
33 typeDefs: string | string[] | DocumentNode | DocumentNode[],
34) {
35 const defs = Array.isArray(typeDefs) ? typeDefs : [typeDefs];
36
37 return defs
38 .map(typeDef => (typeof typeDef === 'string' ? typeDef : print(typeDef)))
39 .map(str => str.trim())
40 .join('\n');
41}