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

TransformQuery path also matches fragments #4171

Closed
Tracked by #5201 ...
saerdnaer opened this issue Jan 25, 2022 · 2 comments
Closed
Tracked by #5201 ...

TransformQuery path also matches fragments #4171

saerdnaer opened this issue Jan 25, 2022 · 2 comments

Comments

@saerdnaer
Copy link
Contributor

saerdnaer commented Jan 25, 2022

Describe the bug

When TransformQuery is used with a too generic path which might also match an attribute in the subtree, users get unexpected results when using fragments:

For example using TransformQuery with path ['editorialCategory'] transform both matches the query and the fragment:

query NestedTestQuery {
  editorialCategory(id: "42914694") {
    id
    sections {
        nodes {
          ...Show
        }
    }
  }
}

fragment Show on ProgramSet {
  id
  title
  editorialCategory {
    id
    title
  }
}

The problem is probably in this section: The visitor needs an indication if it currently in the root query or the fragment, als in both cases the index is set to 1:

public transformRequest(
originalRequest: ExecutionRequest,
delegationContext: DelegationContext,
transformationContext: Record<string, any>
): ExecutionRequest {
const pathLength = this.path.length;
let index = 0;
const document = visit(originalRequest.document, {
[Kind.FIELD]: {
enter: node => {
if (index === pathLength || node.name.value !== this.path[index] || node.selectionSet == null) {
return false;
}
index++;
if (index === pathLength) {
const selectionSet = this.queryTransformer(
node.selectionSet,
this.fragments,
delegationContext,
transformationContext
);
return {
...node,
selectionSet,
};
}
},

Example delegateToSchema call using transform

await delegateToSchema({
        schema: subschema
	fieldName: 'editorialCategory',
	args: { id },
	context,
	info,
	transforms: [
		// Wrap document takes a subtree as an AST node
		new TransformQuery({
			// path at which to apply wrapping and extracting
			path: [ 'editorialCategory' ],
			queryTransformer: (subtree: SelectionSetNode) => ({
				kind: Kind.SELECTION_SET,
				selections: [
					{
						kind: Kind.FIELD,
						name: {
							kind: Kind.NAME,
							value: 'programSets',
						},
						selectionSet: {
							kind: Kind.SELECTION_SET,
							selections: [
								{
									// we create a wrapping AST Field
									kind: Kind.FIELD,
									name: {
										kind: Kind.NAME,
										value: 'nodes',
									},
									// Inside the field selection
									selectionSet: subtree,
								},
							],
						},
					},
				],
			}),
			// how to process the data result at path
			resultTransformer: (result) => result?.programSets?.nodes,
			errorPathTransformer: (path) => path.slice(2),
		}),
	]
});

To Reproduce
Steps to reproduce the behaviour:

Query A, which triggers path matching bug

query NestedTestQuery {
  editorialCategory(id: "42914694") {
    id
    sections {
      ...SectionType
    }
  }
}

fragment SectionType on Section {
  id
  title
  type
  nodes {
    ...Show
  }
}

fragment Show on ProgramSet {
  id
  title
  editorialCategory {
    id
    title
  }
}

Query B, with workaround for path matching bug

query GetEditorialCategoryById {
  editorialCategory(id: "42914694") {
    id
    sections {
      ...SectionType
    }
  }
}

fragment SectionType on Section {
  id
  title
  type
  nodes {
    ... on ProgramSet {
      id
      title
      editorialCategory {
        id
        title
      }
    }
  }
}

Expected behavior

Environment:

    "@graphql-tools/batch-delegate": "8.2.3",
    "@graphql-tools/delegate": "8.4.3",
    "@graphql-tools/graphql-file-loader": "7.3.3",
    "@graphql-tools/load": "7.5.1",
    "@graphql-tools/merge": "8.2.1",
    "@graphql-tools/stitch": "8.4.3",
    "@graphql-tools/utils": "8.6.1",
    "@graphql-tools/wrap": "8.3.3",

Additional context

@saerdnaer
Copy link
Contributor Author

@ardatan Is there a solution or workaround for this problem?

@ardatan
Copy link
Owner

ardatan commented Mar 13, 2024

Fixed in 807491e

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants