-
Notifications
You must be signed in to change notification settings - Fork 17
add records resolution #1974
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
Open
sevenzing
wants to merge
31
commits into
main
Choose a base branch
from
ll/omnigraph-resolution-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
add records resolution #1974
Changes from 5 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
eae25f6
add records resolution
sevenzing bbf391c
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing e8653a4
small docker fixes
sevenzing 6ff2930
add graphql styled records selection
sevenzing 713f1ee
add primary names field in omnigraph
sevenzing 319a99d
forgot introspection
sevenzing 32f53c8
remove default chain id and add disableAcceleration
sevenzing 09b1435
fix docs
sevenzing 7668d50
fix tests a little bit
sevenzing e1e5680
refactor
sevenzing 2e00efe
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing c88dde2
fix tests
sevenzing 380dadf
forgot changeset
sevenzing 4fef571
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing cf7cf01
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing b1232ab
fix PR suggestions
sevenzing bf275b9
fix the cast problem
sevenzing cbdca11
remove seed-cli
sevenzing 8acc0b2
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing 60d2793
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing 0550de5
Merge branch 'main' into ll/omnigraph-resolution-api
sevenzing 7c80a61
update resolution api
sevenzing caa8f76
fix PR comments
sevenzing ab2b849
lint + generate
sevenzing 5b0b1d2
default chain id
sevenzing 4e94c06
add resolve { } object and trace to resolve { }
sevenzing f5bacdd
self review
sevenzing c5330f0
fix for greptile review
sevenzing 6f50a9d
add EMBEDDED_DATA for AccelerationStatus
sevenzing a0008ea
self review again
sevenzing c5d7818
fix no selection bug
sevenzing File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
167 changes: 167 additions & 0 deletions
167
apps/ensapi/src/omnigraph-api/lib/build-records-selection.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,167 @@ | ||
| import { | ||
| type GraphQLFieldConfigMap, | ||
| GraphQLInt, | ||
| GraphQLList, | ||
| GraphQLNonNull, | ||
| GraphQLObjectType, | ||
| type GraphQLResolveInfo, | ||
| GraphQLScalarType, | ||
| GraphQLString, | ||
| Kind, | ||
| parse, | ||
| } from "graphql"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| buildRecordsSelectionFromResolveInfo, | ||
| EMPTY_RECORDS_SELECTION_MESSAGE, | ||
| } from "@/omnigraph-api/lib/build-records-selection"; | ||
| import { | ||
| RECORDS_SELECTION_PARAMETRIC_FIELDS, | ||
| RECORDS_SELECTION_SIMPLE_FIELDS, | ||
| } from "@/omnigraph-api/lib/records-selection-config"; | ||
|
|
||
| const stringListArg = new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLString))); | ||
| const intListArg = new GraphQLNonNull(new GraphQLList(new GraphQLNonNull(GraphQLInt))); | ||
|
|
||
| const mockBigIntArg = new GraphQLNonNull( | ||
| new GraphQLScalarType({ | ||
| name: "BigInt", | ||
| serialize: String, | ||
| parseValue: (value) => BigInt(value as string | number | bigint), | ||
| parseLiteral(ast) { | ||
| if (ast.kind === Kind.STRING || ast.kind === Kind.INT) return BigInt(ast.value); | ||
| throw new Error("BigInt literal must be a string or integer"); | ||
| }, | ||
| }), | ||
| ); | ||
|
|
||
| function buildMockResolvedRecordsType() { | ||
| const fields: Record<string, { type: typeof GraphQLString; args?: Record<string, unknown> }> = {}; | ||
|
|
||
| for (const { graphqlField } of RECORDS_SELECTION_SIMPLE_FIELDS) { | ||
| fields[graphqlField] = { type: GraphQLString }; | ||
| } | ||
|
|
||
| for (const { graphqlField, argName } of RECORDS_SELECTION_PARAMETRIC_FIELDS) { | ||
| const argType = | ||
| argName === "coinTypes" | ||
| ? intListArg | ||
| : argName === "contentTypeMask" | ||
| ? mockBigIntArg | ||
| : stringListArg; | ||
| fields[graphqlField] = { type: GraphQLString, args: { [argName]: { type: argType } } }; | ||
| } | ||
|
|
||
| return new GraphQLObjectType({ | ||
| name: "ResolvedRecords", | ||
| fields: fields as GraphQLFieldConfigMap<unknown, unknown>, | ||
| }); | ||
| } | ||
|
|
||
| const ResolvedRecordsType = buildMockResolvedRecordsType(); | ||
|
|
||
| function resolveInfoForRecordsSubselection(subselection: string): GraphQLResolveInfo { | ||
| const document = parse(`{ records { ${subselection} } }`); | ||
| const operation = document.definitions[0]; | ||
| if (operation.kind !== "OperationDefinition") throw new Error("expected operation"); | ||
|
|
||
| const recordsField = operation.selectionSet.selections[0]; | ||
| if (recordsField.kind !== "Field") throw new Error("expected field"); | ||
|
|
||
| return { | ||
| fieldNodes: [recordsField], | ||
| fragments: {}, | ||
| returnType: ResolvedRecordsType, | ||
| variableValues: {}, | ||
| } as unknown as GraphQLResolveInfo; | ||
| } | ||
|
|
||
| describe("buildRecordsSelectionFromResolveInfo", () => { | ||
| it.each(RECORDS_SELECTION_SIMPLE_FIELDS)( | ||
| "selects $graphqlField as $selectionKey", | ||
| ({ graphqlField, selectionKey }) => { | ||
| const info = resolveInfoForRecordsSubselection(graphqlField); | ||
| expect(buildRecordsSelectionFromResolveInfo(info)).toEqual({ [selectionKey]: true }); | ||
| }, | ||
| ); | ||
|
|
||
| it.each([ | ||
| { | ||
| subselection: 'texts(keys: ["description"])', | ||
| expected: { texts: ["description"] }, | ||
| }, | ||
| { | ||
| subselection: "addresses(coinTypes: [60])", | ||
| expected: { addresses: [60] }, | ||
| }, | ||
| { | ||
| subselection: 'abi(contentTypeMask: "1")', | ||
| expected: { abi: 1n }, | ||
| }, | ||
| { | ||
| subselection: 'interfaces(ids: ["0x01020304"])', | ||
| expected: { interfaces: ["0x01020304"] }, | ||
| }, | ||
| ])("parses parametric field: $subselection", ({ subselection, expected }) => { | ||
| const info = resolveInfoForRecordsSubselection(subselection); | ||
| expect(buildRecordsSelectionFromResolveInfo(info)).toEqual(expected); | ||
| }); | ||
|
|
||
| it("builds combined selection across simple and parametric fields", () => { | ||
| const info = resolveInfoForRecordsSubselection(` | ||
| reverseName | ||
| contenthash | ||
| texts(keys: ["avatar", "description"]) | ||
| addresses(coinTypes: [60]) | ||
| abi(contentTypeMask: "1") | ||
| `); | ||
|
|
||
| expect(buildRecordsSelectionFromResolveInfo(info)).toEqual({ | ||
| name: true, | ||
| contenthash: true, | ||
| texts: ["avatar", "description"], | ||
| addresses: [60], | ||
| abi: 1n, | ||
| }); | ||
| }); | ||
|
|
||
| it("ignores __typename", () => { | ||
| const info = resolveInfoForRecordsSubselection("__typename reverseName"); | ||
| expect(buildRecordsSelectionFromResolveInfo(info)).toEqual({ name: true }); | ||
| }); | ||
|
|
||
| it("throws when selection is empty", () => { | ||
| const info = resolveInfoForRecordsSubselection("__typename"); | ||
| expect(() => buildRecordsSelectionFromResolveInfo(info)).toThrow( | ||
| EMPTY_RECORDS_SELECTION_MESSAGE, | ||
| ); | ||
| }); | ||
|
|
||
| it("throws when only unknown fields are selected", () => { | ||
| const info = { | ||
| fieldNodes: [ | ||
| { | ||
| kind: "Field", | ||
| name: { kind: "Name", value: "records" }, | ||
| selectionSet: { | ||
| kind: "SelectionSet", | ||
| selections: [ | ||
| { | ||
| kind: "Field", | ||
| name: { kind: "Name", value: "unknownField" }, | ||
| }, | ||
| ], | ||
| }, | ||
| }, | ||
| ], | ||
| fragments: {}, | ||
| returnType: ResolvedRecordsType, | ||
| variableValues: {}, | ||
| } as unknown as GraphQLResolveInfo; | ||
|
|
||
| expect(() => buildRecordsSelectionFromResolveInfo(info)).toThrow( | ||
| EMPTY_RECORDS_SELECTION_MESSAGE, | ||
| ); | ||
| }); | ||
| }); |
81 changes: 81 additions & 0 deletions
81
apps/ensapi/src/omnigraph-api/lib/build-records-selection.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { | ||
| type FieldNode, | ||
| GraphQLError, | ||
| type GraphQLResolveInfo, | ||
| getArgumentValues, | ||
| getNamedType, | ||
| isObjectType, | ||
| type SelectionSetNode, | ||
| } from "graphql"; | ||
|
|
||
| import { isSelectionEmpty, type ResolverRecordsSelection } from "@ensnode/ensnode-sdk"; | ||
|
|
||
| import { | ||
| getParametricRecordsSelectionField, | ||
| getSimpleRecordsSelectionField, | ||
| } from "@/omnigraph-api/lib/records-selection-config"; | ||
|
|
||
| export const EMPTY_RECORDS_SELECTION_MESSAGE = "Records selection cannot be empty."; | ||
|
|
||
| function collectFieldNodes(selectionSet: SelectionSetNode, info: GraphQLResolveInfo): FieldNode[] { | ||
| const fields: FieldNode[] = []; | ||
|
|
||
| for (const selection of selectionSet.selections) { | ||
| if (selection.kind === "Field") { | ||
| if (selection.name.value === "__typename") continue; | ||
| fields.push(selection); | ||
| } else if (selection.kind === "InlineFragment") { | ||
| fields.push(...collectFieldNodes(selection.selectionSet, info)); | ||
| } else if (selection.kind === "FragmentSpread") { | ||
| const fragment = info.fragments[selection.name.value]; | ||
| if (fragment) fields.push(...collectFieldNodes(fragment.selectionSet, info)); | ||
| } | ||
| } | ||
|
|
||
| return fields; | ||
| } | ||
|
|
||
| /** | ||
| * Builds a {@link ResolverRecordsSelection} from the GraphQL selection set and field | ||
| * arguments on `Domain.records` → `ResolvedRecords`. | ||
| */ | ||
| export function buildRecordsSelectionFromResolveInfo( | ||
| info: GraphQLResolveInfo, | ||
| ): ResolverRecordsSelection { | ||
| const fieldNode = info.fieldNodes[0]; | ||
| if (!fieldNode?.selectionSet) { | ||
| throw new GraphQLError(EMPTY_RECORDS_SELECTION_MESSAGE); | ||
| } | ||
|
|
||
| const returnType = getNamedType(info.returnType); | ||
| if (!isObjectType(returnType)) { | ||
| throw new GraphQLError("Return type must be an object type."); | ||
| } | ||
|
|
||
| const selection: ResolverRecordsSelection = {}; | ||
|
|
||
| for (const childField of collectFieldNodes(fieldNode.selectionSet, info)) { | ||
| const graphqlField = childField.name.value; | ||
|
|
||
| const simple = getSimpleRecordsSelectionField(graphqlField); | ||
| if (simple) { | ||
| selection[simple.selectionKey] = true; | ||
| continue; | ||
| } | ||
|
|
||
| const parametric = getParametricRecordsSelectionField(graphqlField); | ||
| if (!parametric) continue; | ||
|
|
||
| const fieldDef = returnType.getFields()[graphqlField]; | ||
| if (!fieldDef) continue; | ||
|
|
||
| const args = getArgumentValues(fieldDef, childField, info.variableValues); | ||
| parametric.applySelection(selection, args); | ||
| } | ||
|
|
||
| if (isSelectionEmpty(selection)) { | ||
| throw new GraphQLError(EMPTY_RECORDS_SELECTION_MESSAGE); | ||
| } | ||
|
|
||
| return selection; | ||
| } | ||
95 changes: 95 additions & 0 deletions
95
apps/ensapi/src/omnigraph-api/lib/records-selection-config.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import type { CoinType, ContentType, InterfaceId } from "enssdk"; | ||
|
|
||
| import type { ResolverRecordsSelection } from "@ensnode/ensnode-sdk"; | ||
|
|
||
| export type RecordsSelectionSimpleKey = Extract< | ||
| keyof ResolverRecordsSelection, | ||
| "name" | "contenthash" | "pubkey" | "dnszonehash" | "version" | ||
| >; | ||
|
|
||
| export type RecordsSelectionParametricKey = Extract< | ||
| keyof ResolverRecordsSelection, | ||
| "texts" | "addresses" | "abi" | "interfaces" | ||
| >; | ||
|
|
||
| export type RecordsSelectionSimpleField = { | ||
| graphqlField: string; | ||
| selectionKey: RecordsSelectionSimpleKey; | ||
| }; | ||
|
|
||
| export type RecordsSelectionParametricField = { | ||
| graphqlField: string; | ||
| argName: string; | ||
| selectionKey: RecordsSelectionParametricKey; | ||
| applySelection: (selection: ResolverRecordsSelection, args: Record<string, unknown>) => void; | ||
| }; | ||
|
|
||
| /** | ||
| * GraphQL fields on `ResolvedRecords` that map to boolean flags in {@link ResolverRecordsSelection}. | ||
| * Querying the field (no args) selects that record for resolution. | ||
| */ | ||
| export const RECORDS_SELECTION_SIMPLE_FIELDS = [ | ||
| { graphqlField: "reverseName", selectionKey: "name" }, | ||
| { graphqlField: "contenthash", selectionKey: "contenthash" }, | ||
| { graphqlField: "pubkey", selectionKey: "pubkey" }, | ||
| { graphqlField: "dnszonehash", selectionKey: "dnszonehash" }, | ||
| { graphqlField: "version", selectionKey: "version" }, | ||
| ] as const satisfies readonly RecordsSelectionSimpleField[]; | ||
|
|
||
| /** | ||
| * GraphQL fields on `ResolvedRecords` that require arguments specifying which records to resolve. | ||
| */ | ||
| export const RECORDS_SELECTION_PARAMETRIC_FIELDS = [ | ||
| { | ||
| graphqlField: "texts", | ||
| argName: "keys", | ||
| selectionKey: "texts", | ||
| applySelection: (selection, args) => { | ||
| selection.texts = args.keys as string[]; | ||
| }, | ||
| }, | ||
| { | ||
| graphqlField: "addresses", | ||
| argName: "coinTypes", | ||
| selectionKey: "addresses", | ||
| applySelection: (selection, args) => { | ||
| selection.addresses = args.coinTypes as CoinType[]; | ||
| }, | ||
| }, | ||
| { | ||
| graphqlField: "abi", | ||
| argName: "contentTypeMask", | ||
| selectionKey: "abi", | ||
| applySelection: (selection, args) => { | ||
| selection.abi = args.contentTypeMask as ContentType; | ||
| }, | ||
| }, | ||
| { | ||
| graphqlField: "interfaces", | ||
| argName: "ids", | ||
| selectionKey: "interfaces", | ||
| applySelection: (selection, args) => { | ||
| selection.interfaces = args.ids as InterfaceId[]; | ||
| }, | ||
| }, | ||
| ] as const satisfies readonly RecordsSelectionParametricField[]; | ||
|
|
||
| const simpleFieldByGraphqlName = new Map<string, RecordsSelectionSimpleField>( | ||
| RECORDS_SELECTION_SIMPLE_FIELDS.map((f) => [f.graphqlField, f]), | ||
| ); | ||
|
|
||
| const parametricFieldByGraphqlName = new Map<string, RecordsSelectionParametricField>( | ||
| RECORDS_SELECTION_PARAMETRIC_FIELDS.map((f) => [f.graphqlField, f]), | ||
| ); | ||
|
|
||
| export function getSimpleRecordsSelectionField( | ||
| graphqlField: string, | ||
| ): RecordsSelectionSimpleField | undefined { | ||
| return simpleFieldByGraphqlName.get(graphqlField); | ||
| } | ||
|
|
||
| export function getParametricRecordsSelectionField( | ||
| graphqlField: string, | ||
| ): RecordsSelectionParametricField | undefined { | ||
| return parametricFieldByGraphqlName.get(graphqlField); | ||
| } |
30 changes: 30 additions & 0 deletions
30
apps/ensapi/src/omnigraph-api/lib/validate-primary-names-chain-ids.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import { DEFAULT_EVM_CHAIN_ID } from "enssdk"; | ||
| import { describe, expect, it } from "vitest"; | ||
|
|
||
| import { | ||
| DEFAULT_CHAIN_ID_WITH_OTHERS_MESSAGE, | ||
| EMPTY_CHAIN_IDS_MESSAGE, | ||
| validatePrimaryNamesChainIds, | ||
| } from "@/omnigraph-api/lib/validate-primary-names-chain-ids"; | ||
|
|
||
| describe("validatePrimaryNamesChainIds", () => { | ||
| it.each([ | ||
| { chainIds: undefined, label: "omitted" }, | ||
| { chainIds: null, label: "null" }, | ||
| { chainIds: [1], label: "single chain" }, | ||
| { chainIds: [1, 10], label: "multiple chains" }, | ||
| { chainIds: [DEFAULT_EVM_CHAIN_ID], label: "default chain only" }, | ||
| ])("allows $label", ({ chainIds }) => { | ||
| expect(() => validatePrimaryNamesChainIds(chainIds)).not.toThrow(); | ||
| }); | ||
|
|
||
| it("rejects empty chainIds", () => { | ||
| expect(() => validatePrimaryNamesChainIds([])).toThrow(EMPTY_CHAIN_IDS_MESSAGE); | ||
| }); | ||
|
|
||
| it("rejects default chain id combined with other chain ids", () => { | ||
| expect(() => validatePrimaryNamesChainIds([DEFAULT_EVM_CHAIN_ID, 1])).toThrow( | ||
| DEFAULT_CHAIN_ID_WITH_OTHERS_MESSAGE, | ||
| ); | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.