@graphql-hive/federation-gateway-audit
Version:
Audit tool for Apollo Federation Gateway
201 lines (188 loc) • 3.54 kB
JavaScript
'use strict';
var testkit = require('./testkit-CAf-AsDy.cjs');
require('@apollo/composition');
require('graphql');
require('fets');
require('@apollo/subgraph');
require('graphql-yoga');
const authors = [
{
id: "a1",
name: "John",
yearsOfExperience: 5
},
{
id: "a2",
name: "Jane",
yearsOfExperience: 20
}
];
const posts = [
{
id: "p1",
body: "p1-body",
author: authors[0]
},
{
id: "p2",
body: "p2-body",
author: authors[1]
}
];
var a = testkit.createSubgraph("a", {
typeDefs: (
/* GraphQL */
`
extend schema
type Query {
feed: [Post]
}
type Post {
id: ID!
byNovice: Boolean!
byExpert: Boolean!
}
type Author {
id: ID!
name: String!
yearsOfExperience: Int!
}
`
),
resolvers: {
Query: {
feed() {
return posts.map((post) => ({ id: post.id }));
}
},
Post: {
__resolveReference(ref) {
const post = posts.find((post2) => post2.id === ref.id);
if (!post) {
return null;
}
if (ref.byNovice == null) {
return {
id: post.id
};
}
return {
id: post.id,
byNovice: ref.byNovice
};
},
byExpert(post) {
if (post.byNovice == null) {
return null;
}
return !post.byNovice;
}
},
Author: {
__resolveReference(ref) {
const author = authors.find((author2) => author2.id === ref.id);
if (!author) {
return null;
}
return {
id: author.id,
name: author.name,
yearsOfExperience: author.yearsOfExperience
};
}
}
}
});
var b = testkit.createSubgraph("b", {
typeDefs: (
/* GraphQL */
`
extend schema
type Post {
id: ID!
author: Author!
byNovice: Boolean!
}
type Author {
id: ID!
yearsOfExperience: Int!
}
`
),
resolvers: {
Post: {
__resolveReference(ref) {
const post = posts.find((post2) => post2.id === ref.id);
if (!post) {
return null;
}
return {
id: post.id,
author: {
id: post.author.id,
...ref.author
}
};
},
byNovice(post) {
return post.author.yearsOfExperience < 10;
}
}
}
});
var test = [
testkit.createTest(
/* GraphQL */
`
{
feed {
byNovice
}
}
`,
{
data: {
feed: [
{
byNovice: true
},
{
byNovice: false
}
]
}
}
),
testkit.createTest(
/* GraphQL */
`
{
feed {
byExpert
}
}
`,
{
data: {
feed: [
{
byExpert: false
},
{
byExpert: true
}
]
}
}
)
];
var index = testkit.serve("requires-circular", [a, b], test);
exports.default = index;