Skip to content

Commit

Permalink
fix: entities resolution (#29)
Browse files Browse the repository at this point in the history
* fix: entities resolution

* chore: fix test skip
  • Loading branch information
simone-sanfratello authored Nov 14, 2023
1 parent 0eea5cf commit 942faec
Show file tree
Hide file tree
Showing 8 changed files with 1,052 additions and 717 deletions.
60 changes: 60 additions & 0 deletions fixtures/artists-subgraph.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
'use strict'

const schema = `
type Artist {
id: ID
firstName: String
lastName: String
profession: String
}
type Query {
artists(ids: [ID!]!): [Artist]
}
`

const data = {
artists: null
}

function reset () {
data.artists = {
101: {
id: 101,
firstName: 'Christopher',
lastName: 'Nolan',
profession: 'Director'
},
102: {
id: 102,
firstName: 'Roberto',
lastName: 'Benigni',
profession: 'Director'
},
103: {
id: 103,
firstName: 'Brian',
lastName: 'Molko',
profession: 'Singer'
}
}
}

reset()

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

const entities = {
Artist: {
referenceListResolverName: 'artists',
keys: [{ field: 'id' }]
}
}

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

const schema = `
type Movie {
id: ID!
title: String
directorId: ID
}
type Query {
movies(ids: [ID!]!): [Movie]
}
type Artist {
id: ID
movies: [Movie]
}
extend type Movie {
director: Artist
}
extend type Query {
movieArtists (ids: [ID!]!): [Artist]
}
`

const data = {
movies: null
}

function reset () {
data.movies = {
10: {
id: 10,
title: 'Interstellar',
directorId: 101
},
11: {
id: 11,
title: 'Oppenheimer',
directorId: 101
},
12: {
id: 12,
title: 'La vita é bella',
directorId: 102
}
}
}

reset()

const resolvers = {
Query: {
movies (_, { ids }) {
return Object.values(data.movies).filter(a => ids.includes(String(a.id)))
},
movieArtists: async (parent, args, context, info) => {
return args.ids.map(id => ({ id }))
}
},
Movie: {
director: (parent, args, context, info) => {
return parent?.directorId ? { id: parent.directorId } : null
}
},
Artist: {
// TODO dataloader here
movies: (parent, args, context, info) => {
return Object.values(data.songs).filter(a => String(a.directorId) === String(parent.id))
}
}
}

const entities = {
Movie: {
referenceListResolverName: 'movies',
keys: [{ field: 'id' }, { field: 'directorId', type: 'Artist' }]
},
Artist: {
referenceListResolverName: 'movieArtists',
argsAdapter: (partialResults) => {
return { ids: partialResults.map(r => r.id) }
},
keys: [{ field: 'id' }]
}
}

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

const schema = `
type Song {
id: ID!
title: String
singerId: ID
}
type Query {
songs(ids: [ID!]!): [Song]
}
type Artist {
id: ID
songs: [Song]
}
extend type Song {
singer: Artist
}
extend type Query {
songArtists (ids: [ID!]!): [Artist]
}
`

const data = {
songs: null
}

function reset () {
data.songs = {
1: {
id: 1,
title: 'Every you every me',
singerId: 103
},
2: {
id: 2,
title: 'The bitter end',
singerId: 103
},
3: {
id: 3,
title: 'Vieni via con me',
singerId: 102
}
}
}

reset()

const resolvers = {
Query: {
async songs (_, { ids }) {
return Object.values(data.songs).filter(a => ids.includes(String(a.id)))
},
songArtists: async (parent, args, context, info) => {
return args.ids.map(id => ({ id }))
}
},
Song: {
singer: (parent, args, context, info) => {
return parent?.singerId ? { id: parent.singerId } : null
}
},
Artist: {
// TODO dataloader here
songs: (parent, args, context, info) => {
return Object.values(data.songs).filter(a => String(a.singerId) === String(parent.id))
}
}
}

const entities = {
Song: {
referenceListResolverName: 'songs',
keys: [{ field: 'id' }, { field: 'singerId', type: 'Artist' }]
},
Artist: {
referenceListResolverName: 'songArtists',
argsAdapter: (partialResults) => {
return { ids: partialResults.map(r => r.id) }
},
keys: [{ field: 'id' }]
}
}

module.exports = { name: 'songs', schema, reset, resolvers, entities, data }
10 changes: 3 additions & 7 deletions lib/composer.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const { SubscriptionClient } = require('@mercuriusjs/subscription-client')
const { createEmptyObject, unwrapSchemaType } = require('./graphql-utils')
const { fetchSubgraphSchema, makeGraphqlRequest } = require('./network')
const { QueryBuilder } = require('./query-builder')
const { isObject, keySelection, createDefaultArgsAdapter } = require('./utils')
const { isObject, keySelection, createDefaultArgsAdapter, traverseResult } = require('./utils')
const {
validateArray,
validateFunction,
Expand Down Expand Up @@ -533,6 +533,7 @@ function mergeResults (query, partialResult, response) {

for (let i = 0; i < mergedPartial.length; i++) {
const merging = mergedPartial[i]
if (!merging) { continue }

// no need to be recursive
if (Array.isArray(merging)) {
Expand Down Expand Up @@ -577,12 +578,7 @@ function selectResult (query, partialResult, response) {

if (!mergedPartial && !mergedPartial[path]) { break }

const node = mergedPartial[path]
if (Array.isArray(mergedPartial)) {
mergedPartial = mergedPartial.map(r => r[path])
continue
}
mergedPartial = node
mergedPartial = traverseResult(mergedPartial, path)
}

if (!query.root) {
Expand Down
Loading

0 comments on commit 942faec

Please sign in to comment.