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 linking entity option on m2m #44

Closed
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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ main()

- Arguments
- `config` (object, optional) - A configuration object with the following schema.
- `defaultArgsAdapter` (function, optional) - The default `argsAdapter` function for the entities.
- `addEntitiesResolvers` (boolean, optional) - automatically add entities types and resolvers accordingly with configuration, see [composer entities section](#composer-entities).
- `logger` TODO
- `subgraphs` (array, optional) - Array of subgraph configuration objects with the following schema.
- `name` (string, optional) - A unique name to identify the subgraph; if missing the default one is `#${index}`, where index is the subgraph index in the array.
- `server` (object, required) - Configuration object for communicating with the subgraph server with the following schema:
Expand All @@ -195,11 +198,12 @@ main()
- `resolver` (object, optional) - The resolver definition to query the foreing entity, same structure as `entity.resolver`.
- `many` (array of objects, optional) - Describe a 1-to-many relation - the reverse of the foreign key.
- `type` (string, required) - The entity type where the entity is a foreign key.
- `fkey` (string, required) - The foreign key field in the referred entity.
- `fkey` (string, optional) - The foreign key field in the referred entity. TODO Required but not on `link`
- `as` (string, required) - When using `addEntitiesResolvers`, it defines the name of the relation as a field of the current one, as a list.
- `pkey` (string, optional) - The primary key of the referred entity.
- `subgraph` (string, optional) - The subgraph name of the referred entity, where the resolver is located; if missing is intended the self.
- `resolver` (object, required) - The resolver definition to query the referred entity, same structure as `entity.resolver`.
- `link` TODO
- `onSubgraphError` (function, optional) - Hook called when an error occurs getting schema from a subgraph. The default function will throw the error. The arguments are:
- `error` (error) - The error.
- `subgraph` (string) - The erroring subgraph name.
Expand All @@ -221,8 +225,6 @@ main()
- `queryTypeName` (string, optional) - The name of the `Query` type in the composed schema. **Default:** `'Query'`.
- `mutationTypeName` (string, optional) - The name of the `Mutation` type in the composed schema. **Default:** `'Mutation'`.
- `subscriptionTypeName` (string, optional) - The name of the `Subscription` type in the composed schema. **Default:** `'Subscription'`.
- `defaultArgsAdapter` (function, optional) - The default `argsAdapter` function for the entities.
- `addEntitiesResolvers` (boolean, optional) - automatically add entities types and resolvers accordingly with configuration, see [composer entities section](#composer-entities).

- Returns
- A `Promise` that resolves with a `Composer` instance.
Expand Down
2 changes: 1 addition & 1 deletion fixtures/artists-subgraph-with-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,4 @@ const entities = {
}
}

module.exports = { name: 'artists', schema, reset, resolvers, entities, data }
module.exports = { name: 'artists-subgraph', schema, reset, resolvers, entities, data }
70 changes: 70 additions & 0 deletions fixtures/foods-subgraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use strict'

const schema = `
input WhereConditionIn {
in: [ID!]!
}

input FoodsWhereCondition {
id: WhereConditionIn
}

type Food {
id: ID!
name: String
}

type Query {
foods(where: FoodsWhereCondition): [Food]
}
`

const data = {
foods: null
}

function reset () {
data.foods = {
50: {
id: 50,
name: 'Pizza margherita'
},
51: {
id: 51,
name: 'Pizza boscaiola'
},
52: {
id: 52,
name: 'Pizza capricciosa'
},
60: {
id: 60,
name: 'Spaghetti carbonara'
},
61: {
id: 61,
name: 'Tagliolini scoglio'
},
62: {
id: 62,
name: 'Pici cacio e pepe'
},
63: {
id: 63,
name: 'Grigliata mista'
}
}
}

reset()

const resolvers = {
Query: {
foods (_, { where }) {
return Object.values(data.foods)
.filter(a => where.id.in.includes(String(a.id)))
}
}
}

module.exports = { name: 'foods-subgraph', schema, reset, resolvers, data }
2 changes: 1 addition & 1 deletion fixtures/movies-subgraph-with-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ const entities = {
}
}

module.exports = { name: 'movies', schema, reset, resolvers, entities, data }
module.exports = { name: 'movies-subgraph', schema, reset, resolvers, entities, data }
112 changes: 112 additions & 0 deletions fixtures/restaurants-subgraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
'use strict'

const schema = `
input WhereConditionIn {
in: [ID!]!
}

input RestaurantsWhereCondition {
id: WhereConditionIn
}

input RestaurantsFoodsWhereCondition {
restaurantId: WhereConditionIn
foodId: WhereConditionIn
}

type Restaurant {
id: ID!
businessName: String
}

type RestaurantsFoods {
restaurantId: ID!
foodId: ID!
}

type Query {
restaurants(where: RestaurantsWhereCondition): [Restaurant]
restaurantsFoods(where: RestaurantsFoodsWhereCondition): [RestaurantsFoods]
}
`

const data = {
restaurants: null
}

function reset () {
data.restaurantsFoods = [
{
restaurantId: 90,
foodId: 50
},
{
restaurantId: 90,
foodId: 51
},
{
restaurantId: 90,
foodId: 52
},

{
restaurantId: 91,
foodId: 60
},
{
restaurantId: 91,
foodId: 61
},
{
restaurantId: 91,
foodId: 62
},

{
restaurantId: 92,
foodId: 60
},
{
restaurantId: 92,
foodId: 63
}
]

data.restaurants = {
90: {
id: 90,
businessName: 'Pizzeria Napoletana'
},
91: {
id: 91,
businessName: 'Ristorante Stellato'
},
92: {
id: 92,
businessName: 'Trattoria da Gigi'
}
}
}

reset()

const resolvers = {
Query: {
restaurants (_, { where }) {
return Object.values(data.restaurants)
.filter(r => where.id.in.includes(String(r.id)))
},
restaurantsFoods (_, { where }) {
if (where.restaurantId?.in) {
return data.restaurantsFoods
.filter(rf => where.restaurantId.in.includes(String(rf.restaurantId)))
}
if (where.foodId?.in) {
return data.restaurantsFoods
.filter(rf => where.foodId.in.includes(String(rf.foodId)))
}
}
}
}

module.exports = { name: 'restaurants-subgraph', schema, reset, resolvers, data }
2 changes: 1 addition & 1 deletion fixtures/songs-subgraph-with-entities.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,4 @@ const entities = {
}
}

module.exports = { name: 'songs', schema, reset, resolvers, entities, data }
module.exports = { name: 'songs-subgraph', schema, reset, resolvers, entities, data }
Loading
Loading