Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(diagnostics): validate up to date hash #301

Merged
merged 7 commits into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/many-nails-wait.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@0no-co/graphqlsp': minor
---

Add validation step to check that the persisted-operations hash has been updated when the document changes
25 changes: 25 additions & 0 deletions packages/graphqlsp/src/diagnostics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
getColocatedFragmentNames,
} from './checkImports';
import {
generateHashForDocument,
getDocumentReferenceFromDocumentNode,
getDocumentReferenceFromTypeQuery,
} from './persisted';
Expand Down Expand Up @@ -54,6 +55,7 @@ export const USING_DEPRECATED_FIELD_CODE = 52004;
export const MISSING_PERSISTED_TYPE_ARG = 520100;
export const MISSING_PERSISTED_CODE_ARG = 520101;
export const MISSING_PERSISTED_DOCUMENT = 520102;
export const MISSMATCH_HASH_TO_DOCUMENT = 520103;
export const ALL_DIAGNOSTICS = [
SEMANTIC_DIAGNOSTIC_CODE,
MISSING_OPERATION_NAME_CODE,
Expand All @@ -63,6 +65,7 @@ export const ALL_DIAGNOSTICS = [
MISSING_PERSISTED_TYPE_ARG,
MISSING_PERSISTED_CODE_ARG,
MISSING_PERSISTED_DOCUMENT,
MISSMATCH_HASH_TO_DOCUMENT,
];

const cache = new LRUCache<number, ts.Diagnostic[]>({
Expand Down Expand Up @@ -247,6 +250,28 @@ export function getGraphQLDiagnostics(
};
}

const hash = callExpression.arguments[0].getText().slice(1, -1);
if (hash.startsWith('sha256:')) {
const hash = generateHashForDocument(
info,
initializer.arguments[0],
foundFilename
);
if (!hash) return null;
const upToDateHash = `sha256:${hash}`;
if (upToDateHash !== hash) {
return {
category: ts.DiagnosticCategory.Warning,
code: MISSMATCH_HASH_TO_DOCUMENT,
file: source,
messageText: `The persisted document's hash is outdated`,
start: callExpression.arguments.pos,
length:
callExpression.arguments.end - callExpression.arguments.pos,
};
}
}

return null;
})
.filter(Boolean);
Expand Down
73 changes: 43 additions & 30 deletions packages/graphqlsp/src/persisted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,37 +119,11 @@ export function getPersistedCodeFixAtPosition(
)
return undefined;

const externalSource = getSource(info, foundFilename)!;
const { fragments } = findAllCallExpressions(externalSource, info);

const text = resolveTemplate(
const hash = generateHashForDocument(
info,
initializer.arguments[0],
foundFilename,
info
).combinedText;
const parsed = parse(text);
const spreads = new Set();
visit(parsed, {
FragmentSpread: node => {
spreads.add(node.name.value);
},
});

let resolvedText = text;
[...spreads].forEach(spreadName => {
const fragmentDefinition = fragments.find(x => x.name.value === spreadName);
if (!fragmentDefinition) {
console.warn(
`[GraphQLSP] could not find fragment for spread ${spreadName}!`
);
return;
}

resolvedText = `${resolvedText}\n\n${print(fragmentDefinition)}`;
});

const hash = createHash('sha256').update(text).digest('hex');

foundFilename
);
const existingHash = callExpression.arguments[0];
// We assume for now that this is either undefined or an existing string literal
if (!existingHash) {
Expand Down Expand Up @@ -188,6 +162,45 @@ export function getPersistedCodeFixAtPosition(
}
}

export const generateHashForDocument = (
info: ts.server.PluginCreateInfo,
templateLiteral:
| ts.NoSubstitutionTemplateLiteral
| ts.TaggedTemplateExpression,
foundFilename: string
): string | undefined => {
const externalSource = getSource(info, foundFilename)!;
const { fragments } = findAllCallExpressions(externalSource, info);

const text = resolveTemplate(
templateLiteral,
foundFilename,
info
).combinedText;
const parsed = parse(text);
const spreads = new Set();
visit(parsed, {
FragmentSpread: node => {
spreads.add(node.name.value);
},
});

let resolvedText = text;
[...spreads].forEach(spreadName => {
const fragmentDefinition = fragments.find(x => x.name.value === spreadName);
if (!fragmentDefinition) {
console.warn(
`[GraphQLSP] could not find fragment for spread ${spreadName}!`
);
return;
}

resolvedText = `${resolvedText}\n\n${print(fragmentDefinition)}`;
});

return createHash('sha256').update(text).digest('hex');
};

export const getDocumentReferenceFromTypeQuery = (
typeQuery: ts.TypeQueryNode,
filename: string,
Expand Down