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

Add Items connection mode #151

Merged
merged 2 commits into from
Jun 28, 2020
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
24 changes: 20 additions & 4 deletions docs/guide/connection-mode.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ It seems that there are several standards within the GraphQL community how conne
records) are designed. Some do this via a `nodes` field, some via a `edges { nodes }` query and some do neither of them.
Vuex-ORM-GraphQL tries to be flexible and supports all of them.

There are four possible modes: `AUTO`, `NODES`, `EDGES`, `PLAIN`. The Adapter you use will tell the
There are four possible modes: `AUTO`, `NODES`, `EDGES`, `PLAIN`, `ITEMS`. The Adapter you use will tell the
plugin which ConnectionMode to use. In the DefaultAdapter this is `AUTO`.


Expand All @@ -22,7 +22,7 @@ In rare cases the automatic detection might fail or report the wrong mode. In th
manually set the connection mode via a custom adapter. The modes and the resulting
queries are explained in the next sections.

## Mode 1: `plain`
## Mode 1: `plain`

The third mode is the less preferred one due to the lack of meta information. In this case we just plain pass the field
queries:
Expand Down Expand Up @@ -54,8 +54,8 @@ query Users {
}
```

## Mode 3: `edges`

## Mode 3: `edges`

This mode uses a `edges` not to query the edge an then query the `node` within that edge:

Expand All @@ -72,3 +72,19 @@ query Users {
}
}
```

## Mode 4: `items`

This is the mode used for handling the shape of AWS AppSync queries. Using `items` (or letting the plugin auto detect this mode) will lead to the following query when calling `User.fetch()`:

```
query Users {
users {
items {
id
email
name
}
}
}
```
2 changes: 1 addition & 1 deletion docs/guide/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const options = {
fetchPolicy: 'cache-and-network'
}
},
connectionQueryMode: 'nodes',
connectionQueryMode: 'items',
database: database,
url: awsexports.aws_appsync_graphqlEndpoint,
includeExtensions: true,
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ export enum ConnectionMode {
AUTO,
PLAIN,
NODES,
EDGES
EDGES,
ITEMS
}

export enum ArgumentMode {
Expand Down
2 changes: 1 addition & 1 deletion src/common/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ export default class Context {
private schemaWillBeLoaded: Promise<Schema> | undefined;

/**
* Defines how to query connections. 'auto' | 'nodes' | 'edges' | 'plain'
* Defines how to query connections. 'auto' | 'nodes' | 'edges' | 'plain' | 'items'
*/
public connectionMode: ConnectionMode = ConnectionMode.AUTO;

Expand Down
8 changes: 8 additions & 0 deletions src/graphql/query-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ export default class QueryBuilder {
}
}
`;
} else if (context.connectionMode === ConnectionMode.ITEMS) {
return `
${header} {
items {
${fields}
}
}
`;
} else {
return `
${header} {
Expand Down
2 changes: 2 additions & 0 deletions src/graphql/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ export default class Schema {
return ConnectionMode.NODES;
} else if (connection!.fields!.find(f => f.name === "edges")) {
return ConnectionMode.EDGES;
} else if (connection!.fields!.find(f => f.name === "items")) {
return ConnectionMode.ITEMS;
} else {
return ConnectionMode.PLAIN;
}
Expand Down
7 changes: 7 additions & 0 deletions src/graphql/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,13 @@ export default class Transformer {
);
} else if (data["node"] && context.connectionMode === ConnectionMode.EDGES) {
result = this.transformIncomingData(data["node"], localModel, mutation, true);
} else if (data[key].items && context.connectionMode === ConnectionMode.ITEMS) {
result[pluralize(key)] = this.transformIncomingData(
data[key].items,
localModel,
mutation,
true
);
} else {
let newKey = key;

Expand Down