-
Notifications
You must be signed in to change notification settings - Fork 345
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(supergraph-handler): handle subgraphs individually with interpola…
…ted endpoint and headers (#6758) * fix(supergraph-handler): handle subgraphs individually with interpolated endpoint and headers * Changeset
- Loading branch information
Showing
12 changed files
with
495 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@graphql-mesh/supergraph": patch | ||
--- | ||
|
||
Fix interpolation of headers and endpoint configuration for each subgraph |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/legacy/handlers/supergraph/tests/fixtures/service-author/resolvers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
export const resolvers = { | ||
Author: { | ||
__resolveReference(author: { id: string }) { | ||
return authors.find(a => a.id === author.id); | ||
}, | ||
}, | ||
Query: { | ||
author(_: any, { id }: { id: string }) { | ||
return authors.find(a => a.id === id); | ||
}, | ||
authors() { | ||
return authors; | ||
}, | ||
}, | ||
}; | ||
|
||
const authors = [ | ||
{ | ||
id: '1', | ||
name: 'Jane Doe', | ||
}, | ||
{ | ||
id: '2', | ||
name: 'John Doe', | ||
}, | ||
]; |
36 changes: 36 additions & 0 deletions
36
packages/legacy/handlers/supergraph/tests/fixtures/service-author/server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { GraphQLError, parse } from 'graphql'; | ||
import { createYoga } from 'graphql-yoga'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
import { resolvers } from './resolvers'; | ||
|
||
export const AUTH_HEADER = 'Bearer AUTHORS_SECRET'; | ||
|
||
export const server = createYoga({ | ||
schema: buildSubgraphSchema({ | ||
typeDefs: parse(readFileSync(join(__dirname, 'typeDefs.graphql'), 'utf-8')), | ||
resolvers, | ||
}), | ||
plugins: [ | ||
{ | ||
onRequest({ request }) { | ||
if (request.url.includes('skip-auth')) { | ||
return; | ||
} | ||
const authHeader = request.headers.get('authorization'); | ||
if (authHeader !== AUTH_HEADER) { | ||
throw new GraphQLError('Unauthorized', { | ||
extensions: { | ||
http: { | ||
status: 401, | ||
}, | ||
}, | ||
}); | ||
} | ||
}, | ||
}, | ||
], | ||
}); |
10 changes: 10 additions & 0 deletions
10
packages/legacy/handlers/supergraph/tests/fixtures/service-author/typeDefs.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
type Author @key(fields: "id") { | ||
id: ID! | ||
name: String! | ||
birthDate: String | ||
} | ||
|
||
type Query { | ||
authors: [Author] | ||
author(id: ID!): Author | ||
} |
44 changes: 44 additions & 0 deletions
44
packages/legacy/handlers/supergraph/tests/fixtures/service-book/resolvers.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
export const resolvers = { | ||
Book: { | ||
__resolveReference(book: { id: string }) { | ||
return books.find(b => b.id === book.id); | ||
}, | ||
author(book: { authorId: string }) { | ||
return { __typename: 'Author', id: book.authorId }; | ||
}, | ||
}, | ||
Author: { | ||
__resolveReference(author: { id: string }) { | ||
return books.filter(b => b.authorId === author.id); | ||
}, | ||
books(author: { id: string }) { | ||
return books.filter(b => b.authorId === author.id); | ||
}, | ||
}, | ||
Query: { | ||
book(_: any, { id }: { id: string }) { | ||
return books.find(b => b.id === id); | ||
}, | ||
books() { | ||
return books; | ||
}, | ||
}, | ||
}; | ||
|
||
const books = [ | ||
{ | ||
id: '1', | ||
title: 'Awesome Book', | ||
authorId: '1', | ||
}, | ||
{ | ||
id: '2', | ||
title: 'Book of Secrets', | ||
authorId: '2', | ||
}, | ||
{ | ||
id: '3', | ||
title: 'Book of Mystery', | ||
authorId: '2', | ||
}, | ||
]; |
36 changes: 36 additions & 0 deletions
36
packages/legacy/handlers/supergraph/tests/fixtures/service-book/server.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable import/no-nodejs-modules */ | ||
/* eslint-disable import/no-extraneous-dependencies */ | ||
import { readFileSync } from 'fs'; | ||
import { join } from 'path'; | ||
import { GraphQLError, parse } from 'graphql'; | ||
import { createYoga } from 'graphql-yoga'; | ||
import { buildSubgraphSchema } from '@apollo/subgraph'; | ||
import { resolvers } from './resolvers'; | ||
|
||
export const AUTH_HEADER = 'Bearer BOOKS_SECRET'; | ||
|
||
export const server = createYoga({ | ||
schema: buildSubgraphSchema({ | ||
typeDefs: parse(readFileSync(join(__dirname, 'typeDefs.graphql'), 'utf-8')), | ||
resolvers, | ||
}), | ||
plugins: [ | ||
{ | ||
onRequest({ request }) { | ||
if (request.url.includes('skip-auth')) { | ||
return; | ||
} | ||
const authHeader = request.headers.get('authorization'); | ||
if (authHeader !== AUTH_HEADER) { | ||
throw new GraphQLError('Unauthorized', { | ||
extensions: { | ||
http: { | ||
status: 401, | ||
}, | ||
}, | ||
}); | ||
} | ||
}, | ||
}, | ||
], | ||
}); |
16 changes: 16 additions & 0 deletions
16
packages/legacy/handlers/supergraph/tests/fixtures/service-book/typeDefs.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
type Book @key(fields: "id") { | ||
id: ID! | ||
title: String! | ||
genre: String | ||
author: Author! | ||
} | ||
|
||
type Author @extends @key(fields: "id") { | ||
id: ID! @external | ||
books: [Book] | ||
} | ||
|
||
type Query { | ||
books: [Book] | ||
book(id: ID!): Book | ||
} |
62 changes: 62 additions & 0 deletions
62
packages/legacy/handlers/supergraph/tests/fixtures/supergraph.graphql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
schema | ||
@core(feature: "https://specs.apollo.dev/core/v0.2") | ||
@core(feature: "https://specs.apollo.dev/join/v0.1", for: EXECUTION) { | ||
query: Query | ||
} | ||
|
||
directive @core(as: String, feature: String!, for: core__Purpose) repeatable on SCHEMA | ||
|
||
directive @join__field( | ||
graph: join__Graph | ||
provides: join__FieldSet | ||
requires: join__FieldSet | ||
) on FIELD_DEFINITION | ||
|
||
directive @join__graph(name: String!, url: String!) on ENUM_VALUE | ||
|
||
directive @join__owner(graph: join__Graph!) on INTERFACE | OBJECT | ||
|
||
directive @join__type(graph: join__Graph!, key: join__FieldSet) repeatable on INTERFACE | OBJECT | ||
|
||
type Author | ||
@join__owner(graph: AUTHORS) | ||
@join__type(graph: AUTHORS, key: "id") | ||
@join__type(graph: BOOKS, key: "id") { | ||
birthDate: String @join__field(graph: AUTHORS) | ||
books: [Book] @join__field(graph: BOOKS) | ||
id: ID! @join__field(graph: AUTHORS) | ||
name: String! @join__field(graph: AUTHORS) | ||
} | ||
|
||
type Book @join__owner(graph: BOOKS) @join__type(graph: BOOKS, key: "id") { | ||
author: Author! @join__field(graph: BOOKS) | ||
genre: String @join__field(graph: BOOKS) | ||
id: ID! @join__field(graph: BOOKS) | ||
title: String! @join__field(graph: BOOKS) | ||
} | ||
|
||
type Query { | ||
author(id: ID!): Author @join__field(graph: AUTHORS) | ||
authors: [Author] @join__field(graph: AUTHORS) | ||
book(id: ID!): Book @join__field(graph: BOOKS) | ||
books: [Book] @join__field(graph: BOOKS) | ||
} | ||
|
||
enum core__Purpose { | ||
""" | ||
`EXECUTION` features provide metadata necessary to for operation execution. | ||
""" | ||
EXECUTION | ||
|
||
""" | ||
`SECURITY` features provide metadata necessary to securely resolve fields. | ||
""" | ||
SECURITY | ||
} | ||
|
||
scalar join__FieldSet | ||
|
||
enum join__Graph { | ||
AUTHORS @join__graph(name: "authors", url: "http://authors/graphql") | ||
BOOKS @join__graph(name: "books", url: "http://books/graphql") | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/legacy/handlers/supergraph/tests/fixtures/supergraph.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
subgraphs: | ||
authors: | ||
routing_url: http://authors/graphql | ||
schema: | ||
file: ./service-author/typeDefs.graphql | ||
books: | ||
routing_url: http://books/graphql | ||
schema: | ||
file: ./service-book/typeDefs.graphql |
Oops, something went wrong.