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

Fix Special Characters in Schema #7

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

andreachild
Copy link

@andreachild andreachild commented Oct 24, 2024

PR to Allow Special Characters in Schema

This PR is to edit the graphQL schema to support any graph data that has special/invalid characters

How it Works + Changes

  • get data using OpenCypher queries
    • add backticks to labels to escape special characters
  • creates graphQL schema
    • replaces invalid characters with something else that works (ex. _cn_, _dot_, _hy_)
    • adds alias directives with the original label
  • querying in AppSync which converts graphQL requests to OpenCypher queries
    • uses the labels in the alias directives to create query with original labels
    • add backticks when creating query to escape special characters

@sophiadt sophiadt changed the title Draft: Sophiadt/colon fix Fix Special Characters in Schema Oct 28, 2024
@sophiadt sophiadt marked this pull request as ready for review October 31, 2024 17:36
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/NeptuneSchema.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
@andreachild
Copy link
Author

I tried running a few of the queries from the sample security data and found some of them do not work because we need to escape properties in the RETURN clause.

For example this query:

query MyQuery {
  getNodeEc2_cn_internet_hy_gateways {
    arn
    scan_id
    owner_id
    attachment_dot_state
  }
}

Resolves to this open cypher query:

MATCH(getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway:`ec2:internet-gateway`) RETURN collect({arn:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.arn,scan_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.scan_id,owner_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.owner_id,attachment_dot_state:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.attachment.state})

Which fails because there is a property attachment.state which has a . character. Error message from axios says "Property access over ambiguous type is not supported in this release". It works if this is also surrounded by backticks:

MATCH(getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway:`ec2:internet-gateway`) RETURN collect({arn:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.arn,scan_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.scan_id,owner_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.owner_id,attachment_dot_state:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.`attachment.state`})

src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
@sophiadt
Copy link

sophiadt commented Nov 7, 2024

I tried running a few of the queries from the sample security data and found some of them do not work because we need to escape properties in the RETURN clause.

For example this query:

query MyQuery {
  getNodeEc2_cn_internet_hy_gateways {
    arn
    scan_id
    owner_id
    attachment_dot_state
  }
}

Resolves to this open cypher query:

MATCH(getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway:`ec2:internet-gateway`) RETURN collect({arn:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.arn,scan_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.scan_id,owner_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.owner_id,attachment_dot_state:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.attachment.state})

Which fails because there is a property attachment.state which has a . character. Error message from axios says "Property access over ambiguous type is not supported in this release". It works if this is also surrounded by backticks:

MATCH(getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway:`ec2:internet-gateway`) RETURN collect({arn:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.arn,scan_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.scan_id,owner_id:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.owner_id,attachment_dot_state:getNodeEc2_cn_internet_hy_gateways_Ec2_cn_internet_hy_gateway.`attachment.state`})

I added backticks to the OPTIONAL MATCH queries so it should properly add backticks to schemaTypeInfo.typeAlias which has the . character. I tested it out with the query you used above and also tested getting the relationships by querying ec2_cn_security_hy_groupResource_linkOut and ec2_cn_vpcResource_linkOut and it works for me now.

if (schemaTypeInfo.isRelationship) {        
        if (schemaTypeInfo.relationship.direction === 'IN') {
            matchStatements.push(`OPTIONAL MATCH (${lastNamePath})<-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]-(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`);
        } else {
            matchStatements.push(`OPTIONAL MATCH (${lastNamePath})-[${schemaTypeInfo.pathName}_${schemaTypeInfo.relationship.edgeType}:${schemaTypeInfo.relationship.edgeType}]->(${schemaTypeInfo.pathName}:\`${schemaTypeInfo.typeAlias}\`${queryArguments})`);
        }
    } 

src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/test/graphdb.test.js Outdated Show resolved Hide resolved
@andreachild
Copy link
Author

The tests are failing because the files that contain the expected results are missing the back ticks that were added

Screenshot 2024-11-14 at 4 03 47 PM
Screenshot 2024-11-14 at 4 04 55 PM

Copy link

@kmcginnes kmcginnes left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly minor nit stuff.

src/NeptuneSchema.js Outdated Show resolved Hide resolved
src/NeptuneSchema.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
templates/JSResolverOCHTTPSTemplate.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
src/graphdb.js Outdated Show resolved Hide resolved
@andreachild
Copy link
Author

Approving via this comment

…t has special/invalid characters

Changes
- get data using OpenCypher queries
  - add backticks to labels to escape special characters
- creates graphQL schema
  - replaces invalid characters with something else that works (ex. `_cn_`, `_dot_`, `_hy_`)
  - adds alias directives with the original label
- querying in AppSync which converts graphQL requests to OpenCypher queries
  - uses the labels in the alias directives to create query with original labels
  - add backticks when creating query to escape special characters
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

Successfully merging this pull request may close these issues.

3 participants