Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getOperationName(taggedTemplate) {
const template = taggedTemplate.template.getFullText()
// Strip backticks
const source = template.substring(1, template.length - 1)
const ast = parse(source)
let queryName = null
visit(ast, {
OperationDefinition(node) {
queryName = node.name.value
return BREAK
},
})
return queryName
}
Field (fieldNode) {
return visit(fieldNode, {
Directive (node, key, parent, path, ancestors) {
if (node.name.value === 'paginate') {
if (result.paginate.typeName) {
return BREAK
}
const parentNode = ancestors.slice().pop()
const perPageArg = parentNode.arguments.find(node => node.name.value === 'perPage')
// guess content type by converting root field value into a camel cased string
result.paginate.typeName = upperFirst(trimStart(fieldNode.name.value, 'all'))
if (perPageArg) {
result.paginate.perPage = Number(perPageArg.value.value)
}
function visitFields(
node: OperationDefinitionNode | FragmentDefinitionNode,
graphQLTag: GraphQLTag,
typeInfo: TypeInfo,
cb: (fieldInfo: FieldInfo) => void
) {
if (!node.name) {
throw new Error(
"visitFields expects OperationDefinitions and FragmentDefinitions to be named"
);
}
const { filePath, sourceLocationOffset, template } = graphQLTag;
const operationOrFragmentName = node.name.value;
visit(
node,
visitWithTypeInfo(typeInfo, {
Field(graphqlNode) {
// Discard client only fields, but don't throw an error
if (isClientOnlyField(graphqlNode)) return;
const parentType = typeInfo.getParentType();
const nodeType = typeInfo.getType();
const nodeName = graphqlNode.name.value;
if (!parentType) {
throw new Error(
`visitFields expects fields to have a parent type. No parent type for ${nodeName}`
);
}
const hasPersistDirective = doc => {
let hasDirective = false
visit(doc, {
Directive: ({ name: { value: name } }) => {
if (name === 'persist') {
hasDirective = true
return BREAK
}
}
})
return hasDirective
}
protected _extractFragments(document: FragmentDefinitionNode | OperationDefinitionNode): string[] {
if (!document) {
return [];
}
const names = [];
visit(document, {
enter: {
FragmentSpread: (node: FragmentSpreadNode) => {
names.push(node.name.value);
},
},
});
return names;
}
export function removeConnectionDirectives(ast: ASTNode) {
return visit(ast, {
Directive(node: DirectiveNode): DirectiveNode | null {
if (node.name.value === "connection") return null;
return node;
}
});
}
export function removeAliases(ast: DocumentNode): DocumentNode {
return visit(ast, {
Field(node: FieldNode): FieldNode {
return {
...node,
alias: undefined,
};
},
});
}
shouldFetchEntityType(entityType: string): boolean {
if (!this.cachedEntityTypesToFetch) {
const entityTypesToFetch: string[] = []
visit(this.info.fieldNodes[0], {
Field(node) {
if (node.name.value === "node") {
visit(node, {
InlineFragment(node) {
if (
node.typeCondition &&
(node.typeCondition.name.value !== Searchable.name &&
node.typeCondition.name.value !== SearchableItem.name)
) {
entityTypesToFetch.push(node.typeCondition.name.value)
}
},
FragmentSpread(_node) {
throw new Error(
"Named fragment spreads are currently unsupported for search."
)
export const refactorFieldsWithFragmentSpread = (
ast: DocumentNode,
refactorMap: FragmentRefactorMap,
schema: GraphQLSchema
) => {
const typeInfo = new TypeInfo(schema);
const edited = visit(ast, visitWithTypeInfo(typeInfo, {
Field: {
enter(node) {
typeInfo.enter(node);
const type = typeInfo.getType();
if (type) {
const typeName = getTypeName(type);
const refactorFields = refactorMap[typeName];
if (
refactorFields &&
node.selectionSet &&
refactorFields.every(
field => !!node.selectionSet!.selections.find((selection: FieldNode) => selection.name.value === field)
)
) {
return {
...node,
function parsePageQuery (pageQuery) {
const result = {
content: pageQuery.content || '',
options: pageQuery.options || {},
typeName: pageQuery.typeName,
variables: [],
paginate: {
typeName: undefined,
perPage: PER_PAGE
}
}
const ast = result.content ? parse(result.content) : null
result.query = ast && visit(ast, {
Variable ({ name: { value: name }}) {
if (name === 'page') return
if (name === 'path') return
const path = !NODE_FIELDS.includes(name)
? ['fields'].concat(name.split('__'))
: [name]
result.variables.push({ name, path })
},
Field (fieldNode) {
return visit(fieldNode, {
Directive (node, key, parent, path, ancestors) {
if (node.name.value === 'paginate') {
if (result.paginate.typeName) {
return BREAK