Skip to content

Commit

Permalink
Merge pull request #50 from danstarns/alias-fix
Browse files Browse the repository at this point in the history
Alias fix
  • Loading branch information
danstarns authored Jan 22, 2021
2 parents 220aeb3 + 185c6f0 commit dd83ece
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/graphql/src/translate/create-projection-and-params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,14 @@ function createProjectionAndParams({
varName: string;
chainStrOverRide?: string;
}): [string, any] {
function reducer(res: Res, [key, field]: [string, FieldsByTypeName]): Res {
function reducer(res: Res, [k, field]: [string, any]): Res {
let key = k;
const alias: string | undefined = field.alias !== field.name ? field.alias : undefined;

if (alias) {
key = field.name as string;
}

let param = "";
if (chainStr) {
param = `${chainStr}_${key}`;
Expand Down Expand Up @@ -301,6 +308,7 @@ function createProjectionAndParams({
}

res.projection.push(`.${key}`);

return res;
}

Expand Down
59 changes: 59 additions & 0 deletions packages/graphql/tests/tck/tck-test-files/cypher-alias.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
## Cypher Alias

Tests to ensure when using aliases that the cypher is correct.

Schema:

```schema
type Actor {
name: String
}
type Movie {
id: ID
actors: [Actor] @relationship(type: "ACTED_IN", direction: "IN")
custom: [Movie] @cypher(statement: """
MATCH (m:Movie)
RETURN m
""")
}
```

---

### Alias

**GraphQL input**

```graphql
{
movies {
movieId: id
actors {
aliasActorsName: name
}
custom {
aliasCustomId: id
}
}
}
```

**Expected Cypher output**

```cypher
MATCH (this:Movie)
RETURN this {
.id,
actors: [ (this)<-[:ACTED_IN]-(this_actors:Actor) | this_actors { .name } ],
custom: [this_custom IN apoc.cypher.runFirstColumn("MATCH (m:Movie) RETURN m", {this: this, jwt: $jwt}, true) | this_custom { .id }]
} as this
```

**Expected Cypher params**

```cypher-params
{
"jwt": {}
}
```

0 comments on commit dd83ece

Please sign in to comment.