Skip to content

Commit

Permalink
Keep requires directive in gateway schema (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
aleccool213 authored Mar 24, 2022
1 parent ea2e50d commit 57ff23a
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 22 deletions.
6 changes: 4 additions & 2 deletions lib/federation.js
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,13 @@ function addServiceResolver (schema, originalSchemaSDL) {

function buildGatewaySchemaDirectives (schema) {
// Remove service federation directives which should not be present in the gateway
const federationDirectiveNames = parse(BASE_FEDERATION_TYPES).definitions
const federationDirectiveNamesToRemove = parse(BASE_FEDERATION_TYPES).definitions
.filter(definition => definition.kind === 'DirectiveDefinition')
// Keep `@requires` directive as it can be used in the gateway schema
.filter(definition => definition.name.value !== 'requires')
.map(definition => definition.name.value)
const directives = schema.getDirectives()
.filter(directive => !federationDirectiveNames.includes(directive.name))
.filter(directive => !federationDirectiveNamesToRemove.includes(directive.name))

// De-duplicate custom directives
const gatewayDirectives = []
Expand Down
1 change: 1 addition & 0 deletions test/gateway/custom-directives-with-batching.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ t.test('gateway with batching', t => {
'skip',
'deprecated',
'specifiedBy',
'requires',
'custom'
])

Expand Down
1 change: 1 addition & 0 deletions test/gateway/custom-directives.js
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ t.test('gateway', t => {
'skip',
'deprecated',
'specifiedBy',
'requires',
'custom'
])

Expand Down
20 changes: 0 additions & 20 deletions test/gateway/pollingInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,16 +710,6 @@ test('Polling schemas (should properly regenerate the schema when a downstream s
})
t.teardown(() => clock.uninstall())
const oldSchema = `
directive @extends on INTERFACE | OBJECT
directive @external on FIELD_DEFINITION | OBJECT
directive @key(fields: String!) on INTERFACE | OBJECT
directive @provides(fields: String!) on FIELD_DEFINITION
directive @requires(fields: String!) on FIELD_DEFINITION
type Query {
me: User
}
Expand Down Expand Up @@ -804,16 +794,6 @@ test('Polling schemas (should properly regenerate the schema when a downstream s
})

const refreshedSchema = `
directive @extends on INTERFACE | OBJECT
directive @external on FIELD_DEFINITION | OBJECT
directive @key(fields: String!) on INTERFACE | OBJECT
directive @provides(fields: String!) on FIELD_DEFINITION
directive @requires(fields: String!) on FIELD_DEFINITION
type User @key(fields: "id") {
id: ID!
lastName: String!
Expand Down
77 changes: 77 additions & 0 deletions test/gateway/requires-directive.js
Original file line number Diff line number Diff line change
Expand Up @@ -449,3 +449,80 @@ test('gateway handles @requires directive correctly apart of other directives',
}
})
})

test('gateway exposes @requires directive in list of directives', async (t) => {
const users = {
u1: {
id: 'u1',
name: 'John'
},
u2: {
id: 'u2',
name: 'Jane'
}
}

const userService = await createService(`
extend type Query {
me: User
}
type User @key(fields: "id") {
id: ID!
name: String!
}
`, {
Query: {
me: (root, args, context, info) => {
return users.u1
}
},
User: {
__resolveReference: (user, args, context, info) => {
return users[user.id]
}
}
})

const biographyService = await createService(`
type User @key(fields: "id") @extends {
id: ID! @external
name: String @external
biography: String @requires(fields: "id name")
}
`, {
User: {
biography (user) {
return `${user.name} with id ${user.id} test biography`
}
}
})

const { gateway, teardown } = await createGateway(biographyService, userService)
t.teardown(teardown)

const query = `
query IntrospectionQuery {
__schema {
directives {
name
}
}
}
`
const res = await gatewayRequest(gateway, query)

t.same(JSON.parse(res.body), {
data: {
__schema: {
directives: [
{ name: 'include' },
{ name: 'skip' },
{ name: 'deprecated' },
{ name: 'specifiedBy' },
{ name: 'requires' }
]
}
}
})
})

0 comments on commit 57ff23a

Please sign in to comment.