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: select node when a type is duplicated #745

Closed
wants to merge 4 commits into from
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
49 changes: 43 additions & 6 deletions lib/gateway.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,37 @@ function isDefaultType (type) {
* posts: [Post]
* }
*/
function defineResolvers (schema, typeToServiceMap, serviceMap, typeFieldsToService) {
function defineResolvers (schema, typeToServiceMap, serviceMap, typeFieldsToService, duplicatedTypes) {
const types = schema.getTypeMap()

function getService (key, priorityService) {
const service = typeToServiceMap[key]
if (service || !duplicatedTypes[key] || priorityService === false) {
return service
}

const duplicatedType =
duplicatedTypes[key].find(s => s === priorityService) ||
duplicatedTypes[key][0] ||
null
return duplicatedType
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you move this to a top-level function?


for (const type of Object.values(types)) {
if (isObjectType(type) && !isDefaultType(type.name)) {
const serviceForType = typeToServiceMap[type.name]
const serviceForType = getService(type)

for (const field of Object.values(type.getFields())) {
const fieldType = getNamedType(field.type)
if (fieldType.astNode && fieldType.astNode.kind === Kind.ENUM_TYPE_DEFINITION) continue
const fieldName = field.name
if (!isScalarType(fieldType)) {
const serviceForFieldType = typeToServiceMap[fieldType]
// TODO: if the field type is/contains an external type,
// the getService function should skip the duplicated types logic
const isExternal = fieldType.astNode.isExternal

const serviceForFieldType = getService(fieldType, isExternal)

/* istanbul ignore else */
if (
(serviceForFieldType === null && serviceForType !== null) ||
Expand All @@ -143,7 +161,9 @@ function defineResolvers (schema, typeToServiceMap, serviceMap, typeFieldsToServ
* In this case, the field type is a value type and the type is neither a query, mutation nor a subscription.
* - Or there is a service for the type and a service for the field type and both refer to the same service.
*/
field.resolve = (parent, args, context, info) => parent && parent[info.path.key]
field.resolve = (parent, args, context, info) => {
return parent && parent[info.path.key]
}
} else if (serviceForType === null) {
/**
* If the return type of a query, subscription or mutation is a value type, its service is undefined or null, e.g. for
Expand Down Expand Up @@ -319,6 +339,7 @@ async function buildGateway (gatewayOpts, app) {
const typeToServiceMap = {}
const typeFieldsToService = {}
let allTypes = []
const duplicatedTypes = {}
const factory = new Factory()
app.decorateReply(kEntityResolvers)
app.addHook('onRequest', async function (req, reply) {
Expand All @@ -328,6 +349,13 @@ async function buildGateway (gatewayOpts, app) {
for (const [service, serviceDefinition] of Object.entries(serviceMap)) {
for (const type of serviceDefinition.types) {
allTypes.push(serviceDefinition.schema.getTypeMap()[type])
if (typeToServiceMap[type]) {
if (!duplicatedTypes[type]) {
duplicatedTypes[type] = [typeToServiceMap[type], service]
} else {
duplicatedTypes[type].push(service)
}
}
typeToServiceMap[type] = service
}

Expand Down Expand Up @@ -410,7 +438,7 @@ async function buildGateway (gatewayOpts, app) {
typeToServiceMap[typeName] = null
}

defineResolvers(schema, typeToServiceMap, serviceMap, typeFieldsToService)
defineResolvers(schema, typeToServiceMap, serviceMap, typeFieldsToService, duplicatedTypes)

return {
schema,
Expand Down Expand Up @@ -468,10 +496,18 @@ async function buildGateway (gatewayOpts, app) {
this._serviceSDLs = _serviceSDLs

allTypes = []
const duplicatedTypes = {}

for (const [service, serviceDefinition] of Object.entries(serviceMap)) {
for (const type of serviceDefinition.types) {
allTypes.push(serviceDefinition.schema.getTypeMap()[type])
if (typeToServiceMap[type]) {
if (!duplicatedTypes[type]) {
duplicatedTypes[type] = [typeToServiceMap[type], service]
} else {
duplicatedTypes[type].push(service)
}
}
typeToServiceMap[type] = service
}

Expand All @@ -494,12 +530,13 @@ async function buildGateway (gatewayOpts, app) {
typeToServiceMap.Mutation = null
typeToServiceMap.Subscription = null

// todo
const valueTypes = findValueTypes(allTypes)
for (const typeName of valueTypes) {
typeToServiceMap[typeName] = null
}

defineResolvers(schema, typeToServiceMap, serviceMap, typeFieldsToService)
defineResolvers(schema, typeToServiceMap, serviceMap, typeFieldsToService, duplicatedTypes)

return schema
},
Expand Down
168 changes: 168 additions & 0 deletions test/gateway/fix-743.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
'use strict'

const { test } = require('tap')
const Fastify = require('fastify')
const GQL = require('../..')

async function buildService () {
const app = Fastify()
const schema = `
extend type Query {
list: PageInfo
}

type User @key(fields: "id") {
id: ID!
name: String
}

type PageInfo {
edges: [User]
}
`

const resolvers = {
Query: {
list: () => {
return {
edges: [
{ id: 1, name: 'Davide' },
{ id: 2, name: 'Fiorello' }
]
}
}
}
}

app.register(GQL, {
schema,
resolvers,
federationMetadata: true,
allowBatchedQueries: true
})

return app
}

async function buildServiceExternal () {
const app = Fastify()
const schema = `
type PageInfo {
edges: [User]
}

type User @key(fields: "id") @extends {
id: ID! @external
}
`

const resolvers = {
// Query: {
// listx: () => {}
// }
}

app.register(GQL, {
schema,
resolvers,
federationMetadata: true,
allowBatchedQueries: true
})

return app
}

async function buildProxy (port1, port2) {
const proxy = Fastify()

proxy.register(GQL, {
graphiql: true,
gateway: {
services: [
{
name: 'ext1',
url: `http://localhost:${port1}/graphql`
},
{
name: 'ext2',
url: `http://localhost:${port2}/graphql`
}
]
},
pollingInterval: 2000
})

return proxy
}

test('federated node should be able to return aliased value if the type is declared in multiple places', async (t) => {
const port1 = 3040
const serviceOne = await buildService()
await serviceOne.listen(port1)
t.teardown(() => { serviceOne.close() })

const port2 = 3041
const serviceTwo = await buildServiceExternal()
await serviceTwo.listen(port2)
t.teardown(() => { serviceTwo.close() })

const serviceProxy = await buildProxy(port1, port2)
await serviceProxy.ready()
t.teardown(() => { serviceProxy.close() })

let res = await serviceProxy.inject({
method: 'POST',
url: '/graphql',
body: {
query: `{
list { edges { id name } }
}`
}
})

t.same(res.json(), {
data: {
list: {
edges: [
{
id: '1',
name: 'Davide'
},
{
id: '2',
name: 'Fiorello'
}
]
}
}
})

res = await serviceProxy.inject({
method: 'POST',
url: '/graphql',
body: {
query: `{
list {
items: edges { id name }
}
}`
}
})

t.same(res.json(), {
data: {
list: {
items: [
{
id: '1',
name: 'Davide'
},
{
id: '2',
name: 'Fiorello'
}
]
}
}
})
})
4 changes: 2 additions & 2 deletions test/gateway/type-redefined.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,12 @@ async function buildProxy (port1, port2) {
}

test('federated node should be able to redefine type', async (t) => {
const port1 = 3027
const port1 = 3030
const serviceOne = await buildService()
await serviceOne.listen(port1)
t.teardown(() => { serviceOne.close() })

const port2 = 3028
const port2 = 3031
const serviceTwo = await buildServiceExternal()
await serviceTwo.listen(port2)
t.teardown(() => { serviceTwo.close() })
Expand Down